<?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>Jaysen Marais &#187; tips</title>
	<atom:link href="http://www.jaysenmarais.com/blog/tag/tips/feed" rel="self" type="application/rss+xml" />
	<link>http://www.jaysenmarais.com/blog</link>
	<description>Making it happen, bit by bit</description>
	<lastBuildDate>Tue, 10 Jan 2012 06:50:13 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Ragged CSS: makes life easier</title>
		<link>http://www.jaysenmarais.com/blog/20090126/ragged-css-makes-life-easier</link>
		<comments>http://www.jaysenmarais.com/blog/20090126/ragged-css-makes-life-easier#comments</comments>
		<pubDate>Mon, 26 Jan 2009 19:55:44 +0000</pubDate>
		<dc:creator>jaysen</dc:creator>
				<category><![CDATA[web development]]></category>
		<category><![CDATA[code formatting]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[semantic html]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[web design]]></category>

		<guid isPermaLink="false">http://jaysenmarais.com/blog/?p=95</guid>
		<description><![CDATA[The web community is generally coming (or has come) to accept that &#8216;semantic&#8217; class naming conventions (&#8216;old school&#8217; elements with classes describing content not appearance) are preferable to the 'boldGreyLink' type conventions popular around the millennium. Millenium era HTML &#38; CSS &#8216;Semantic&#8217; era HTML &#38; CSS &#60;html&#62; &#60;head&#62; &#60;title&#62;'Millenium' era&#60;/title&#62; &#60;style type="text/css"&#62; div.redSubHead { color: [...]]]></description>
			<content:encoded><![CDATA[<p>The web community is generally coming (or has come) to accept that &#8216;semantic&#8217; class naming conventions (&#8216;old school&#8217; elements with classes describing content not appearance) are preferable to the <code>'boldGreyLink'</code> type conventions popular around the millennium.	</p>
<table cellpadding="0" cellspacing="10" border="0">
<tr>
<th style="padding:5px;">Millenium era HTML &amp; CSS</th>
<th style="padding:5px;">&#8216;Semantic&#8217; era HTML &amp; CSS</th>
</tr>
<tr>
<td style="position:relative; width:50%;padding:5px;" valign="top">
<pre style="border:solid 1px #eee;padding:10px; background:#f8f8f8;line-height:1.2em;">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;'Millenium' era&lt;/title&gt;
&lt;style type="text/css"&gt;
<span style="color:#c00;">div.redSubHead</span> {
  color: red;
}
<span style="color:#c00;">p.greyText</span> {
  color: #666;
}
<span style="color:#c00;">td.columnHeaderCell</span> {
  font-weight: bold;
}
<span style="color:#c00;">td.oddRowCell</span> {
  background: #eee;
}
<span style="color:#c00;">td.evenRowCell</span> {
  background: #ddd;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

  <span style="color:#c00;">&lt;div class="redSub"&gt;</span>sub 1&lt;<span style="color:#c00;">/div&gt;</span>
  <span style="color:#c00;">&lt;p class="greyText"&gt;</span>blah&lt;<span style="color:#c00;">/p&gt;</span>
  &lt;table&gt;

    &lt;tr&gt;
      <span style="color:#c00;">&lt;td class="colHeader"&gt;</span>A<span style="color:#c00;">&lt;/td&gt;</span>
      <span style="color:#c00;">&lt;td class="colHeader"&gt;</span>B<span style="color:#c00;">&lt;/td&gt;</span>
    &lt;/tr&gt;

    &lt;tr&gt;
      &lt;td <span style="color:#c00;">class="oddRow"</span>&gt;1&lt;/td&gt;
      &lt;td <span style="color:#c00;">class="oddRow"</span>&gt;2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td <span style="color:#c00;">class="evenRow"</span>&gt;3&lt;/td&gt;
      &lt;td <span style="color:#c00;">class="evenRow"</span>&gt;4&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/table&gt;

&lt;/body&gt;
&lt;/html&gt;</pre>
</td>
<td valign="top" style="width:50%;padding:5px;">
<pre style="border:solid 1px #eee;padding:10px; background:#f8f8f8;line-height:1.2em;">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;'Semantic' era&lt;/title&gt;
&lt;style type="text/css"&gt;
<span style="color:#0a0;">.section h2</span> {
  color: red;
}
<span style="color:#0a0;">.section p</span> {
  color: #666;
}
<span style="color:#0a0;">.section thead th</span> {
  font-weight: bold;
}
<span style="color:#0a0;">.section td</span> {
  background: #ddd;
}
<span style="color:#0a0;">.section tr.odd td</span>  {
  background: #eee;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;
  <span style="color:#0a0;">&lt;div class="section"&gt;</span>
    <span style="color:#0a0;">&lt;h2&gt;</span>sub 1&lt;<span style="color:#0a0;">/h2&gt;</span>
    <span style="color:#0a0;">&lt;p&gt;</span>blah&lt;<span style="color:#0a0;">/p&gt;</span>
    &lt;table&gt;
      <span style="color:#0c0;">&lt;thead&gt;</span>
        &lt;tr&gt;
          <span style="color:#0a0;">&lt;th&gt;</span>A<span style="color:#0a0;">&lt;/th&gt;</span>
          <span style="color:#0a0;">&lt;th&gt;</span>B<span style="color:#0a0;">&lt;/th&gt;</span>
        &lt;/tr&gt;
      <span style="color:#0a0;">&lt;/thead&gt;</span>
      &lt;tr <span style="color:#0a0;">class="odd"</span>&gt;
        &lt;td&gt;1&lt;/td&gt;
        &lt;td&gt;2&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
        &lt;td&gt;3&lt;/td&gt;
        &lt;td&gt;4&lt;/td&gt;
      &lt;/tr&gt;
    &lt;/table&gt;
  <span style="color:#0a0;">&lt;/div&gt;</span>
&lt;/body&gt;
&lt;/html&gt;</pre>
</td>
</tr>
<tr>
<td valign="top" style="padding:5px;">
<ol style="color:#900;">
<li>document structure inferred</li>
<li>classes describe appearance (volatile)</li>
<li>direct class application bloats html</li>
<li>changing style properties causes headaches (change all instance names or they&#8217;ll be out of sync with appearance).
			</ol>
</td>
<td valign="top" style="padding:5px;">
<ol style="color:#090;">
<li>document structure inherent</li>
<li>classes describe content (stable)</li>
<li>&#8216;container&#8217; classes give context to descendants</li>
<li>appearance (volatile) is abstracted from structure (stable). Redesign is anticipated &amp; therefore easier</li>
</ol>
</td>
</tr>
</table>
<p>One pillar of the &#8216;semantic&#8217; technique is the sparing application of <code>class</code> attributes, primarily to container elements. Elements are &#8216;caught&#8217; using CSS <a href="http://www.w3.org/TR/CSS2/selector.html#descendant-selectors">&#8216;descendant&#8217; selectors</a> (multi-part) which target hierarchies rather than using simple <a href="http://www.w3.org/TR/CSS2/selector.html#class-html">class selectors</a> which require a directly applied <code>class</code> attribute. Using descendant selectors respects the hierarchical nature of the <abbr title="Document Object Model">DOM</abbr> but makes writing bulk CSS quite tricky. Why? Mainly because CSS stylesheets (and style blocks) are fundamentally rule <b>lists</b>, but the DOM elements which our descendent selectors target are obviously <b>trees</b>. How do we bridge the gap between these fundamentally different structures?</p>
<h4>Enter &#8216;ragged&#8217; CSS</h4>
<p>In 2006 I (then a contracting developer) found myself filling the shoes of a fantastic design contractor whose contract was up but sadly not extended (project managers eternally underfund and misunderstand UI). I was the new &#8216;CSS guy&#8217; for a project with scores of developers, hundreds of pages and crippling compatibility/accessibility requirements. I wasn&#8217;t really qualified for the task, but my predecessor gave me a CSS tip that got me through: &#8220;don&#8217;t sweat the properties, focus on rule hierarchies&#8221;.</p>
<p>This rule-centric approach is most clearly evident in the CSS formatting style I picked up on that project and used ever since; a style I call &#8216;<b>Ragged CSS</b>&#8216;.</p>
<table cellpadding="0" cellspacing="10" border="0">
<tr>
<th style="padding:5px;">&#8216;tall&#8217; CSS</th>
<th style="padding:5px;">&#8216;ragged&#8217; CSS</th>
</tr>
<tr>
<td valign="top" style="padding:5px;">
<pre style="border:solid 1px #eee;padding:10px; background:#f8f8f8;line-height:1.2em;">
.section h2 {
  color: red;
}
.section p {
  color: #666;
}
.section thead th {
  font-weight: bold;
}
.section td {
  background: #ddd;
}
.section tr.odd td  {
  background: #eee;
}</pre>
</td>
<td valign="top" style="padding:5px;">
<pre style="border:solid 1px #eee;padding:10px; background:#f8f8f8;line-height:1.2em;">
.section { }
  .section h2 { color: red; }
  .section p { color: #666; }
  .section table { }
    .section thead th { font-weight: bold; }
    .section td { background: #ddd; }
      .section tr.odd td { background: #eee; }</pre>
</td>
</tr>
</table>
<p>It differs from the common &#8216;tall&#8217; CSS formatting style in only a few (primarily cosmetic) ways:</p>
<ol>
<li><b style="display:block;">One line per rule, one rule per line</b>There may be hundreds of rules, but each will only have a handful of properties.
	</li>
<li>
		<b style="display:block;">Indent CSS rules to represent target element hierarchy</b>A block of markup (e.g. a calendar) will typically involve several rules (perhaps dozens). Indenting rules more clearly indicates how their properties will cascade.</p>
<li>
		<b style="display:block;">Make rules for &#8216;landmark&#8217; elements (empty rules if necessary)</b>Adding an empty rule for element&#8217;s whose classes are used in subsequent selectors makes indentation hierarchy far clearer (improves readability).
	</li>
<li>
		<b style="display:block;">&#8216;namespace&#8217; your rules to avoid conflict</b>Class names may be repeated on large projects unintentionally. Target &#8216;abc&#8217; class elements inside &#8216;xyz&#8217; class elements using a &#8216;.xyz .abc&#8217; selector rather than a global &#8216;.abc&#8217; selector to avoid tainting someone else&#8217;s unrelated &#8216;abc&#8217; class.
	</li>
<li>
		<b style="display:block;">Avoid element prefixes sparingly in your selectors</b>Don&#8217;t use a <code>'span.content'</code> selector where a plain <code>'.content'</code> selector will do. Dogmatic prefixing (i.e. increasing <a href="http://www.smashingmagazine.com/2007/07/27/css-specificity-things-you-should-know/">specificity</a> unnecessarily) makes your rules unnecessarily vulnerable to otherwise inconsequential markup changes (changing the <code>span</code> to a <code>div</code> for instance).
	</li>
</ol>
<p>I&#8217;ve been using this formatting style for a few years now and it&#8217;s made complex CSS development a snap (browser quirks aside). If you&#8217;re writing &#8216;tall&#8217; CSS (or using a tool which generates it), give ragged CSS a go on your next project. Your future self (and/or your successor) will thank you for it!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jaysenmarais.com/blog/20090126/ragged-css-makes-life-easier/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

