<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>M. David Green &#187; CSS</title>
	<atom:link href="http://mdavidgreen.com/blog/category/css/feed/" rel="self" type="application/rss+xml" />
	<link>http://mdavidgreen.com/blog</link>
	<description>Existence is a byproduct of Semantics</description>
	<lastBuildDate>Tue, 28 Jun 2011 15:42:44 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Turn Your Mobile HTML 5 and CSS Strategy Inside Out</title>
		<link>http://mdavidgreen.com/blog/turn-your-mobile-html-5-and-css-strategy-inside-out/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=turn-your-mobile-html-5-and-css-strategy-inside-out</link>
		<comments>http://mdavidgreen.com/blog/turn-your-mobile-html-5-and-css-strategy-inside-out/#comments</comments>
		<pubDate>Tue, 28 Jun 2011 15:42:44 +0000</pubDate>
		<dc:creator>M. David Green</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[HTML 5]]></category>
		<category><![CDATA[Mobile]]></category>
		<category><![CDATA[Progressive Enhancement]]></category>
		<category><![CDATA[semantic HTML]]></category>

		<guid isPermaLink="false">http://mdavidgreen.com/blog/?p=254</guid>
		<description><![CDATA[Developing for mobile devices seems to come second for most web developers, after developing for the traditional desktop web browser. When thinking about supporting different browsers, we generally focus on progressive enhancement in terms of adding functionality rather than basic layout. But if you think about how your semantic HTML will be used, it&#8217;s likely [...]]]></description>
			<content:encoded><![CDATA[<p>Developing for mobile devices seems to come second for most web developers, after developing for the traditional desktop web browser. When thinking about supporting different browsers, we generally focus on progressive enhancement in terms of adding functionality rather than basic layout. But if you think about how your semantic HTML will be used, it&#8217;s likely you&#8217;ll come to the same conclusion that I did: a mobile device should be your first target development platform.</p>
<p>One of the principles of semantic HTML development is that you want to boil your page down to its most elemental, essential components before you start, and build your structure around these. HTML 5 provides a number of new page elements that can help identify those basic components, but it doesn&#8217;t change this key approach. </p>
<p>However, the need to support the smaller screens and tighter bandwidth associated with mobile devices actually provides a developer with a new opportunity. By paring down a site to the minimal set of components necessary for a complete mobile experience, a developer can create a solid semantic structure for that page right from the start. A page built in that manner is much easier to enhance for a richer desktop experience than it would be to trim away desktop features for a more minimal mobile device experience later.</p>
<p>This is the bare essence of progressive enhancement, and it will sharpen your focus as it streamlines your process.</p>
<p>To do this effectively, it may be useful to turn your CSS inside out, and build your standard stylesheet as if all you were developing was a mobile site. Once you have that working the way you want it on a mobile device, you can specify the presentation for desktop web browsers conditionally. This not only keeps your stylesheets lean and responsive; it also helps you focus your development efforts around the most critical elements of your site.</p>
<p>This may sound like a very different way of approaching your site development, but it&#8217;s not all that complicated, thanks to a few convenient CSS conventions. The real trick is thinking about your desktop site as an enhanced child of your mobile site, instead of the other way around.</p>
<p>For example, imagine you&#8217;re creating a new HTML 5 website with a main page, a set of related subpages, and a template that involves a standard header, footer, navigation menu, and a sidebar with a set of elements you want repeated on each page. You&#8217;re already ahead of the game if you&#8217;ve visualized this as a set of repeating components that can take advantage of our new semantic HTML 5 elements, such as <code>header</code>, <code>footer</code>, <code>nav</code>, <code>article</code>, <code>section</code>, and <code>aside</code>.</p>
<p>The first question to ask is what the simplest presentation of a site like this could be. If you stripped away anything that would detract from a user&#8217;s experience on a mobile device, what would you have left? Might the sidebar elements be extraneous? Or if they are necessary, do they have to float off to the side, squeezing the space available for the main content of a page, or could they be pushed down to the bottom of the page?</p>
<p>With these thoughts in mind, you might start building your semantic HTML 5 page skeleton by populating a <code>header</code>, <code>article</code>, and <code>footer</code>, inserting both a <code>section</code> and an <code>aside</code> into the <code>article</code>. Put the elements together in the order that they would be read logically if there were no styles applied, and you can begin to imagine how they will appear to a mobile device. So the <code>aside</code> would be placed after the <code>section</code> inside the <code>article</code>. That way it will easily fall after the <code>section</code> on a mobile screen when the page is built, unless you explicitly position it to the side.</p>
<p>After building the basic semantic HTML structure, and making sure that all the elements on the page use appropriate HTML 5 elements, you&#8217;re well on your way to having a mobile site that flows easily into a small screen with few bandwidth-hogging embellishments. In your stylesheet, you can now add all the design driven rules for features such as colors, fonts, alignments, and background images to make this lean and sleek mobile site support your branding and style guide requirements on your iphone or Android device, or any mobile device that supports CSS styling. Remember that all these styles will be inherited on the desktop, saving you the trouble of re-creating them independently for both environments.</p>
<p>To add styling for desktop browsers, you just need to add the following @media definition to your CSS stylesheet, and put your desktop-specific CSS definitions inside of it:</p>
<pre class="brush: css">
@media only screen and (min-width: 768px) {
    /* Put desktop CSS definition overrides here */
}
</pre>
<p>This definition includes a conditional statement querying the browser window to see if it is 768 pixes wide or wider. Since this definition falls after all your general CSS style definitions, it will override them selectively, and let the rest cascade through. And because these definitions sit inside of the @media definition, they will only be applied in the case of a browser with a width of 768px or more. </p>
<p>So for example, to get that <code>aside</code> sitting comfortably off to the right of your window in desktop browsers, you might want to float the <code>section</code> and the <code>aside</code> to the left, and give the <code>article</code> a hidden overflow and a right padding the width you want the sidebar to be. Then give the <code>section</code> a width of 100% and a border-right the width you want the sidebar to be, and a negative margin-right that matches. Then give the <code>aside</code> the same width, giving it a negative margin-right that matches the right border width of the <code>section</code>:</p>
<pre class="brush: css">
@media only screen and (min-width: 768px) {
    article {
        padding-right: 250px;
        overflow: hidden;
    }
    article section {
        float: left;
        width: 100%;
        border-right: 250px solid transparent;
        margin-right: -250px;
    }
    article aside {
        float: left;
        margin-right: -250px;
        width: 250px;
    }
}
</pre>
<p>Now your mobile device and your desktop browser will share most of the same basic styles, and the same semantic HTML structure, but the site will expand horizontally on browsers wider than 768px to allow the <code>aside</code> to appear as a sidebar next to your page content. </p>
<p>And there may be other rich presentation elements such as tabbed navigation or larger background images that you would want to incorporate into your desktop browser experience in the same way. This is the place in your CSS to do it.</p>
<p>Of course, some older browsers might not recognize conditional @media definitions. But that&#8217;s another benefit to using this approach. Those browsers will ignore these new definitions, and serve those users the pared-down but fully functional mobile-friendly version of your site&#8211;which is probably what you want when trying to support ancient technology in the 21st century.</p>
<p>Developing your site inside out, considering the mobile device application first, will strengthen your skills and improve your process:</p>
<ul>
<li>For web developers, it forces you to focus on the basics first, and use progressive enhancement at the deepest levels of your page structure.</li>
<li>For user interface and user experience designers, it isolates experiential design from visual design in a way that supports the advantages of each platform and how users expect them to behave.</li>
<li>And for visual designers, it encourages the creation of a device-independent style guide, as it separates branding from layout to optimize the user experience for each type of device.</li>
</ul>
<p>Once you get your head around it, you may never go back to developing pages for the desktop first. </p>
]]></content:encoded>
			<wfw:commentRss>http://mdavidgreen.com/blog/turn-your-mobile-html-5-and-css-strategy-inside-out/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HTML 5, Progressive Enhancement, and Semantic HTML</title>
		<link>http://mdavidgreen.com/blog/html-5-progressive-enhancement-and-semantic-html/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=html-5-progressive-enhancement-and-semantic-html</link>
		<comments>http://mdavidgreen.com/blog/html-5-progressive-enhancement-and-semantic-html/#comments</comments>
		<pubDate>Sun, 29 May 2011 18:20:31 +0000</pubDate>
		<dc:creator>M. David Green</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[HTML 5]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[semantic HTML]]></category>

		<guid isPermaLink="false">http://mdavidgreen.com/blog/?p=216</guid>
		<description><![CDATA[HTML 5 is supposed to change everything all over again, but can we use progressive enhancement, or does HTML 5 mean starting from scratch after all the lessons we&#8217;ve learned about semantic HTML and beautiful markup from HTML 4? Different web browsers interpret HTML differently. It&#8217;s a fact of life, and it can feel like [...]]]></description>
			<content:encoded><![CDATA[<p>HTML 5 is supposed to change everything all over again, but can we use progressive enhancement, or does HTML 5 mean starting from scratch after all the lessons we&#8217;ve learned about semantic HTML and beautiful markup from HTML 4? </p>
<p>Different web browsers interpret HTML differently. It&#8217;s a fact of life, and it can feel like a curse to HTML developers when their clean semantic HTML and CSS code fail to deliver the same results to users across all browsers. Yet the folks developing the next generation of browsers continue to push forward with browsers that include incompatible features from unreleased standards, or proprietary standards that lure us into innocently building web apps that break for our poor users on competing browsers. That&#8217;s happening right now with CSS 3 and HTML 5 (branded as &#8220;HTML5&#8243; by some and just plain &#8220;HTML&#8221; by others).</p>
<p>The proposed HTML 5 standards introduce a number of useful semantic elements that can help HTML developers plan the structure of their documents. But even with the new web standards in flux, the savvy HTML developer comfortable with the concept of progressive enhancement can start using these elements right now.</p>
<h2>New HTML 5 Semantic Elements</h2>
<p>Among the new semantic HTML elements to help a developer structure a web page are new general container elements called <code>header</code>, <code>footer</code>, and <code>nav</code>. These are block-level elements that can replace the conventional <code>div</code> with a class of header, footer, or nav commonly used by HTML developers for branding, nav bar, and attribution.</p>
<p>We now have <code>article</code>, <code>section</code>, and <code>aside</code> elements to group content within a page. And since a single page of HTML 5 may have multiple sections with multiple articles, or multiple articles with multiple sections, the cascading style sheet selectors for these elements should provide inheritable styles, just as it would be with a set of divs that share the same class. </p>
<p>Each of these articles or sections, as well as the main content area, may now also have its own hierarchy of headers from <code>h1</code> to <code>h6</code>, wrapped safely inside its own <code>header</code> elements. No more limiting yourself to a single <code>h1</code> per page. The CSS styles for these headers can be inherited logically based on the containers.</p>
<h2>Progressive Enhancement</h2>
<p>But we still live in a world where we have no control over the browsers a user might have. Is it an old one, designed well before standards for HTML 5 were even proposed? Or is it a new one, with a unique idea of how to interpret the elements in this new unfinished web standard?</p>
<p>Happily, there are some Javascript utilities that are coming to the rescue. One of the smallest and simplest is the <a href="http://code.google.com/p/html5shim/" target="_blank">Google HTML 5 Shim</a>. This simply allows older browsers to recognize the current set of HTML 5 elements for the sake of CSS styling. Yes, this even works in Internet Explorer 6!</p>
<p>Another tool is <a href="http://www.modernizr.com/" target="_blank">Modernizr</a>. This handy bit of code includes a shim, but also adds several enhancements that allow features such as advanced CSS3 selectors to take advantage of the native features of older browsers. After all, if we&#8217;re going to create our beautiful markup, we want our cascading style sheets to have all the modern advantages, too. To get started using it, all you have to do is include the script, and add a &#8220;no-js&#8221; class to your <code>html</code> element. </p>
<h2>What We&#8217;re Losing</h2>
<p>The proposed XHTML standard many of us adopted&mdash;in spite of the way it was actually interpreted by web browsers&mdash;isn&#8217;t going away. However, future development has been halted, and we are being encouraged to use HTML 5 instead for web development. HTML 5, like earlier flavors of HTML, doesn&#8217;t require us to close all of our elements the way XHTML does, and supports target tags for opening links in separate windows. Of course, creating structured HTML with XML-inspired closed elements is still a good practice, as it makes our page structure cleaner and easier to manage and maintain.</p>
<p>One of the convenient enhancements that follows the practice of actual HTML developers who ignored the published standard is that we no longer have to worry about including block-level elements inside our anchor elements. It is now permitted to put an entire div inside a link, and our HTML 5 markup with still be compliant.</p>
<p>And although we&#8217;ve all learned to favor <code>em</code> and <code>strong</code> elements over <code>i</code> and <code>b</code> elements&#8230;some of the time&#8230;HTML 5 has reinvented our old bold and italic friends, redefining them based on how they have been used by HTML developers over the years instead of deprecating them.</p>
<p>There are plenty of other changes in the proposed HTML 5 standards, but these are a few that might get you started down the right path as you apply the concepts of progressive enhancement to your HTML development.</p>
<p>So with a good sense of semantic structure, we can approach any web design we encounter and parse it into clean, cross-browser compliant HTML 5 based on the web standards that exist. It just takes a little sense, a little discipline, and some Javascript tricks, to help our web sites survive whatever the browser developers throw at us!</p>
]]></content:encoded>
			<wfw:commentRss>http://mdavidgreen.com/blog/html-5-progressive-enhancement-and-semantic-html/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Truncating Styled Text to Fit a Container Using JQuery</title>
		<link>http://mdavidgreen.com/blog/truncating-styled-text-to-fit-a-container-using-jquery/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=truncating-styled-text-to-fit-a-container-using-jquery</link>
		<comments>http://mdavidgreen.com/blog/truncating-styled-text-to-fit-a-container-using-jquery/#comments</comments>
		<pubDate>Sun, 13 Sep 2009 06:22:15 +0000</pubDate>
		<dc:creator>M. David Green</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[JQuery]]></category>

		<guid isPermaLink="false">http://mdavidgreen.com/blog/?p=132</guid>
		<description><![CDATA[Recently I had the need to truncate a section of styled HTML text so it would fit inside a container with a specific pixel width. You might have thought this would be something pretty easy to make happen with a little JQuery and some .css() and .width() commands. You would have been mistaken. I know [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I had the need to truncate a section of styled HTML text so it would fit inside a container with a specific pixel width. You might have thought this would be something pretty easy to make happen with a little <a href="http://www.jquery.com">JQuery</a> and some .css() and .width() commands.</p>
<p>You would have been mistaken. I know I was.</p>
<p>Among the tricky aspects was my desire that the text retain its styling, regardless of the width of the container. I also wanted it to truncate at the appropriate complete letter, rather than chop a word off in the middle of a letter. I wanted to be able to specify a string to use to indicate truncation (eg: &#8220;&#8230;&#8221;). I wanted the complete text to be preserved, and added to the element as a title tag so that it would be available to readers who hovered over the truncated text. And of course, I wanted it to work seamlessly across browsers.</p>
<p>What I decided I needed was a temporary DOM object where I could copy the styled text. I needed something that would report its width based on the width of the text it contained. That ruled out <em>both</em> block elements such as divs (which take their width from their container) and inline elements such as spans (which report zero when queried about their width, regardless of their actual contents). What to do?</p>
<p>Then I realized that tables adjust their width to the width of their contents, AND are able to report that width when queried. Eureka! I just needed to add a temporary table to the DOM somewhere that it would be untouched by other styling, and make sure all the elements that made up the table had no padding, margin, or border settings. I didn&#8217;t need to worry about the semantics of using a table this way since I was going to remove it from the DOM as soon as I was done with it. You won&#8217;t hear me saying things like that very often, but in this case even I was satisfied.</p>
<p>To copy the styled text, I had to take a two-step approach. After creating a temporary table, I copied the contents of the original element I wanted to measure, and then copied each of the original element&#8217;s font-relates styles and applied them to the inner td of the table. Firefox immediately rewarded me with an accurate measurement, which I could use in a recursive loop to chop away one letter at a time until I had the width I desired. It looked like I was going to be done with this one quickly.</p>
<p>I replaced the contents of the original element with the truncated text easily. I tossed in a call to cache the full text and apply it as a title attribute if the text needed truncating. I even made a quick swap of the truncation indicator text (&#8220;&#8230;&#8221;) into the styled table cell from the start, so I could account for the pixels that added to the width. It was looking pretty sweet. </p>
<p>Until I tested it in Internet Explorer. </p>
<p>Now the creepy thing was that it even worked in Internet Explorer with the font size set to the default value. But as soon as I tried font sizes that were larger or smaller, I started seeing the width vary in odd ways. Larger fonts would result in a reported width that was wider by an ever-increasing factor, resulting in truncated strings that were too short for the space. Smaller fonts did the opposite, reporting a narrower width than they actually required, resulting in truncated strings too long to fit the desired width. It wasn&#8217;t a straight line rate of change; I could see there was some mysterious equation at work here.</p>
<p>I will admit it took me a few hours of fiddling, and I had to dust off that part of my brain that was paying attention during high school algebra. What I discovered (and this is the <a href="http://www.viewpoints.com/Wylers-Mrs-Grass-soup-mix-Golden-Flavor-Nugget-reviews">golden nugget</a> in this chicken soup of a blog posting) was that Microsoft calculates the pixel size of rendered text based on the square root of the desired font size divided by a base font size of 16.</p>
<p>That works out like this in Internet Explorer:</p>
<pre class="brush: javascript">
Math.sqrt(parseFloat($(&#039;#element&#039;).css(&quot;font-size&quot;))/16)
</pre>
<p>Whereas other browsers use a formula more like this:</p>
<pre class="brush: javascript">
parseFloat($(&#039;#element&#039;).css(&quot;font-size&quot;))/16
</pre>
<p>Once I had that worked out, the rest of the project came together nicely. Here&#8217;s the code for the set of JQuery plugins I ended up with. One does the truncation, and relies on the other one to get the width of the text:</p>
<pre class="brush: javascript">
(function ($) {
  // A plugin to truncate the text inside an element to a given width
  // This plugin depends on the textWidth() plugin, which should be below
  $.fn.textTruncate = function(width,marginText) {
    var that = this;
    var width = width || &quot;250&quot;; // default width of 250 px
    var marginText = marginText || &quot;...&quot;; // default margin text of &quot;...&quot;
    that.css(&quot;visibility&quot;,&quot;hidden&quot;); // The element should be hidden in CSS
    return this.each(function () {
      // A table as a temporary dom element for measuring the text width
      $(&#039;body&#039;).append(&#039;&lt;table id=&quot;textWidthMeasurer&quot; style=&quot;padding:0;margin:0;border:0;width:auto;zoom:1;position:relative;&quot;&gt;&lt;tr style=&quot;padding:0;margin:0;border:0;&quot;&gt;&lt;td style=&quot;padding:0;margin:0;border:0;white-space:nowrap;&quot;&gt;&#039; + marginText + &#039;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&#039;);
      var measurer = $(&#039;#textWidthMeasurer&#039;);
      var margin = measurer.textWidth(measurer);
      if (that.textWidth(measurer) &gt; width) {
        var contentLength = that.text().length;
        that.attr(&quot;title&quot;,that.text());
        while (that.textWidth(measurer) &gt;= width - margin) {
          contentLength--;
          that.text(that.text().substring(0,contentLength));
        }
        that.text($.trim(that.text()) + marginText);
      }
      // Make the element visible and remove the measuring table
      that.css(&quot;visibility&quot;,&quot;visible&quot;);
      $(&#039;#textWidthMeasurer&#039;).remove();
    });
  };

  // A helper plugin to get the width of the text inside an element
  $.fn.textWidth = function(context,css) {
    var that = this;
    var context = context || null;
    var css = css || null;
    // Optionally pass in an array of additional CSS properties to use for measuring
    var properties = [&#039;font-family&#039;,&#039;font-weight&#039;,&#039;font-style&#039;,&#039;letter-spacing&#039;];
    if ((css != null) &amp;amp;&amp;amp; (css[0] != null)) {
      properties.concat(css);
    }
    // Establish a default context if none is passed in (slow)
    if (context == null) {
      if ($(&#039;#textWidthMeasurer&#039;) == null) {
        $(&#039;body&#039;).append(&#039;&lt;table id=&quot;textWidthMeasurer&quot; style=&quot;padding:0;margin:0;border:0;width:auto;zoom:1;position:relative;&quot;&gt;&lt;tr style=&quot;padding:0;margin:0;border:0;&quot;&gt;&lt;td style=&quot;padding:0;margin:0;border:0;white-space:nowrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&#039;);
      }
      var context = $(&#039;#textWidthMeasurer&#039;);
    }
    var target = $(&#039;td&#039;,context);
    // IE uses a bizarre formula to calculate the pixel value of font sizes:
    var fontSize = ($.browser.msie) ?
      Math.sqrt(parseFloat(that.css(&quot;font-size&quot;))/16) + &quot;em&quot; :
      parseFloat(that.css(&quot;font-size&quot;))/16 + &quot;em&quot;;
    target.text(that.text()).css(&#039;font-size&#039;,fontSize);
    properties.forEach(function(property) {
      target.css(property,that.css(property));
    });
    var width = context.width();
    return width;
  };
})(jQuery);
</pre>
<p>If you want a demo, you can check out <a href="http://www.mdavidgreen.com/textTruncate/" target="_blank">a sample page</a> with styled text that adjusts to match the width specified.</p>
<p>You can also download the <a href="http://www.mdavidgreen.com/textTruncate/textTruncate.zip">source code</a> for the plugin set. It&#8217;s open source, and free to use in commercial or personal projects under an MIT (X11) license.</p>
<p>Let me know what you think.</p>
]]></content:encoded>
			<wfw:commentRss>http://mdavidgreen.com/blog/truncating-styled-text-to-fit-a-container-using-jquery/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Making Apache serve HAML and SASS pages dynamically</title>
		<link>http://mdavidgreen.com/blog/making-apache-serve-haml-and-sass-pages-dynamically/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=making-apache-serve-haml-and-sass-pages-dynamically</link>
		<comments>http://mdavidgreen.com/blog/making-apache-serve-haml-and-sass-pages-dynamically/#comments</comments>
		<pubDate>Tue, 04 Aug 2009 04:47:22 +0000</pubDate>
		<dc:creator>M. David Green</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HAML]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[SASS]]></category>

		<guid isPermaLink="false">http://mdavidgreen.com/blog/?p=112</guid>
		<description><![CDATA[I&#8217;m a big fan of the elegant &#8220;haiku&#8221; mark-up syntax HAML and it&#8217;s CSS cousin SASS. But it&#8217;s been tricky getting these to work independent of the Rails framework for Ruby. I&#8217;ve managed to get pretty far with the apparently abandoned but quite satisfactory phpHaml project, but I wanted something a little more complete and [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m a big fan of the elegant &#8220;haiku&#8221; mark-up syntax <a href="http://haml-lang.com/">HAML </a>and it&#8217;s CSS cousin <a href="http://sass-lang.com/">SASS</a>. But it&#8217;s been tricky getting these to work independent of the <a href="http://rubyonrails.org/">Rails framework for Ruby</a>. I&#8217;ve managed to get pretty far with the apparently abandoned but quite satisfactory <a href="http://sourceforge.net/projects/phphaml/">phpHaml</a> project, but I wanted something a little more complete and straightforward.</p>
<p>I wanted to be able to just write an /index.haml file that referenced a /stylesheets/index.sass file, upload them to my webroot, and just have them render in any browser.</p>
<p>This weekend I came up with a simple solution that seems to work pretty efficiently. It doesn&#8217;t do any code caching, so the pages are interpreted from HAML and SASS every time they are loaded. But that&#8217;s the kind of dynamic access I was looking for. The engines are efficient enough that the processing time doesn&#8217;t seem to slow down access much. </p>
<p>The basis of the code is a .htaccess file that captures requests for files with the .haml and .sass suffices, and passes them to the appropriate Ruby cgi for processing with the latest and greatest HAML. The rendered HTML and CSS code are sent straight to the browser. You just need a ruby installation with the haml gem installed on your Apache-based web server. This works fine on <a href="http://www.dreamhost.com/">shared virtual hosts</a>, since there is no modification of any configuration files required.</p>
<p>Here&#8217;s a basic version of the .htaccess file, stripped down to just the HAML and SASS aspects. I&#8217;ve also defined a DirectoryIndex that gives preference to index.haml over index.html.</p>
<pre class="brush: sh">
DirectoryIndex index.haml index.html

AddHandler hamlpage .haml
Action hamlpage /cgi-bin/haml.cgi

AddHandler sasspage .sass
Action sasspage /cgi-bin/sass.cgi
</pre>
<p>The haml.cgi to parse the HAML goes into the cgi-bin directory. (Note that the seemingly extra &#8220;puts&#8221; command on a separate line is required to make the cgi work. Leaving it out will bring you nothing but sorrow and 500 errors.):</p>
<pre class="brush: ruby">
#!/usr/bin/ruby
require &#039;cgi&#039;
cgi = CGI.new
exit if cgi.path_translated.nil?
exit unless File.exists?(cgi.path_translated)
haml_file = cgi.path_translated
require &#039;rubygems&#039;
require &#039;haml&#039;
template = File.read(haml_file)
haml_engine = Haml::Engine.new(template, :format =&gt;:html4)
output = haml_engine.to_html()
puts &quot;Content-type: text/html&quot;
puts
puts output
</pre>
<p>The sass.cgi to parse .sass files looks very similar, and goes in the same place:</p>
<pre class="brush: ruby">
#!/usr/bin/ruby
require &#039;cgi&#039;
cgi = CGI.new
exit if cgi.path_translated.nil?
exit unless File.exists?(cgi.path_translated)
sass_file = cgi.path_translated
require &#039;rubygems&#039;
require &#039;sass&#039;
template = File.read(sass_file)
sass_engine = Sass::Engine.new(template, :style=&gt;:compact)
output = sass_engine.to_css()
puts &quot;Content-type: text/css&quot;
puts
puts output
</pre>
<p>With these files in place, and given executable permissions, you should be able to create a .haml file like this:</p>
<pre class="brush: html">
!!! strict
%html
  %head
    %link{ :href =&gt; &#039;stylesheets/test.sass&#039;, :rel =&gt; :stylesheet, :type =&gt; &#039;text/css&#039;, :media =&gt; &#039;Screen&#039; }
  %body.test_page
    %h1 Hello World
    %ul
      - (1..3).each do |counter|
        %li
          This is line
          &amp;= counter
</pre>
<p>With a .sass file that looks like this:</p>
<pre class="brush: php">
.test_page
  h1
    color: #f00
</pre>
<p>And get an HTML output file like this:</p>
<pre class="brush: html">
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;link href=&#039;stylesheets/test.sass&#039; media=&#039;Screen&#039; rel=&#039;stylesheet&#039; type=&#039;text/css&#039;&gt;
  &lt;/head&gt;
  &lt;body class=&#039;test_page&#039;&gt;
    &lt;h1&gt;Hello World&lt;/h1&gt;
    &lt;ul&gt;
      &lt;li&gt;
        This is line
        1
      &lt;/li&gt;
      &lt;li&gt;
        This is line
        2
      &lt;/li&gt;
      &lt;li&gt;
        This is line
        3
      &lt;/li&gt;
    &lt;/ul&gt;
  &lt;/body&gt;
&lt;/html&gt;
</pre>
<p>With associated CSS that looks like this:</p>
<pre class="brush: css">
.test_page h1 { color: #f00; }
</pre>
<p>The SASS engine doesn&#8217;t seem to be as efficient as the HAML engine, so it would probably be my first candidate for caching. But as an immediate way to explore the flexibility and expressiveness of HAML and SASS, this was a very satisfying little project for me.</p>
<p>If you want to try it locally, here&#8217;s <a href="http://www.mdavidgreen.com/HAML_and_SASS.zip">a zip file</a> with the code above, structured the way you would want it in your web root directory.</p>
]]></content:encoded>
			<wfw:commentRss>http://mdavidgreen.com/blog/making-apache-serve-haml-and-sass-pages-dynamically/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>How to Coordinate CSS with Javascript</title>
		<link>http://mdavidgreen.com/blog/how-to-coordinate-css-with-javascript/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-coordinate-css-with-javascript</link>
		<comments>http://mdavidgreen.com/blog/how-to-coordinate-css-with-javascript/#comments</comments>
		<pubDate>Sun, 25 Jan 2009 05:44:23 +0000</pubDate>
		<dc:creator>M. David Green</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[JQuery]]></category>

		<guid isPermaLink="false">http://mdavidgreen.com/blog/?p=48</guid>
		<description><![CDATA[I recommend using unique id's for targeting Javascript to an element, and using classes and inheritance to apply styles to an element.]]></description>
			<content:encoded><![CDATA[<p>Javascript is used to change the content of an HTML page, and apply various CSS styles, dynamically in response to user action and other factors. When writing HTML and CSS, consider how you might use Javascript to manipulate the pages. It is important to provide the Javascript programmers with useful semantic targets which they can populate with new information, or style by applying different CSS. For example, if there is a word in your document that you know will be generated in Javacscript, create a &lt;span&gt;&#8230;&lt;/span&gt; to contain it, and allow the Javascript programmer to assign it an appropriate id.</p>
<p>If the Javascript programmer needs to alter the HTML, move HTML blocks from one place to another using Javascript commands such as the JQuery commands .after() or .before(), or dynamically generate CSS styles not defined in our CSS pages, our application will be more brittle and harder to manipulate in the future.</p>
<p>Knowing how the page will work before coding the HTML skeleton is critical. For example, it allows us to create the spans which the Javascript programmers will fill with information, and the divs which will be shown, hidden, and otherwise manipulated. With AJAX, a single page may change radically over its lifetime on a user&#8217;s screen. Examine the page carefully to see which elements will change, and which ones will stay the same. We are less likely to break the layout of a page if we replace the content of a single defined header div across the lifetime of the page than we are if we hide that header div and then show another in the same location.</p>
<p>The <a href="http://www.jquery.com">JQuery Javascript library</a> allows programmers to target HTML elements in a page using the same CSS definitions that define the style of the elements. While that is convenient, it is generally more flexible to separate the functionality of the Javascript from the presentation of the CSS. That way, if things change in the style, they are less likely to compromise the program logic. Javascript is also much more efficient about parsing a large document for id&#8217;s than for classes. Therefore, I recommend using unique id&#8217;s for targeting Javascript to an element, and using classes and inheritance to apply styles to an element.</p>
]]></content:encoded>
			<wfw:commentRss>http://mdavidgreen.com/blog/how-to-coordinate-css-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>When to use classes or ID&#8217;s in CSS</title>
		<link>http://mdavidgreen.com/blog/when-to-use-classes-or-ids-in-css/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=when-to-use-classes-or-ids-in-css</link>
		<comments>http://mdavidgreen.com/blog/when-to-use-classes-or-ids-in-css/#comments</comments>
		<pubDate>Sat, 27 Dec 2008 03:18:59 +0000</pubDate>
		<dc:creator>M. David Green</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://mdavidgreen.com/blog/?p=41</guid>
		<description><![CDATA[One of the problems I often run across when I&#8217;m trying to work with someone else&#8217;s CSS code is the abuse of classes and ID&#8217;s. When to Use Classes Classes are used for styles that may be used repeatedly for different similar elements. In CSS (and JQuery), a class is indicated by a . character. [...]]]></description>
			<content:encoded><![CDATA[<p>One of the problems I often run across when I&#8217;m trying to work with someone else&#8217;s CSS code is the abuse of classes and ID&#8217;s.</p>
<h2>When to Use Classes</h2>
<p>Classes are used for styles that may be used repeatedly for different similar elements.</p>
<p>In CSS (and JQuery), a class is indicated by a . character.</p>
<p>in HTML, a class is indicated by adding class=&#8221;classname&#8221; to an element.</p>
<p>Use a class as the default method to apply a style to any element on a page.</p>
<p>Classes may be inherited from a parent element, and a single element may use multiple classes by using a space-separated list of classes, so try to re-use existing classes whenever practical.</p>
<p>This is a CSS style definition, for the class &#8220;title&#8221;</p>
<pre class="brush: css">.title {color:#f00;font-weight:bold;}</pre>
<p>It will apply to any HTML element with the class &#8220;title&#8221;</p>
<pre class="brush: html">
&lt;p class=&quot;title&quot;&gt;This is a title&lt;/p&gt;
&lt;div class=&quot;title&quot;&gt;
    &lt;p&gt;This title inherits its class from the contining div&lt;/p&gt;
&lt;/div&gt;
</pre>
<h2>When to Use ID&#8217;s</h2>
<p>ID&#8217;s are used to identify page elements that are unique to a page, such as input fields in a form, or header blocks that are only used once on a page, but may be used the same way on every page in a site.</p>
<p>In CSS (and JQuery), an id is indicated by a # character.</p>
<p>in HTML, an id is indicated by adding id=&#8221;idname&#8221; to an element.</p>
<p>Use an id if you are creating a major section of a page, such as a header or a navigation bar, or a form element such as an input field, a select list, or a checkbox. Otherwise, it&#8217;s preferable to allow the Javascript programmers to assign id&#8217;s to elements.</p>
<p>This is a CSS style definition, for the id header</p>
<pre class="brush: css">
#header {background-color:#000;color:#ddd;border:3px solid #ccc;padding:0.5em;font-family:sans-serif;}
</pre>
<p>It will apply to the single unique HTML element with the id &#8220;header&#8221;</p>
<pre class="brush: html">
&lt;div id=&quot;header&quot;&gt;
    &lt;p&gt;This is the only div on the page with the id of header&lt;/p&gt;
    &lt;p class=&quot;title&quot;&gt;This paragraph inherits both the #header and the .title styles&lt;/p&gt;
&lt;/div&gt;
</pre>
]]></content:encoded>
			<wfw:commentRss>http://mdavidgreen.com/blog/when-to-use-classes-or-ids-in-css/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Efficient HTML Skeleton Construction</title>
		<link>http://mdavidgreen.com/blog/efficient-html-skeleton-construction/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=efficient-html-skeleton-construction</link>
		<comments>http://mdavidgreen.com/blog/efficient-html-skeleton-construction/#comments</comments>
		<pubDate>Wed, 17 Dec 2008 00:16:58 +0000</pubDate>
		<dc:creator>M. David Green</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Semantics]]></category>
		<category><![CDATA[semantichtml]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://mdavidgreen.com/blog/?p=22</guid>
		<description><![CDATA[The basics are always worth considering again. Sometimes it&#8217;s the obvious things you don&#8217;t think about that can cause the worst problems. The wrong doctype can spoil all your hard cross-browser CSS styling efforts. The wrong class name can lock you into awkward naming conventions. Even something as common as putting your script tags in [...]]]></description>
			<content:encoded><![CDATA[<p>The basics are always worth considering again. Sometimes it&#8217;s the obvious things you don&#8217;t think about that can cause the worst problems. The wrong doctype can spoil all your hard cross-browser CSS styling efforts. The wrong class name can lock you into awkward naming conventions. Even something as common as putting your script tags in the head of your document can slow down your site. </p>
<p>As we enter 2009, this is my state-of-the-art approach to constructing an efficient, semantic, and flexible web site. I&#8217;ll start with the HTML skeleton:</p>
<p>A basic HTML page skeleton should tell the browser what kind of content to present, in what order the content should appear, and what to do with it.</p>
<p>I start with a !DOCTYPE to tell the browser what our page will consist of; for example, a public HTML document that complies with w3C standards for HTML 4.01 Strict.</p>
<p>Next I have the HTML element, which consists of a HEAD and a BODY element. Note that you have to close the HTML, HEAD, and BODY tags (/HTML, /HEAD, /BODY)at the end of each definition.</p>
<p>In the HEAD I include the title, the encoding standard, and references to external stylesheet CSS files, starting with a CSS reset script, a possible CSS script with code for all the shared elements across a set of pages, and then a CSS script with code specific to this particular page.</p>
<p>In the BODY I create elements of semantic HTML and fill them with content.</p>
<p>At the end of the BODY tag I include SCRIPT tags. These tags load external JavaScript libraries, such as JQuery, or contain document-specific Javascript.</p>
<p>The code below will create a page that looks like <a href="http://www.mdavidgreen.com/semantic/hello.html" target="_blank">this</a>.</p>
<pre class="brush: html">
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
    &lt;title&gt;Hello Page&lt;/title&gt;
	&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html;charset=ansi&quot; &gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;css/reset.css&quot; type=&quot;text/css&quot;&gt;
    &lt;link rel=&quot;stylesheet&quot; href=&quot;css/hello.css&quot; type=&quot;text/css&quot;&gt;
&lt;/head&gt;
&lt;body&gt;
    &lt;p class=&quot;description&quot;&gt;The box on this page is ten lines down and has a dotted border.&lt;/p&gt;
    &lt;div id=&quot;mainContent&quot;&gt;
        &lt;p&gt;Hello World!&lt;/p&gt;
    &lt;/div&gt;
	&lt;script type=&quot;text/javascript&quot; src=&quot;js/jquery-1.2.6.min.js&quot;&gt;&lt;/script&gt;
	&lt;script type=&quot;text/javascript&quot;&gt;
	    $(function() {
	        $(&quot;#mainContent&quot;).click( function() {
	            alert(&quot;You clicked me&quot;);
	        });
	    });
	&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Since this page is called hello.html, the visual style of the elements on this page is defined in the a hello.css file, which is coded like this:</p>
<pre class="brush: css">
/* Formatting for the hello.html page */
mainContent {width:50%;position:relative;top:10em;margin:0.5em;padding:0.5em;border:2px dotted #ff0;}  

/* Styles for the hello.html page */
body {color:#fff;background-color:#005;}
mainContent p {color:#fcc;text-align:center;font-family:impact,helvetica,sans-serif;font-size:3em;}
.description {text-align:center;font-style:italic;}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://mdavidgreen.com/blog/efficient-html-skeleton-construction/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

