<?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>CSS Wizardry &#187; Web Development</title>
	<atom:link href="http://csswizardry.com/category/web-development/feed/" rel="self" type="application/rss+xml" />
	<link>http://csswizardry.com</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Wed, 16 May 2012 12:57:59 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Keep your CSS selectors short</title>
		<link>http://csswizardry.com/2012/05/keep-your-css-selectors-short/</link>
		<comments>http://csswizardry.com/2012/05/keep-your-css-selectors-short/#comments</comments>
		<pubDate>Tue, 15 May 2012 19:35:38 +0000</pubDate>
		<dc:creator>Harry Roberts</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[CSS Selectors]]></category>
		<category><![CDATA[OOCSS]]></category>
		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://csswizardry.com/?p=3649</guid>
		<description><![CDATA[One thing I believe, as a very, very general rule of thumb, is that as sites get bigger, selectors should get shorter.
By this I mean that if you want to create extensible and maintainable, flexible and predictable websites, you should really take care to make your CSS selectors as dev-friendly as possible; i.e. short.
Keeping CSS [...]]]></description>
			<content:encoded><![CDATA[<p>One thing I believe, as a very, <em>very</em> general rule of thumb, is that as sites get bigger, selectors should get shorter.</p>
<p>By this I mean that if you want to create extensible and maintainable, flexible and predictable websites, you should really take care to make your CSS selectors as dev-friendly as possible; i.e. short.</p>
<p>Keeping CSS selectors short helps with a lot of things:</p>
<ul>
<li>Increases selector efficiency</li>
<li>Reduces location dependency</li>
<li>Increases portability</li>
<li>Reduces chances of selector breakage</li>
<li>Decreases specificity</li>
<li>Can make code more forgiving</li>
</ul>
<p>This is a very vague list, so I’m going to address each in order. You will find that there is a lot of crossover between each point (e.g. reducing location dependency inherently means your selectors are more portable) but I feel they are all points in their own right.</p>
<h2>Increases selector efficiency</h2>
<p>I have written before about CSS selector efficiency. I&#8217;m going to gloss over a lot of the intricacies in this post so for a full background understanding I recommend you read <a href="http://csswizardry.com/2011/09/writing-efficient-css-selectors/"><cite>Writing efficient CSS selectors</cite></a> first.</p>
<p>If we ignore actual types of selector (<code>*{}</code> is typically the slowest, depending on how it&#8217;s being used, IDs are the fastest followed by classes, descendants are comparably quite slow followed by pseudo-selectors) then <em>in general</em> it is safe to say that shorter selectors are faster.</p>
<p>This stands to reason, if we compare these two selectors:</p>
<pre><code>html body .header .nav{}
.nav{}</code></pre>
<p>There we can see pretty clearly that in the first example, the browser has to look out for four things, the <code>.nav</code> class, then the <code>.header</code> class, then the <code>body</code> element and then, finally, the <code>html</code> element (browsers read selectors right-to-left).</p>
<p>With the second example the browser only needs to look for one thing; the <code>.nav</code> class. The browser has <em>four times</em> less work to do to match that selector. Every time you write a selector try and trim as much losable stuff from it as possible. Instead of <code>ul.nav{}</code> (two checks) write <code>.nav{}</code> (one check). Instead of <code>.nav li a{}</code> (three) write <code>.nav a{}</code> (two).</p>
<p>Now, <a href="http://calendar.perfplanet.com/2011/css-selector-performance-has-changed-for-the-better/">CSS selector performance is—by-and-large—not something we <em>really</em> need to worry about any more</a>, but that doesn&#8217;t mean we should be wasteful. I&#8217;m sure none of us would miss a lost &pound;5 but that doesn&#8217;t mean we go slipping banknotes into paper shredders… Selector efficiency <em>does</em> exist and you might as well improve it where you <strong>very easily</strong> can.</p>
<h2>Reduces location dependency</h2>
<p>By keeping selectors short you are likely to be reducing the amount of descendant (e.g. <code>.sidebar .promo{}</code>) and child (e.g. <code>.sidebar &gt; .promo{}</code>) selectors. By removing these descending types of selectors you are reducing the necessity for an element to live inside another one. Let&#8217;s reuse the <code>.sidebar .promo{}</code> example…</p>
<p>By having a selector like <code>.sidebar .promo{}</code> we are saying we want to target any promotional item that lives in an element with the class of <code>.sidebar</code>. This means that we are tied to always using that styling inside a certain element; we have a dependency on location.</p>
<p>By replacing <code>.sidebar .promo{}</code> with something like <code>.secondary-promo{}</code> we can now place the element in question <em>anywhere</em> we wish. In the sidebar—as before—but now also in the footer, or in the header, or after an article.</p>
<p>By reducing descendants we can really reduce dependency and make things a lot more portable…</p>
<h2>Increases portability</h2>
<p>So now that we&#8217;re not tied to locationally dependant selectors, we find that our components are a lot more portable. We can move things a lot more easily because our CSS doesn&#8217;t care where a thing lives, it just cares that it exists. Awesome!</p>
<p>Another way to increase portability is to not qualify selectors. A qualified selector is one like <code>ul.nav{}</code> or <code>a.button{}</code> or <code>div.content{}</code>.</p>
<p>Qualified selectors are bad because they reduce efficiency (more checks than we really need) but also because they tie us to specific elements. We can&#8217;t now use that <code>.button</code> class on an <code>&lt;input&gt;</code> or a <code>&lt;button&gt;</code>, for example. We can&#8217;t <a href="http://csswizardry.com/2011/09/the-nav-abstraction/">apply <code>.nav</code> to an <code>&lt;ol&gt;</code> to make a breadcrumb</a>.</p>
<p><strong>Selectors should be element-agnostic</strong>. Your CSS shouldn&#8217;t care what element you&#8217;re wanting to apply styling to.</p>
<p>Another way to make selectors more portable is to drop elements <em>altogether</em>. Take this, for example:</p>
<pre><code><span class=code-comment>/* Base widget styling */</span>
.widget{}

<span class=code-comment>/* Style up widget titles */</span>
.widget &gt; h2{}</code></pre>
<p>Here we have a troublesome selector; what if that <code>&lt;h2&gt;</code> needs to become a <code>&lt;h3&gt;</code>? What if we need to add another, non-titling <code>&lt;h2&gt;</code> as a child of <code>.widget</code>? We&#8217;ve made ourselves a very rigid and unportable selector here. Instead we should have:</p>
<pre><code><span class=code-comment>/* Base widget styling */</span>
.widget{}

<span class=code-comment>/* Style up widget titles */</span>
.widget-title{}</code></pre>
<p>Now we can apply <code>.widget-title</code> to <em>any</em> element—let&#8217;s say a <code>&lt;h4&gt;</code>—and can now also have any number of unclassed <code>&lt;h4&gt;</code>s in the widget without them adopting any title styling. Ossom!</p>
<h2>Reduces chances of selector breakage</h2>
<p>The longer a selector is, the more things the browser has to satisfy before it can match it. The more checks there are then—naturally—the more chance there is for something to go wrong.</p>
<p>A (very exaggerated) selector like <code>body &gt; div:nth-of-type(2) &gt; article:first-child &gt; p:first-child{}</code>—borrowed from my talk <a href="https://speakerdeck.com/u/csswizardry/p/breaking-good-habits?slide=15"><cite>Breaking Good Habits</cite></a>—has <em>ten</em> checks; ten things that must be satisfied in order for the browser to make that match.</p>
<p>All that needs to happen is the location of the <code>div:nth-of-type(2)</code> to change or the <code>p:first-child</code> to become a <code>blockquote</code> or the <code>article:first-child</code> to no longer be a child of the <code>div:nth-of-type(2)</code> or <em>any manner</em> of things before that selector will break. Simply replacing that with a class of <code>.intro{}</code> means that there is only one thing that could possibly break, and the chances of that happening are pretty much zero (you&#8217;d have to explicitly delete the class from your HTML to prevent a match).</p>
<p>Shorter selectors mean there is statistically less chance for things to go wrong.</p>
<h2>Decreases specificity</h2>
<p>This is the big one! This is where it really matters!</p>
<p>Longer selectors have a higher specificity. Specificity is a nightmare and <strong>you should keep specificity as low as possible all of the time</strong>. We already know that we <a href="http://csswizardry.com/2011/09/when-using-ids-can-be-a-pain-in-the-class/"><strong>do not use IDs in CSS</strong></a> but a chain of selectors are often just as bad (though not quite).</p>
<p>A selector like <code>.widget &gt; h2{}</code> has a higher specificity (as well as the other problems we discussed) than a selector like <code>.widget-title{}</code>.</p>
<p><code>.nav li a{}</code> has a higher specificity than <code>.nav a</code> (and is also less efficient). Reducing selector length reduces selector specificity and that is <strong>very important</strong>. High specificity leads to self-induced specificity battles that can only be won by making subsequent selectors <em>more</em> specific (or using <code>!important</code>, shame on you). This is a terrible thing. The easiest way to reduce specificity (after <em>dropping IDs from your CSS <strong>completely</strong></em>) is to keep your selectors short.</p>
<h2>Can make code more forgiving</h2>
<p>This is a very specific but very decent example of how short selectors can make code more forgiving. However, I will warn you, you can argue two sides of what I&#8217;m about to tell you; you can argue that it makes your code a lot more flexible and can gracefully handle breakages <strong>or</strong> you could argue that it allows breakages in the first place by being too lenient. Anyway, here&#8217;s a true story…</p>
<p>In working on a pretty huge project at Sky I stuck to my own rules and coded a (vertical) nav bar CSS like so:</p>
<pre><code>.nav{ <span class=code-comment>/* Nav styles */</span> }

<span class=code-comment>/* Note NO .nav li styles as this was a vertically stacking nav. */</span>

.nav a{ display:block; <span class=code-comment>/* More styles */</span> }</code></pre>
<p>Now, there was a CMS error which went undetected where the markup getting spat out was:</p>
<pre><code>&lt;ul class=nav&gt;
    &lt;a href=#&gt;&lt;/a&gt;
    &lt;a href=#&gt;&lt;/a&gt;
    &lt;a href=#&gt;&lt;/a&gt;
    &lt;a href=#&gt;&lt;/a&gt;
&lt;/ul&gt;</code></pre>
<p>Spot the problem? No <code>&lt;li&gt;</code>s! This is <em>really</em> not cool <strong>but</strong>, as I had used <code>.nav a{}</code> instead of <code>.nav li a{}</code> nothing broke. My code was a lot more forgiving than if I&#8217;d had that third check in there.</p>
<p>Now, this doesn&#8217;t make the markup right, and it does actually <em>allow</em> poorer markup than a more verbose selector, but you can see how the CSS was very forgiving of a markup error.</p>
<p>Now I said you could argue both sides here, a more verbose selector means that we&#8217;d have spotted the CMS error immediately as no styles would have hit the <code>&lt;a&gt;</code>s. But! In the same breath, our CSS was flexible enough to be okay with that. Make of it what you will, because I too am sat on the fence and a little disappointed that the error wasn&#8217;t spotted, but here is a very specific example of how shorter selectors can lead to more forgiving CSS.</p>
<h2>Final word</h2>
<p>I did mention that this is a rule I&#8217;ve applied to larger sites but, honestly, you should apply this everywhere. The things we&#8217;ve discussed tend to really come into their own (and their absence painfully aware) on larger builds, but they will definitely, <em>definitely</em> help you on builds of all sizes; small or large.</p>
<p>So, by using more classes and less descendants, keeping selectors short and portable, keeping selectors element-agnostic and generally considering maintenance and chance-of-change when writing our CSS, we can really easily improve the quality of our code infinitely. We can make things more efficient, more forgiving, more flexible and more reusable just by revisiting one of the most simple and fundamental aspects of CSS; our selectors.</p>
]]></content:encoded>
			<wfw:commentRss>http://csswizardry.com/2012/05/keep-your-css-selectors-short/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>The single responsibility principle applied to CSS</title>
		<link>http://csswizardry.com/2012/04/the-single-responsibility-principle-applied-to-css/</link>
		<comments>http://csswizardry.com/2012/04/the-single-responsibility-principle-applied-to-css/#comments</comments>
		<pubDate>Sat, 28 Apr 2012 22:57:37 +0000</pubDate>
		<dc:creator>Harry Roberts</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Abstraction]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[OOCSS]]></category>

		<guid isPermaLink="false">http://csswizardry.com/?p=3614</guid>
		<description><![CDATA[Having just spoken at the Front-Trends conference in Warsaw, I’ve decided to expand on something which my talk mentioned a lot: classes.
My talk covered how to build big, scalable front-ends and one of the key factors involved in doing so is sensible and generous use of abstracted classes. One thing that really helps you achieve [...]]]></description>
			<content:encoded><![CDATA[<p>Having just spoken at <a href="http://2012.front-trends.com/">the Front-Trends conference in Warsaw</a>, I’ve decided to expand on something which my talk mentioned a lot: classes.</p>
<p>My talk covered how to build big, scalable front-ends and one of the key factors involved in doing so is sensible and generous use of abstracted classes. One thing that really helps you achieve this is the application of the <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">single responsibility principle</a>, a method used mainly in OO programming.</p>
<p><span id="more-3614"></span></p>
<p>Loosely, the single responsibility principle states that every module or chunk of code (a function etc) should do one job well and one job only. The benefits of this are mainly in the way of maintainability and extensibility.</p>
<p>If we don’t adhere to the SRP then we are likely to end up with code which does more than it should, this means that altering one part of that code could negatively impact a seemingly unrelated part of the same snippet. It also makes our code a lot less flexible in that we find our code is trying to do too much; it is too specific in its job to be portable and reusable. Abstracting chunks of functionality into several responsibilities means we can reuse a lot more of our code and recombine it over and over with other similarly abstracted chunks.</p>
<p>The <a href="http://en.wikipedia.org/wiki/Single_responsibility_principle">Wikipedia article</a> makes a much better job of explaining than I can so I would recommend giving that a quick read (it’s only short).</p>
<p>Also, you will find that a lot of us do this already, and perhaps without even realising. A class of <code>.wrapper</code> is a good example of the SRP at play; by having a single, reusable class whose sole job it is to group content we make our code a lot easier to work with. We don’t need to apply lots of widths to lots of elements; we use one extra <code>div</code> and delegate the width-constraining responsibility to that.</p>
<p>We do this because it makes sense, but we don’t do it often enough; if we start actually thinking like this all the time we find we can vastly improve our code&#8230;</p>
<p>The SRP is normally applied in programming circles but I have definitely found it is <em>incredibly</em> useful when making lean, scalable front-ends. Here’s a quick example of un-abstracted code that <strong>doesn’t</strong> follow the SRP:</p>
<pre><code><span class=code-comment>&lt;!-- HTML --&gt;</span>
&lt;a href=/product class=<mark>promo</mark>&gt;Buy now!&lt;/a&gt;

<span class=code-comment>/* CSS */</span>
.promo{
    display:block;
    padding:20px;
    margin-bottom:20px;
    background-color:#09f;
    color:#fff;
    tex-shadow:0 0 1px rgba(0,0,0,0.25);
    border-radius:4px;
}</code></pre>
<p>Here we have a class for a promotional box of content. Here we are doing <strong>two</strong> things—we are defining box model and structure <em>and</em> we are defining cosmetics (colouring etc).</p>
<p>We can refactor this code to adhere to the SRP by splitting those two chunks of functionality into two classes:</p>
<pre><code> <span class=code-comment>&lt;!-- HTML --&gt;</span>
&lt;a href=product class="<mark>island</mark> promo"&gt;Buy now!&lt;/a&gt;

<span class=code-comment>/* CSS */</span>
.island{
    display:block;
    padding:20px;
    margin-bottom:20px;
}

.promo{
    background-color:#09f;
    color:#fff;
    tex-shadow:0 0 1px rgba(0,0,0,0.25);
    border-radius:4px;
}</code></pre>
<p>We now have two classes which each carry a single responsibility; <code>.island</code> boxes off content and <code>.promo</code> applies our promotional styling. This now means that we can do things like this, which previously we couldn’t:</p>
<pre><code> <span class=code-comment>&lt;!-- HTML --&gt;</span>
&lt;h2&gt;Buy now with promo code: &lt;span class=<mark>promo</mark>&gt;0MG4WE50ME&lt;/span&gt;&lt;/h2&gt;</code></pre>
<p>Previously we couldn’t have managed this as the <code>.promo</code> class also carried a lot of box model; by abstracting our code into single responsibilities we can pick and choose what we want to use and where a lot more easily.</p>
<p>We can take this much, much further; now we also have a generic class for boxing off content! Where we once might have had:</p>
<pre><code><span class=code-comment>&lt;!-- HTML --&gt;</span>
&lt;div id=header&gt;
&lt;/div&gt;

&lt;div id=content&gt;
&lt;/div&gt;

&lt;div id=sub-content&gt;
&lt;/div&gt;

&lt;div id=footer&gt;
&lt;/div&gt;

<span class=code-comment>/* CSS */</span>
#header{
    padding:20px;
    margin-bottom:20px;
    background-color:#121416;
    color:#fff;
}

#content{
    width:640px;
    float:left;
    margin-right:20px;
    padding:20px;
    margin-bottom:20px;
}

#sub-content{
    width:280px;
    float:left;
    padding:20px;
    margin-bottom:20px;
}

#footer{
    padding:20px;
    margin-bottom:20px;
    background-color:#e4e4e4;
    color:#333;
}</code></pre>
<p>We would now have:</p>
<pre><code><span class=code-comment>&lt;!-- HTML --&gt;</span>
&lt;div class="<mark>island</mark> header"&gt;
&lt;/div&gt;

&lt;div class="<mark>island</mark> content"&gt;

    &lt;h2&gt;Buy now with promo code &lt;span class=<mark>promo</mark>&gt;0MG4WE50ME&lt;/span&gt;&lt;/h2&gt;

&lt;/div&gt;

&lt;div class="<mark>island</mark> sub-content"&gt;

    &lt;a href=product class="<mark>island</mark> <mark>promo</mark>"&gt;Buy now!&lt;/a&gt;

&lt;/div&gt;

&lt;div class="<mark>island</mark> footer"&gt;
&lt;/div&gt;

<span class=code-comment>/* CSS */</span>
.island{
    display:block;
    padding:20px;
    margin-bottom:20px;
}

.promo{
    background-color:#09f;
    color:#fff;
    tex-shadow:0 0 1px rgba(0,0,0,0.25);
    border-radius:4px;
}

.header{
    background-color:#121416;
    color:#fff;
}

.content{
    width:640px;
    float:left;
    margin-right:20px;
}

.sub-content{
    width:280px;
    float:left;
}

.footer{
    background-color:#e4e4e4;
    color:#333;
}</code></pre>
<p>Because we abstracted our code out we now have a really portable class for simply boxing off content!</p>
<p><strong>N.B.</strong> You should also abstract your layout to a grid system, taking the floats and widths off the page components and leaving that responsibility to the grids.</p>
<p>Just some of the benefits of working like this are:</p>
<ol>
<li>Your CSS is a lot DRYer.</li>
<li>You can make far-reaching changes to your designs by simply modifying a base abstraction only once.</li>
<li>You can make safer changes because you know that when editing a class you are only ever altering <em>one</em> responsibility.</li>
<li>You can combine responsibilities to make a variety of components from a lot of abstracted classes.</li>
</ol>
<p>Now we can take it further <em>still</em>.</p>
<p>Because we have a nice <code>.island</code> class whose sole responsibility is to box off content then we can now do things like this:</p>
<pre><code><span class=code-comment>&lt;!-- HTML --&gt;</span>
&lt;div class="island content"&gt;
    ...
    &lt;form&gt;
        &lt;p class="island <mark>error</mark>"&gt;Please provide your name.&lt;/p&gt;
        ...
        &lt;label class=<mark>error</mark>&gt;Name:&lt;/label&gt;
        ...
    &lt;/form&gt;
    ...
&lt;/div&gt;

<span class=code-comment>/* CSS */</span>
.island{
    display:block;
    padding:20px;
    margin-bottom:20px;
}

.error{
    background-color:#fce0e2;
    color:#c00;
}
.error.island{
    border:5px solid #c00;
    border-radius:4px;
}</code></pre>
<p>Because we have our chunks of CSS only working on one thing at a time then we can reuse and combine useful things time and time again. I’ve written about this before: <a href="https://plus.google.com/u/0/110483125936065828120/posts/DGPQzUdPi84">Class based builds are like eating at Subway</a>.</p>
<h2>When to use it?</h2>
<p>There’s no definite answer to questions like this but as a general rule try and stick to the SRP any time you think that subsets of a style rule could be split out into more manageable and <strong>reusable</strong> abstractions. Any time you are coding a potentially repeatable design pattern then try and split it out as per the single responsibility principle.</p>
<h3>But!</h3>
<p>It’s important not to take this too far; classes should be abstracted but ideally not presentational. Classes like <code>.round-corners</code> for the sake of SRP are really not all that advisable.</p>
]]></content:encoded>
			<wfw:commentRss>http://csswizardry.com/2012/04/the-single-responsibility-principle-applied-to-css/feed/</wfw:commentRss>
		<slash:comments>26</slash:comments>
		</item>
		<item>
		<title>My HTML/CSS coding style</title>
		<link>http://csswizardry.com/2012/04/my-html-css-coding-style/</link>
		<comments>http://csswizardry.com/2012/04/my-html-css-coding-style/#comments</comments>
		<pubDate>Fri, 20 Apr 2012 18:58:53 +0000</pubDate>
		<dc:creator>Harry Roberts</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://csswizardry.com/?p=3561</guid>
		<description><![CDATA[Whenever I post jsFiddles, tutorials, articles or do some work here at Sky, I’m very particular about my coding style and am often asked questions about it. A lot of people say ‘code is art’; to me that sentence is a little pretentious but what I do think is that code can be a little [...]]]></description>
			<content:encoded><![CDATA[<p>Whenever I post jsFiddles, tutorials, articles or do some work here at Sky, I’m very particular about my coding style and am often asked questions about it. A lot of people say ‘code is art’; to me that sentence is a little pretentious but what I <em>do</em> think is that code can be a little more quirky and original than most people write it. It can be interesting. <a href="https://twitter.com/mathias">Mathias Bynens</a> is constantly tweeting crazy little snippets of quirky, valid JS; he’s also made <a href="http://mothereff.in">some great little tools</a> to help you explore the different avenues available to you when writing awesome, expressive and ‘different’ code.</p>
<p><span id="more-3561"></span></p>
<p>When I write HTML and CSS I have a certain set of rules and guidelines I stick to with regards syntax and formatting. I‘ve decided to write them up here <strong>not</strong> to preach or tell you how to do things, but simply to share how <em>I</em> like to work. If you hate the things I do that’s cool, these are not recommendations. If you like anything I do then great; you can have it. What I <em>would</em> love to see is other people doing the same; please consider writing up your coding styles, nuances and preferences and share them about!</p>
<hr />
<h2>HTML</h2>
<p>With HTML5 we have a lot looser syntax which means more scope for experimentation than we were typically used to with XHTML. With things like <code>&lt;li&gt;</code>s not requiring closing, some attributes not requiring quotes, tags not needing to be lowercase and elements like <code>&lt;hr /&gt;</code>s not needing to be self-closing, there’s a lot to play with! Here’s what I do with my HTML&#8230;</p>
<h3>Unquoted attributes</h3>
<p>Where I don’t need to, I don’t quote attributes. Where I would have had <code>class="wrapper"</code> I now have <code>class=wrapper</code>. Why? Because I can and I like the look of it!</p>
<p>Obviously, sometimes you do need to quote attributes. If you have multiple classes like <code>class=wrapper header-wrapper</code> then you <em>need</em> quotes. Without quotes the browser sees this as: <code>class="wrapper" header-wrapper=""</code>.</p>
<p>To know when you can and when you can’t use quotes then have a tinker with Mathias’ <a href="http://mothereff.in/unquoted-attributes"><cite>Mothereffing unquoted attribute value validator</cite></a>.</p>
<p>There are instances where you <em>could</em> use unquoted attributes but I choose not to. It’s hard to summarise this in a quick rule so I’ll give an example:</p>
<pre><code>&lt;input type=submit class=btn value=Login&gt;</code></pre>
<p>This is perfectly valid usage of unquoted attributes, however, imagine the client calls up the agency and (very rightly) says <q>Hey, the word <i>Login</i> needs to be <i>Log in</i>, can we change that please?</q> The project manager knows the designer is totally flat out busy and doesn&#8217;t have the time for smaller amends at the moment. Because project managers are awesome lovely people (right? Right?!), they decide that they can make this simple change themselves to save the designer a job. They open up the relevant file, see <code>value=Login</code> and simply change it to <code>value=Log in</code>, assuming that’s all there is to it. Now, the PM is likely not a technical person so they won’t necessarily know that this will now need quoting; the designer would have.</p>
<p>So, anywhere where the content of the attribute is subject to arbitrary change, I like to quote the attributes as a backup. Also, any time an attribute is populated dynamically (for example, through a CMS) it is best to quote it just in case; if you aren’t manually and consciously altering the attribute, play it safe and pop some quotes round it&#8230;</p>
<p>Now, I know what you’re thinking: <q>If you sometimes have to use quotes then why not always use quotes? It’s more consistent.</q> Well, you’re half right. It is more consistent on the whole, but by introducing rules I make my own consistency; if you can get away with not using quotes, then do so.</p>
<h3>No self closing tags</h3>
<p>With HTML5 we can write things like <code>&lt;hr /&gt;</code> as <code>&lt;hr&gt;</code>, <code>&lt;img /&gt;</code> as <code>&lt;img&gt;</code> and so on. I choose to omit the <code>/&gt;</code>, so in my HTML you’ll see things like:</p>
<pre><code>&lt;meta name=viewport content="width=device-width, minimum-scale=1.0, maximum-scale=1.0"&gt;
&lt;link rel=stylesheet href=css/style.css&gt;</code></pre>
<p>Unquoted attributes where I can, and no self closing tags.</p>
<h3>Optional closing tags</h3>
<p>In HTML5 (or non-XHTML syntax) you can omit actual, full closing tags from <a href="http://meiert.com/en/blog/20080601/optional-tags-in-html-4/">certain elements</a>! This means that the following is totally valid:</p>
<pre><code>&lt;ul&gt;
    &lt;li&gt;Lorem
    &lt;li&gt;Ipsum
    &lt;li&gt;Dolor
    &lt;li&gt;Sit
    &lt;li&gt;Amet
&lt;/ul&gt;</code></pre>
<p>I <em>don’t</em> write my HTML like this as I’m just not too keen on the look of it&#8230; If you want to though, you certainly can.</p>
<h3>Whitespace</h3>
<p>This one is hard to quantify, but I like to use whitespace to loosely reflect the separation of elements that you might see once rendered. I group and space elements with whitespace as you would expect them to be grouped visually in the rendered page, for example:</p>
<pre><code>&lt;dl&gt;

    &lt;dt&gt;Lorem&lt;/dt&gt;
    &lt;dd&gt;Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.&lt;/dt&gt;

    &lt;dt&gt;Ipsum&lt;/dt&gt;
    &lt;dd&gt;Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante.&lt;/dt&gt;

    &lt;dt&gt;Dolor&lt;/dt&gt;
    &lt;dd&gt;Morbi in sem quis dui placerat ornare. Pellentesque odio nisi euismod in pharetra.&lt;/dt&gt;

&lt;/dl&gt;
&nbsp;
&nbsp;
&lt;div class=promo&gt;

    &lt;p&gt;&lt;strong&gt;Pellentesque habitant morbi tristique&lt;/strong&gt; senectus et netus et malesuada fames ac turpis egestas.&lt;/p&gt;
    &lt;a href=# class=btn&gt;Lorem&lt;/a&gt;

&lt;/div&gt;</code></pre>
<p>A mixture of no, single and double lines of whitespace help to separate elements, or show their visual relationship to one another. For example, the <code>&lt;dt&gt;</code>s and <code>&lt;dd&gt;</code>s belong with each other, but are separate from other groupings.</p>
<h3>Comments on closing tags</h3>
<p>After every major chunk of HTML, for example, the end of a carousel, or the end of the content <code>&lt;div&gt;</code>, I place a closing-comment, for example:</p>
<pre><code>&lt;div class=content&gt;

    ...

    &lt;div class=carousel&gt;
    ...
    &lt;/div&gt;<span class=code-comment>&lt;!-- /carousel --&gt;</span>

    ...

&lt;/div&gt;<span class=code-comment>&lt;!-- /content --&gt;</span></code></pre>
<p>This is certainly not uncommon practice, but because all closing tags look the same as each other (they can’t carry the classes that the opening tags do) it’s often nice to leave a comment so you know what element you’re dealing with.</p>
<h3>Namespaced fragment identifiers</h3>
<p>I came up with <a href="http://csswizardry.com/2011/06/namespacing-fragment-identifiers/">this idea to namespace fragment identifiers</a> last year. It’s basically, I think, a nice way to add a little more meaning to your fragment identifiers and give a little bit more of a clue as to what they actually link to.</p>
<pre><code>&lt;a href=<mark>#section:fragment-identifiers</mark>&gt;Fragment identifiers&lt;/a&gt;

...

&lt;div id=<mark>section:fragment-identifiers</mark>&gt;...&lt;/div&gt;</code></pre>
<hr />
<h2>CSS</h2>
<p>So far I’ve dealt with how I write HTML, but what about CSS? My CSS is a lot less ‘different’, it’s mainly only formatting and syntax rules here.</p>
<h3>No IDs</h3>
<p>This is more a technical thing, but I have <a href="http://csswizardry.com/2011/09/when-using-ids-can-be-a-pain-in-the-class/">a blanket-ban on IDs in CSS</a>. There is literally no point in them, and they only ever cause harm. Everything that needs styling is done so without using IDs.</p>
<h3>Table of contents</h3>
<p>At the top of my CSS files I have a table of contents that maps to the section titles in the document, it looks a little like:</p>
<pre><code><span class=code-comment>/*------------------------------------*\
    CONTENTS
\*------------------------------------*/</span>
<span class=code-comment>/*
NOTES
RESET
SHARED     Share anything we can across elements.
MAIN       HTML, BODY, etc.
*/</span></code></pre>
<h3>Section titles</h3>
<p>I denote each section (layout, type, tables etc) of my CSS thus:</p>
<pre><code><span class=code-comment>/*------------------------------------*\
    $MAIN
\*------------------------------------*/</span></code></pre>
<p>This section heading is also prepended with a <code>$</code>. This is so that&mdash;when I do a find for a section&mdash;I actually do a find for <code>$MAIN</code> and not <code>MAIN</code>. This means that I’m only ever searching within section headings. A search for <code>$MAIN</code> will only ever find me a section with that name whereas a search for <code>MAIN</code> could find me something like:</p>
<pre><code>.s{
    background-image:url(/img/css/sprites/<mark>main</mark>.png);
}</code></pre>
<p>Being able to search just in the scope of headings is very, very useful.</p>
<p>I also leave five carriage returns between each section, for example:</p>
<pre><code><span class=code-comment>/*------------------------------------*\
    $MAIN
\*------------------------------------*/</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span class=code-comment>/*------------------------------------*\
    $TYPE
\*------------------------------------*/</span></code></pre>
<p>This means that when scrolling quickly through my stylesheet I know that any gaps in the code are likely to be new sections.</p>
<h3>The shared section</h3>
<p>I wrote about the shared section briefly on Smashing Magazine in my article <a href=http://coding.smashingmagazine.com/2011/08/26/writing-css-for-others/><cite>Writing CSS for others</cite></a>. This is basically a section in a stylesheet where, instead of declaring one rule over and over, we define it once and attach selectors to it, for example:</p>
<pre><code>a,.brand,h1,h2,h3,h4,h5,h6{
    color:#BADA55;
}
h1,h2,h3,h4,h5,h6,
ul,ol,dl,
p,
table,
form,
pre,
hr{
    margin-bottom:24px;
    margin-bottom:1.5rem;
}</code></pre>
<p>Doing this means that things I want to use over and over are only written once, and I can update all instances in one place. It’s kinda like variables in vanilla CSS&#8230;</p>
<h3>Formatting</h3>
<p>I write multiline CSS with a distinct lack of whitespace that most people seem to hate:</p>
<pre><code>.wrapper{
    margin:0 auto;
    max-width:940px;
    padding:10px;
}</code></pre>
<p>No spaces before braces or after colons.</p>
<h3>Vendor prefixes</h3>
<p>I write vendor prefixes so that the values all line up vertically; this means I can scan them quicker (to check they&#8217;re all identical) and I can alter them all in one go if my editor supports typing in columns:</p>
<pre><code>.island{
    padding:1.5em;
    margin-bottom:1.5em;
    -webkit-border-radius:<mark>4px</mark>;
       -moz-border-radius:<mark>4px</mark>;
            border-radius:<mark>4px</mark>;
}</code></pre>
<h3>Indenting rulesets</h3>
<p>One thing I do like to do is indent my rulesets to mirror the nesting of their corresponding HTML. For example, take this carousel HTML:</p>
<pre><code>&lt;div class=carousel&gt;

    &lt;ul class=panes&gt;

        &lt;li class=pane&gt;

            &lt;h2 class=pane-title&gt;Lorem&lt;/h2&gt;

        &lt;/li&gt;<span class=code-comment>&lt;!-- /pane --&gt;</span>

        &lt;li class=pane&gt;

            &lt;h2 class=pane-title&gt;Ipsum&lt;/h2&gt;

        &lt;/li&gt;<span class=code-comment>&lt;!-- /pane --&gt;</span>

        &lt;li class=pane&gt;

            &lt;h2 class=pane-title&gt;Dolor&lt;/h2&gt;

        &lt;/li&gt;<span class=code-comment>&lt;!-- /pane --&gt;</span>

    &lt;/ul&gt;<span class=code-comment>&lt;!-- /panes --&gt;</span>

&lt;/div&gt;<span class=code-comment>&lt;!-- /carousel --&gt;</span></code></pre>
<p>My CSS for this would be formatted:</p>
<pre><code><span class=code-comment>/*------------------------------------*\
    $CAROUSEL
\*------------------------------------*/</span>
.carousel{
    [styles]
}

    .panes{
        [styles]
    }

        .pane{
            [styles]
        }

            .pane-title{
                [styles]
            }</code></pre>
<p>By doing this, I can see from the CSS roughly how the HTML should look; I don’t need the HTML in front of me in order to work out what lives in what.</p>
<h3>HTML in CSS</h3>
<p>In situations where it <em>would</em> be useful for a developer to know exactly how a chunk of CSS applies to some HTML, I often include a snippet of HTML in a CSS comment, for example:</p>
<pre><code><span class=code-comment>/*------------------------------------*\
    $TOOLTIPS
\*------------------------------------*/</span>
<span class=code-comment>/*
&lt;small class=tooltip&gt;&lt;span&gt;Lorem ipsum dolor&lt;/span&gt;&lt;/small&gt;
*/</span>
.tooltip{
    [styles]
}
    .tooltip &gt; span{
        [styles]
    }</code></pre>
<hr />
<h2>Final word</h2>
<p>For a loose example of the above, poke through <a href=https://github.com/csswizardry/vanilla><cite>vanilla</cite></a> or the <a href=http://hry.rbrts.me>hry.rbrts.me</a> source <a href=https://github.com/csswizardry/hry.rbrts.me>on GitHub</a>.</p>
<p>Just to reiterate, this is my coding <em>style</em>; I’m not making suggestions or laying down rules here. If you like anything then feel free to adopt it yourself, if you hate it then that’s cool (just hope you don’t inherit one of my projects!)</p>
<p>I’d really love if people wrote up and shared their own; there are loads of different ways of writing code and I’d be really interested to see what other people do. There’s a really good opportunity for learning some really neat tips!</p>
<p>If you do write your own, please tweet it at me <strong>with the hashtag <a href="https://twitter.com/#!/search/realtime/%23codestyle">#codestyle</a></strong>, that way everyone can easily keep track of any posts.</p>
<p><strong>Unquoted attributes, no self-closing tags, loads of whitespace, weird CSS indenting and a lot of comments; what&#8217;s yours?</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://csswizardry.com/2012/04/my-html-css-coding-style/feed/</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>Hacker News rebuttal</title>
		<link>http://csswizardry.com/2012/03/hacker-news-rebuttal/</link>
		<comments>http://csswizardry.com/2012/03/hacker-news-rebuttal/#comments</comments>
		<pubDate>Tue, 13 Mar 2012 01:29:12 +0000</pubDate>
		<dc:creator>Harry Roberts</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[GitHub]]></category>
		<category><![CDATA[Hacker News]]></category>
		<category><![CDATA[OOCSS]]></category>

		<guid isPermaLink="false">http://csswizardry.com/?p=3523</guid>
		<description><![CDATA[Yesterday I decided to post on Hacker News something I’ve recently been working on for both myself and Sky. That something was a list of guidelines as to how best write CSS for manageable and maintainable projects.
Unfortunately, a number of the HN community didn’t take so well to my points so I’ve decided to publish [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday <a href="http://news.ycombinator.com/item?id=3693610">I decided to post on Hacker News something I’ve recently been working on for both myself and Sky</a>. That something was a list of <a href="https://github.com/csswizardry/CSS-Guidelines/blob/master/CSS%20Guidelines.md">guidelines as to how best write CSS for manageable and maintainable projects</a>.</p>
<p>Unfortunately, a number of the HN community didn’t take so well to my points so I’ve decided to publish a full response, as opposed to the disjointed and frankly unprofessional comment discussion that was emerging.</p>
<p>One user in particular took exception to a lot of my advice and it is his issues I shall address most directly. This isn’t a vendetta at all, more my trying to answer his and others’ concerns at large. He offered a pretty 1:1 comeback to a lot of my points so I shall try to maintain a similar format here.</p>
<p>I may paraphrase a lot here so I urge you, <strong><a href="http://news.ycombinator.com/item?id=3693610">please, read the original thread</a></strong>. I apologise and regret and instances in which I come across as rude and/or unprofessional; I acted on impulse rather than rationally, as I should have done.</p>
<hr />
<p>I said:</p>
<blockquote>
<p>”For each level of markup nesting, try and indent your CSS to match.”</p>
</blockquote>
<p>He said:</p>
<blockquote>
<p>“Nope.”</p>
</blockquote>
<p>Indenting CSS to mirror your markup structure can be very useful for a number of reasons, chief among which is the ability to see <em>at a glance</em> how the listed CSS selectors should be used in HTML. For example, when building a carousel, I often find myself with CSS selectors like:</p>
<pre><code>.carousel{}
.panes{}
.pane{}
.slide-image{}
.slide-title{}
</code></pre>
<p>As these selectors are only one class deep it is difficult to see at a glance how they might map to my HTML. One solution might be to write my CSS like so:</p>
<pre><code>.carousel{}
.carousel .panes{}
.carousel .panes .pane{}
.carousel .panes .pane .slide-image{}
.carousel .panes .pane .slide-title{}
</code></pre>
<p>But of course you don’t need me to tell you how nasty that is…</p>
<p>Instead, I prefer a structure more like:</p>
<pre><code>.carousel{}
    .panes{}
        .pane{}
            .slide-image{}
            .slide-title{}
</code></pre>
<p>Here my selectors are still nice and short but I can still see how they map to my HTML structure. I find this really useful to see at a glance how these rules relate to one another.</p>
<p>If you don’t like this, or won’t find it useful, that’s cool! Just don’t use it.</p>
<hr />
<p>I said:</p>
<blockquote>
<p>“Also write vendor prefixed CSS so that colons all line up”</p>
</blockquote>
<p>He said:</p>
<blockquote>
<p>“Nope”</p>
</blockquote>
<p>I really have no idea why you wouldn’t do this one. It’s often been proposed that we should write vendor prefixed CSS thus:</p>
<pre><code>-webkit-border-radius:4px;
   -moz-border-radius:4px;
        border-radius:4px;
</code></pre>
<p>This means all our values—the bits that matter the most—are lined up to a) scan quickly and b) edit at once with columnal typing (in text editors which support it). This, to me, is a <a href="http://www.google.co.uk/search?sourceid=chrome&amp;ie=UTF-8&amp;q=define%3Ano-brainer">no-brainer</a> and a very quick-win.</p>
<hr />
<p>I said:</p>
<blockquote>
<p>“Instead of building dozens of unique components, try and spot repeated design patterns abstract them; build these skeletons as base ‘objects’ and then peg classes onto these to extend their styling for more unique circumstances.”</p>
</blockquote>
<p>He said:</p>
<blockquote>
<p>[Not unless you are a big website like Facebook]</p>
</blockquote>
<p>It doesn’t matter if you are working on the next Facebook or for some guy from the pub, if you can build a website from several repeated components you should. From a performance, consistency, maintainability and sheer sensibility point of view, don’t solve the same problem a dozen times when you can solve it once with an abstraction.</p>
<p>Best practices are best practices, the size of a project is irrelevant.</p>
<hr />
<p>I said:</p>
<blockquote>
<p>“All components should be left totally free of widths; your components should always remain fluid and their widths should be governed by a grid system.”</p>
</blockquote>
<p>He said:</p>
<blockquote>
<p>“Nope. Again, grid systems are something that should be used on a case-by-case basis, according to the nature of the site you&#8217;re building.”</p>
</blockquote>
<p>Here I made something of a mistake; when I say grid system what I actually mean is <em>any</em> means of abstracting your layout into its own layer.</p>
<p>If this <em>is</em> a grid system then this is simple; a grid system handles your layout and your components fill it.</p>
<p>If you are not using a grid system then wrapper and parent elements should handle layout and the components should still say fluid. As I said on Twitter just three days prior to this:</p>
<blockquote>
<p>“Box-model properties are the most fragile and unpredictable and thus should be avoided wherever you can. Leave layout to wrappers/parents…” (<a href="https://twitter.com/csswizardry/status/178062012742504448">#</a>)</p>
</blockquote>
<p>And:</p>
<blockquote>
<p>“When sites break it‘s usually because of layout—the less layout stuff we declare the less chance it has to break. Abstract layout to grids!” (<a href="https://twitter.com/csswizardry/status/178063631265710080">#</a>)</p>
</blockquote>
<p>Whether using a grid system or not, your components should not carry dimensions. Think of a page like a set of shelves; you set up your shelving units (grid system (or similar)) and then populate them with things (components). If you didn’t have the shelves erected then the components would be supporting themselves, holding themselves up; this makes moving or changing them very volatile.</p>
<p>Abstract your layout to wrappers, parents or grid systems, always.</p>
<hr />
<p>I said:</p>
<blockquote>
<p>“Heavily location-based selectors are bad for a number of reasons.”</p>
</blockquote>
<p>He said:</p>
<blockquote>
<p>“Nope. What you&#8217;re arguing against is a rule that has a high degree of specificity…”</p>
</blockquote>
<p>I’m not; I’m talking about location based styling. As soon as an elements begins to rely on its parent, and their parent, and their parent’s parent then you are doing it wrong. Your styles should never rely too heavily on where they live as this makes them <em>incredibly</em> unportable.</p>
<p>Putting classes on the elements you wish to affect rather than drilling down to them via the DOM tree is a lot better in terms of portability in that you don’t have to rely on a location in order to set styles. This one really is a <a href="http://www.google.co.uk/search?sourceid=chrome&amp;ie=UTF-8&amp;q=define%3Ano-brainer">no-brainer</a> for me…</p>
<hr />
<p>I said:</p>
<blockquote>
<p>“An over-qualified selector is one like div.promo”</p>
</blockquote>
<p>He said:</p>
<blockquote>
<p>[This is actually a good idea unless you want a class to be applicable to any element]</p>
</blockquote>
<p>Erm, yes please! Ideally classes are applicable to any element; classes should be element-agnostic. The ability to apply them to any element we wish is <em>exactly</em> what we want.</p>
<p>Why would you ever, <em>ever</em> write <code>div.promo{}</code> in your stylesheet if that could be left at <code>.promo{}</code>?! Why would you <em>ever</em> limit yourself to only being able to use a class on one type of element when you could leave the leading type selector off and have an element-agnostic rule?</p>
<p>This is another <a href="http://www.google.co.uk/search?sourceid=chrome&amp;ie=UTF-8&amp;q=define%3Ano-brainer">no-brainer</a> to me. Don’t tie yourself to things when you really just don’t need to. I think we can all agree that <code>.promo{}</code> is a lot more (re)usable than <code>div.promo{}</code>. By tying an element to any class you are the only thing that will hinder you going forward; you are preemptively closing a lot of doors on yourself…</p>
<hr />
<p>I said:</p>
<blockquote>
<p>“Do not use IDs in CSS at all.”</p>
</blockquote>
<p>He said:</p>
<blockquote>
<p>“NOPE NOPE NOPE NOPE NOPE. Understand specificity rules. Write good selectors. Don&#8217;t outright ban IDs just because you&#8217;re not careful enough to write clean CSS.”</p>
</blockquote>
<p>I’m pretty sure no CSS developer has ever said ‘I wish I’d used an ID here instead of a class.’</p>
<p>IDs work, sure, but they are way too specific. It doesn’t matter how good a developer you are, you cannot change the fact that an ID is massively more specific than a class.</p>
<p>I would genuinely love to see an elegant solution to <a href="http://jsfiddle.net/csswizardry/3sxZR/">this</a> that doesn’t use a class over an ID. <small>(<a href=http://csswizardry.com/2011/09/when-using-ids-can-be-a-pain-in-the-class/>There isn&#8217;t one.</a>)</small></p>
<p>There is literally <em>no</em> reason why, from a purely CSS point of view, that an ID is better than a class. An ID is impossible to reuse (whether you want to or not) and they have a way-over-the-top specificity.</p>
<p>Anything you can do with an ID can be done with a class, but with none of the drawbacks. IDs will only ever trip you up because you can never reuse them and they will trump your class-based-selectors by quite an order of magnitude.</p>
<p>Classes; all the bits you love about IDs with none of the bits that are like IDs.</p>
<p>To me, another <a href="http://www.google.co.uk/search?sourceid=chrome&amp;ie=UTF-8&amp;q=define%3Ano-brainer">no-brainer</a>.</p>
<hr />
<p>I said:</p>
<blockquote>
<p>“As a general rule, all layout and box-model rules can and will work without an IE stylesheet if you refactor and rework your CSS.”</p>
</blockquote>
<p>He said:</p>
<blockquote>
<p>“Hahahahahahahahahhaa”</p>
</blockquote>
<p>I was the sole CSS developer on <a href="http://skybet.com">Sky Bet</a>. This project (as a whole) has taken over a year for the whole team involved (and almost a year of my dev time).</p>
<p>To support IE7+ I used all of zero IE stylesheets. Not one. A project which took me almost a year did not require one single IE stylesheet. It is doable, it should be done.</p>
<p>I haven’t written an IE stylesheet in all of my professional career. I started working when I was 18 so that makes three years; I haven’t written an IE stylesheet in over three years. It’s not that hard, seriously.</p>
<p>To just laugh off such a statement seemed a little odd, but seriously, if you invest enough time and take enough care, you will not need an IE stylesheet.</p>
<hr />
<p>Like I said in <a href="https://github.com/csswizardry/CSS-Guidelines/blob/master/README.md">the documents’s README</a>, you can disregard the advice if you wish, but make sure you fully understand it before shunning it completely.</p>
]]></content:encoded>
			<wfw:commentRss>http://csswizardry.com/2012/03/hacker-news-rebuttal/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Pragmatic, practical font sizing in CSS</title>
		<link>http://csswizardry.com/2012/02/pragmatic-practical-font-sizing-in-css/</link>
		<comments>http://csswizardry.com/2012/02/pragmatic-practical-font-sizing-in-css/#comments</comments>
		<pubDate>Wed, 29 Feb 2012 23:49:06 +0000</pubDate>
		<dc:creator>Harry Roberts</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[DRY]]></category>
		<category><![CDATA[OOCSS]]></category>
		<category><![CDATA[Typography]]></category>

		<guid isPermaLink="false">http://csswizardry.com/?p=3496</guid>
		<description><![CDATA[One thing I&#8217;ve been thinking a lot about lately is how to build sites properly. Not what we have been told is proper, but what actually makes sense for us as developers. I recently spoke at The Digital Barn on exactly this; my talk—Breaking Good Habits—dealt with how we as developers need to solve problems [...]]]></description>
			<content:encoded><![CDATA[<p>One thing I&#8217;ve been thinking a lot about lately is how to build sites properly. Not what we have been <em>told</em> is proper, but what actually makes sense for us as developers. I recently spoke at <a href="http://thedigitalbarn.co.uk/"><cite>The Digital Barn</cite></a> on exactly this; my talk—<cite>Breaking Good Habits</cite>—dealt with how we as developers need to solve problems not only for our users and clients, but for ourselves as well.</p>
<p><a href="http://twitter.com/stubbornella">Nicole Sullivan</a>—who totally rocks—has laid a lot of new foundations for us in her work on <a href="http://oocss.org">OOCSS</a> and her &#8216;unconventional&#8217; but absolutely spot-on approach to building websites. Gems like <a href="http://www.stubbornella.org/content/2010/06/25/the-media-object-saves-hundreds-of-lines-of-code/"><i>the media object</i></a> have seriously changed how I build websites and, if you take the time to study it for yourself, I think it might just do the same for you as well.</p>
<h2>Double-stranded heading hierarchy</h2>
<p>Another absolutely stellar nugget of wisdom she&#8217;s given us is what I call <i>double-stranded heading hierarchy</i>. This is the practice of defining a class every time you define a heading in CSS.</p>
<p>For example, if&mdash;for whatever reason&mdash;we want our <code>h2</code>s in our sidebar to be the same size as a <code>h1</code>, and the <code>h4</code>s in our footer to be the same size as a <code>h3</code>, we might have had some code like this:</p>
<pre><code>&lt;div class=content&gt;
  &lt;h1&gt;Lorem&lt;/h1&gt;
&lt;/div&gt;

&lt;div class=sub-content&gt;
  &lt;h2&gt;Lorem&lt;/h2&gt;
&lt;/div&gt;

&lt;div class=footer&gt;
  &lt;h4&gt;Lorem&lt;/h4&gt;
&lt;/div&gt;

h1,
.sub-content h2{ [font styles] }
h2{ [font styles] }
h3,
.footer h4{ [font styles] }
h4{ [font styles] }
h5{ [font styles] }
h6{ [font styles] }</code></pre>
<p>But now we&#8217;d have:</p>
<pre><code>&lt;div class=content&gt;
  &lt;h1&gt;Lorem&lt;/h1&gt;
&lt;/div&gt;

&lt;div class=sub-content&gt;
  &lt;h2 class=h1&gt;Lorem&lt;/h2&gt;
&lt;/div&gt;

&lt;div class=footer&gt;
  &lt;h4 class=h3&gt;Lorem&lt;/h4&gt;
&lt;/div&gt;

h1,.h1{ [font styles] }
h2,.h2{ [font styles] }
h3,.h3{ [font styles] }
h4,.h4{ [font styles] }
h5,.h5{ [font styles] }
h6,.h6{ [font styles] }</code></pre>
<p>As you can see, the former is far more arbitrary and those lists of selectors can soon become unwieldy, especially over a larger project. By assigning a class along with each heading style we now have those styles attached to a very flexible selector that can be moved anywhere, rather than to a very specific and non-movable one.</p>
<p>Now, I&#8217;m not such a fan of the <code>.hN</code> notation, I much prefer a solution that I <em>believe</em> to have been suggested by Mr Jeremy Keith, and that is to use abstract classes made up of the first six letters of the Greek alphabet, thus:</p>
<pre><code>h1,.alpha   { [font styles] }
h2,.beta    { [font styles] }
h3,.gamma   { [font styles] }
h4,.delta   { [font styles] }
h5,.epsilon { [font styles] }
h6,.zeta    { [font styles] }</code></pre>
<p>Which now gives us:</p>
<pre><code>&lt;div class=content&gt;
  &lt;h1&gt;Lorem&lt;/h1&gt;
&lt;/div&gt;

&lt;div class=sub-content&gt;
  &lt;h2 class=alpha&gt;Lorem&lt;/h2&gt;
&lt;/div&gt;

&lt;div class=footer&gt;
  &lt;h4 class=gamma&gt;Lorem&lt;/h4&gt;
&lt;/div&gt;</code></pre>
<p>Neat, huh?</p>
<p>So now <code>.alpha</code> can carry the style information of a <code>h1</code> wherever you wish; it doesn&#8217;t depend on location <em>or</em> a type of element. A double-stranded heading hierarchy; lovely.</p>
<h2>Extending this?</h2>
<p>Okay, so now we have our heading styles all nice and portable we&#8217;ve won most of the battle. I&#8217;ve been using this method for months now and I <em>love</em> it. My CSS is so much more efficient, more portable, more powerful, I can build faster, I&#8217;m not repeating font styles over and over, but what next?</p>
<p>The other night whilst working on <a href="http://faavorite.com">faavorite</a> with <a href="http://twitter.com/makeusabrew">Nick</a> I came up with a full on font-sizing micro-framework.</p>
<p>The problems I found I had with font-sizing on <em>any</em> site include (but are not limited to):</p>
<ul>
<li>Repetition of <code>font-size</code>, <code>line-height</code> etc declarations.</li>
<li>Overly-specific and/or location-dependent selectors (e.g. <code>.sidebar h2{}</code>).</li>
<li>Arbitrary font sizes could and <em>did</em> creep into my CSS.</li>
<li>When using <code>rem</code> with <code>px</code> fallbacks, there is a lot to type!</li>
</ul>
<p>And a few important things to remember:</p>
<ul>
<li>Font sizes, like colour palettes, should be limited, preset and non-arbitrary.</li>
<li>Vertical rhythm is important and easy.</li>
<li>DRY code is important for both efficiency and maintainability.</li>
<li>It&#8217;s important to save yourself as much time as possible.</li>
<li>Classes are neither semantic or insemantic.</li>
</ul>
<p>With this in mind, I decided that I wanted to use font-sizing much like a grid system; define it once in the stylesheet and just constantly reuse it.</p>
<h3>Preset font sizes</h3>
<p>Like colour palettes are, font sizes should be strict, predefined and intentional. From both a code and design point of view, you shouldn&#8217;t deviate from your scale—you shouldn&#8217;t really ever need to and doing so will just make code harder to work with.</p>
<p>Presetting your font sizes is pretty easy; typically you might have requirements for:</p>
<ul>
<li>Normal body copy</li>
<li>Headings 1–6</li>
<li>Small print</li>
<li>A few other sizes for larger-than-normal headings etc.</li>
</ul>
<p>Setting the base font size is simple, just pop it on the <code>html</code> and everything will inherit it, paragraphs, lists, tables, you name it.</p>
<p>For your headings you define a series of <code>hN</code> and its corresponding Greek letter class, e.g. <code>h1,.alpha{}</code>.</p>
<h3>Non-standard font-sizing</h3>
<p>You ever had that need to turn a design <a href="http://www.youtube.com/watch?v=EbVKWCpNFhY">up to 11</a>? When you have a masthead promo and even a <code>h1</code> ain&#8217;t big enough? I think we all have…</p>
<p>It&#8217;s tempting to create a new, unique selector to cater for this new requirement, perhaps something like:</p>
<pre><code>.masthead h1{ font-size:5em; }</code></pre>
<p>And whilst this will work, you&#8217;ll only ever get that 5em goodness if you use <em>specifically</em> a <code>h1</code> that is <em>specifically</em> in a <code>.masthead</code>. This isn&#8217;t very reusable at all. Sadface.</p>
<p>To combat this, I decided to create some new abstract classes, this time borrowing <a href="http://en.wikipedia.org/wiki/SI_prefix">SI prefixes</a>. Now we have the <code>h1,.alpha{}</code> through to <code>h6,.zeta{}</code> that we did before, but as well as those we have:</p>
<pre><code>.giga{ [font styles] }
.mega{ [font styles] }
.kilo{ [font styles] }</code></pre>
<p>These classes are the ones <em>above</em> <code>h1</code> and are the seldom used ones that make stuff massive!</p>
<h3>Going the other way?</h3>
<p>Okay, so far we&#8217;ve worked with body copy to headings to beyond; what about small print? Well I opted to use:</p>
<pre><code>small,<del datetime="2012-03-01T00:00:16+00:00">.omega</del><ins datetime="2012-03-01T00:00:16+00:00">.milli</ins>{ [font styles] }</code></pre>
<p><a href="http://csswizardry.com/2011/01/html5-and-text-level-semantics/#small-el"><code>small</code> has been redefined in HTML5</a> so that&#8217;s an element we can use again freely<del datetime="2012-03-01T00:00:16+00:00"> and <code>.omega</code> is simply the last letter in the Greek alphabet</del>.</p>
<h4 id="lets-use-milli"><ins datetime="2012-03-01T00:06:30+00:00">Addendum</ins></h4>
<p>That there genius and awesome chap <a href="http://twitter.com/TomNomNom">Tom Hudson</a> suggested I use <code>.milli</code> for this as it goes <em>below</em> the regular scale. So, anything <em>on</em> the normal scale is Greek letters, anything <em>off</em> the scale (above or below) is SI prefixes.</p>
<h3>Vertical rhythm</h3>
<p>To maintain vertical rhythm we need two key ingredients; consistent line heights and consistent bottom margins. We need a <a href="http://coding.smashingmagazine.com/2011/03/14/technical-web-typography-guidelines-and-techniques/#tt-magic-number">magic number</a>. This number is typically defined by the line height of your body copy, so if you have <code>html{ font-size:16px; line-height:1.5; }</code> then your magic number is 16 x 1.5 = <strong>24</strong>.</p>
<p>Now you know that all your line heights and margin bottoms <em>have</em> to be a multiple of 24px.</p>
<h2>Bringing it together</h2>
<p>It really is imperative to take a look at an actual example of all the above brought together in order to fully &#8216;get&#8217; it. I made <a href="https://bitly.com/xxiqfm">this jsFiddle demo</a> of <em>just</em> CSS; you can add HTML and tinker with it yourselves, combining elements with classes to create double stranded, portable font sizing framework stuff!</p>
<h2>Where does that leave us?</h2>
<p>We now have a self-contained font-sizing framework which should hopefully mean we never need to define another <code>font-size</code> declaration again! We can manage our brand specific type rules from one place, we can build stuff faster, we can build faster stuff, we can keep our code DRYer, we can keep our styling a lot more consistent, we can keep styling location <em>in</em>dependent and we can make far reaching changes in one fell-swoop!</p>
<p>Feel free to take the code and modify or improve it.</p>
<p>Also please note that I am not suggesting we all use these specific classes; experiment, find your own, see what you&#8217;re comfortable with and report back!</p>
]]></content:encoded>
			<wfw:commentRss>http://csswizardry.com/2012/02/pragmatic-practical-font-sizing-in-css/feed/</wfw:commentRss>
		<slash:comments>47</slash:comments>
		</item>
		<item>
		<title>On HTML and CSS best practices</title>
		<link>http://csswizardry.com/2011/12/on-html-and-css-best-practices/</link>
		<comments>http://csswizardry.com/2011/12/on-html-and-css-best-practices/#comments</comments>
		<pubDate>Sun, 11 Dec 2011 12:15:26 +0000</pubDate>
		<dc:creator>Harry Roberts</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://csswizardry.com/?p=3457</guid>
		<description><![CDATA[Best practices are exactly that; best. Not &#8216;better&#8217;, not &#8216;good when&#8230;&#8217; or &#8216;best if&#8230;&#8217;, just best. They&#8217;re always the best, no matter what.
This is something I learned whilst undertaking the single biggest project of my career so far; the complete (and not-yet-live) rebuild of one of BSkyB&#8217;s most trafficked websites. For years I&#8217;d been working [...]]]></description>
			<content:encoded><![CDATA[<p>Best practices are exactly that; <em>best</em>. Not &#8216;better&#8217;, not &#8216;good when&hellip;&#8217; or &#8216;best if&hellip;&#8217;, just best. They&#8217;re always the best, no matter what.</p>
<p>This is something I learned whilst undertaking the single biggest project of my career so far; the complete (and not-yet-live) rebuild of one of BSkyB&#8217;s most trafficked websites. For years I&#8217;d been working on medium-sized projects where I strove to use as few classes as possible, my CSS was so elegant and hand-crafted and everything used the cascade. I thought it was beautiful.</p>
<p>I found my old approach isn&#8217;t best practice when working on a big site, therefore it&#8217;s not best practice at all&hellip; You can scale down the big site mentality to smaller builds, you can&#8217;t scale up small site mentality to bigger ones. With this in mind, how you&#8217;d build bigger sites is best practice, how you tend to build smaller sites is <em>typically</em> (though, as ever, not always) based on fallacy and myth.</p>
<p><span id="more-3457"></span></p>
<p>I recently rebuilt my friend <a href="http://sampenrose.co.uk/">Sam&#8217;s design portfolio site</a>. Typically I&#8217;d have used IDs everywhere, not used any OO and not really paid much attention to the length or efficiency of my CSS selectors. This would have worked but only because the site is small. Any attempts by Sam to scale the site up, add pages, move components or alter the layout would have been hampered by these methods. Instead I decided to apply big-site mentality and dropped any IDs, used an OO approach and made sure every component is reusable. <a href="https://github.com/csswizardry/sampenrose.co.uk">The resulting code</a> is incredibly flexible, very efficient and still looks nice.</p>
<ul>
<li>OOCSS is <em>always</em> best practice.</li>
<li>DRY is <em>always</em> best practice.</li>
<li>Efficiency is <em>always</em> best practice.</li>
<li>Maintainability is <em>always</em> best practice.</li>
<li>Flexibility is <em>always</em> best practice.</li>
</ul>
<p>It doesn&#8217;t matter if you&#8217;re building the next Facebook or if it&#8217;s just a site for the builder down the road; best practice is always best. You might not notice an inefficient selector on a small site, but it doesn&#8217;t mean that it&#8217;s not still inefficient. Just because you don&#8217;t notice something it doesn&#8217;t mean it&#8217;s not still happening.</p>
<p>Build every site like it&#8217;s a 1000 page behemoth because then it can scale; it may never need to, but it <em>can</em>. Building every site like it&#8217;s a piece of art, using convoluted selectors and rigid, ID ridden code, it can never scale, even if you want it to.</p>
<p>Your code might look like the <a href="http://en.wikipedia.org/wiki/Sistine_Chapel">Sistine Chapel</a>, but if it&#8217;s a chore to maintain, or you find you can&#8217;t pick up a component and drop it anywhere with zero worry, then it&#8217;s not powerful. Code is about power before prettiness. You might feel dirty at first, but when you realise how nicely things fall into place using proper best practices you&#8217;ll see the benefits.</p>
<p>The only person who cares how pretty your code is is you. Your users want fast UIs, your clients want reliable builds and you and your team want code that is easy to maintain 6 months and a dozen client mind-changes down the line.</p>
<p><strong>Best always means best, it has no caveats or conditions.</strong></p>
<h2>Further reading</h2>
<ul>
<li><a href="http://www.lukew.com/ff/entry.asp?1379"><cite>Our Best Practices are Killing Us</cite></a> by Nicole Sullivan</li>
<li><a href="http://oocss.org/">OOCSS.org</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://csswizardry.com/2011/12/on-html-and-css-best-practices/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Measuring and sizing UIs, 2011-style</title>
		<link>http://csswizardry.com/2011/12/measuring-and-sizing-uis-2011-style/</link>
		<comments>http://csswizardry.com/2011/12/measuring-and-sizing-uis-2011-style/#comments</comments>
		<pubDate>Wed, 07 Dec 2011 13:10:21 +0000</pubDate>
		<dc:creator>Harry Roberts</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Sizing]]></category>
		<category><![CDATA[Typography]]></category>
		<category><![CDATA[Units]]></category>

		<guid isPermaLink="false">http://csswizardry.com/?p=3420</guid>
		<description><![CDATA[For years we used pixels to lay out web pages. Then, not so long ago, we were dabbling with ems to make our pages elastic. Now, in 2011, most of us are adopting a responsive approach and using fluid grids and percentages.
These methods seem to have all happened sequentially, with us each time dropping the [...]]]></description>
			<content:encoded><![CDATA[<p>For years we used pixels to lay out web pages. Then, not so long ago, we were dabbling with ems to make our pages <i>elastic</i>. Now, in 2011, most of us are adopting a responsive approach and using fluid grids and percentages.</p>
<p>These methods seem to have all happened sequentially, with us each time dropping the last. I&#8217;ve decided, though, that the best builds use aspects of all previous methods; fixed, elastic and fluid.</p>
<p><span id="more-3420"></span></p>
<h2>Using percentages&hellip;</h2>
<p>Let&#8217;s forget responsive design for a second. Responsive design is a three-tiered approach but here we&#8217;re only interested in fluid layouts (N.B. not even necessarily fluid grids). A fluid layout, as you all know, is one that is size-agnostic; it acts like a liquid, occupying a constant percentage of a varying space.</p>
<p>Now, fluid grids are a little tricky as they&#8217;re based around full grid systems. Luckily I&#8217;ve created a simple <a href="http://csswizardry.com/fluid-grids/">fluid grid calculator</a> to work these behemoths out, but in more simple terms it&#8217;s remarkably easy to set a fluid layout even if that was never the intention.</p>
<p>If your designer sends you a two-column design then all you need to do is work out <strong>not</strong> how big the respective columns are, but instead work out <em>how much bigger one is than the other</em>. That is to say; stop thinking &#8216;this content area is 600px and the sidebar is 300px&#8217; and <em>start</em> thinking &#8216;this content area is twice as big as the sidebar&#8217;. Percentages work well whether you&#8217;re going responsive or not!</p>
<p>Design is about proportions, not absolutes, and in ignoring actual pixel measurements in favour of relative ones you can ensure that designs are not tethered to numbers, but to proportions.</p>
<p>So the next PSD you get sent that is not meant to be responsive, just try this method out and at least make it fluid. If your sidebar is 220px wide and your content area is 640px then take that as meaning &#8216;the content area is 2.909 times bigger than the sidebar&#8217;.</p>
<h3>&hellip;or using nothing at all</h3>
<p>Even better than using percentages is to use no measurement at all. All components you build (navs, tables, figures, thumbnail galleries, banners, you name it) should never have widths (and never <em>ever</em> have heights) applied to them. They should be constrained only by their parents.</p>
<p>An element without measurements is inherently fluid, but in the best possible way; <em>it will work wherever you put it</em>. Ironically, the most important takeaway from this&mdash;an article about setting measurements&mdash;is simply &#8216;don&#8217;t&#8217;. Every time you can avoid setting a measurement, you should.</p>
<h2>Using ems</h2>
<p>Ems are an old favourite. They&#8217;re great for setting type, and with ems it&#8217;s <em>all</em> about the type.</p>
<p>As I outline in the next section, I don&#8217;t actually set font-sizes in ems; I use rems which is essentially <i>ems-with-benefits</i>. What I do set in ems is things <em>to do with</em> type; <code>margin-bottom</code>s on paragraphs, lists, headings and the like; paddings on buttons, nav links and promos and other such measurements; borders on promos etc.</p>
<p>What this does is ensure that the &#8216;feel&#8217; of the design is maintained no matter how far a user wishes to scale their text.</p>
<p>Let&#8217;s take an example. A designer has made a PSD with a nav in it. The nav links are blocks with a font-size of 12px (0.75em) and a padding of 5px, <a href="http://jsfiddle.net/csswizardry/NZLwc/">thus</a>.</p>
<p>Now, as I stated previously, a lot of design isn&#8217;t about numbers, it&#8217;s about <em>proportions</em>. It&#8217;s not about 12px and 5px looking best here, it&#8217;s about <em>a link looks best when its padding is just under half its own font-size</em>.</p>
<p>It can be a confusing way of looking at things at first, but once you realise that the 12 and 5 are actually totally irrelevant here and that it&#8217;s about their <em>relation</em> to one another, you should soon get the hang of things.</p>
<p>Let&#8217;s assume the user doubles their font-size, now you have a link that is 24px in size but still has a padding of 5px. That&#8217;s going to look very cramped. This is why we need to set paddings on text in ems, so that they track the font-size. In this case 5px as an em unit of 12px is 0.417, so now <a href="http://jsfiddle.net/csswizardry/NZLwc/2/">our code looks like this</a>. Try scaling up these two examples up and see how the second looks a lot nicer when the padding scales too.</p>
<p>So, whenever you are setting a measurement because it looks good when set against nearby type (think borders, paddings, margins etc) try and forget the absolutes and begin thinking &#8216;this padding needs to be just under half of whatever the font-size needs to be&#8217;.</p>
<p>Not convinced that users scale their text all that much? Well I have no data but I love <a href="https://twitter.com/5minuteargument/status/134682811683717121">this thought provoking analogy</a> from <a href="https://twitter.com/5minuteargument/">@5minuteargument</a>:</p>
<blockquote>
<p>&#8220;I&#8217;m going to start using TV volume as a CSS font analogy: not everyone is listening to your programme at the same volume&hellip;&#8221;</p>
</blockquote>
<p><strong class=hilite>Please note that when I refer to scaling I don&#8217;t mean the browser&#8217;s Ctrl+[+/-] scaling/zooming, I mean a user stylesheet or design alteration that changes the base font-size of the document.</strong></p>
<h3>Line-heights</h3>
<p>There are several ways of setting line-heights. A general rule of thumb is to never set them in pixels as this, as discussed above, won&#8217;t track your font-size. In this case you&#8217;d set them in ems or percentages, so that you never end up with &#8216;a font-size of 12px and a line height of 18px&#8217;, you&#8217;d get &#8216;text whose line-height is 1.5 times its font-size&#8217;.</p>
<p>However, even better than using ems, you just set them unitless. Work out the em value, but drop the em <em>from</em> the value, so <code>line-height:1.5em;</code> would just be <code>line-height:1.5;</code>. Eric Meyer explains this nicely <a href="http://meyerweb.com/eric/thoughts/2006/02/08/unitless-line-heights/">over on his site</a>.</p>
<p>To work out unitless line-heights is simple. Use the following equation: the line-height I want &divide; the font-size I have. Want a 20px heading to have a 30px line-height? 30 &divide; 20 = 1.5. A 12px button to have a 24px line-height? 24 &divide; 12 = 2.</p>
<h2>Using rems</h2>
<p>Despite <a href="http://csswizardry.com/2011/05/font-sizing-with-rem-could-be-avoided/">my initial thoughts</a>, I have now started setting type in rems (with a pixel fallback).</p>
<p>This gives us two things, firstly we have what I call &#8216;progressive accessibility&#8217; in that it brings the accessibility benefits of ems but only in more advanced browsers.</p>
<p>Secondly, and more importantly, it gives us the confidence of pixels plus the scaling properties of ems. This is ridiculously cool. What this means is that we avoid the annoying compounding issues that ems can give us (a <code>small</code> in a smaller-than-body-copy promo springs to mind) but we can also keep their feature of scalability; we can alter the (font-)size of an entire document based on a parent rather than having to redeclare each individual elements&#8217; font-size over and over. It really is the best of both worlds.</p>
<p>Take <a href="http://jsfiddle.net/csswizardry/6CEjX/">this example</a>. Try changing the <code>html</code>&#8217;s font-size to 2em, does the page scale up as a whole? Nope. <a href="http://jsfiddle.net/csswizardry/6CEjX/1/">Try this one</a>. See how just changing the <code>html</code> element&#8217;s font-size will make your entire page act accordingly. This is something that pixels can&#8217;t give us&#8230;</p>
<p>Interestingly, <a href="https://github.com/csswizardry/hry.rbrts.me/blob/master/css/style.css#LC152">I also set <code>margin-bottom</code> in rems</a> so as to always maintain a consistent vertical rhythm around my <a href="http://coding.smashingmagazine.com/2011/03/14/technical-web-typography-guidelines-and-techniques/#tt-magic-number">magic number</a>.</p>
<h2>Using pixels</h2>
<p>I&#8217;ve said before today that the pixel&#8217;s day is numbered in the web design world. Most of my work involves ems, rems, percentages or nothing at all. The only time I ever really use pixels is if I have a known and fixed dimension to work to; nearly all the time this is images and icons.</p>
<p>Most pixel based measurements pertain to laying out fixed-size images like avatars or sprited elements who require fixed-size background images applying to them. Nearly everything else is best suited to proportional sizing with ems or to non-sizing with just being left fully fluid.</p>
<p>There really isn&#8217;t much work left for the old pixel in 2011.</p>
<h2>An example</h2>
<p>A fairly decent pre-existing example of the techniques discussed would be my <a href=http://hry.rbrts.me/>hry.rbrts.me</a> hub-site.</p>
<p>If you open that page and fire up your inspector you should see that:</p>
<dl>
<dt>Percentages</dt>
<dd>The <code>body</code> and <code>article</code>s all have their structure set in percentages.</dd>
<dt>Nothing</dt>
<dd>The social icons <em>component</em> is free of dimensions, it&#8217;s fully fluid and will always occupy its parent, wherever you put it.</dd>
<dt>Ems</dt>
<dd>The indentations of <code>ul</code>s, <code>ol</code>s and <code>dd</code>s are all set in ems so that they will scale with the lists&#8217; font-size. The gaps between sections are also set in ems so that if the font-size ever increases, so will the space around sections of text.</dd>
<dt>Line-heights</dt>
<dd>These are all set unitlessly (except the tagline which needs to line up with the logo).</dd>
<dt>Rems</dt>
<dd>Type styles are all set in rems to allow us scaling with control.</dd>
<dt>Pixels</dt>
<dd>Only the social media icons and the logo, which are fixed in size before we even start thinking about CSS, are set in pixels.</dd>
</dl>
<p>If you&#8217;d prefer, you can poke through the nicely formatted and commented CSS <a href=https://github.com/csswizardry/hry.rbrts.me/blob/master/css/style.css>on GitHub</a>.</p>
<h2>Key points</h2>
<p>The key things to take away from this article are:</p>
<ul>
<li><strong>Set structure in percentages.</strong> This includes content areas, sidebars etc.</li>
<li><strong>Set type in rems</strong> with a pixel fallback for older browsers. This gives us a great amount of control with added scalability.</li>
<li><strong>Set type-<em>related</em> items in ems</strong> so that paddings et al scale with the font-size.</li>
<li><strong>Set line-heights in relative units.</strong> Or, even better, with no units at all.</li>
<li><strong>Don&#8217;t set measurements on components at all.</strong> They should remain fully fluid and &#8216;just work&#8217; wherever you drop them.</li>
</ul>
<p>So there are a few tips and guidelines on sizing UIs and designs in 2011. We have a whole host of brilliant methods at our disposal and we can just cherry-pick them whatever they&#8217;re best suited to.</p>
]]></content:encoded>
			<wfw:commentRss>http://csswizardry.com/2011/12/measuring-and-sizing-uis-2011-style/feed/</wfw:commentRss>
		<slash:comments>29</slash:comments>
		</item>
		<item>
		<title>Create a notched current-state nav</title>
		<link>http://csswizardry.com/2011/11/create-a-notched-current-state-nav/</link>
		<comments>http://csswizardry.com/2011/11/create-a-notched-current-state-nav/#comments</comments>
		<pubDate>Tue, 08 Nov 2011 20:00:57 +0000</pubDate>
		<dc:creator>Harry Roberts</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://csswizardry.com/?p=3393</guid>
		<description><![CDATA[Ben Everard tweeted last night asking if anyone knew how to build a notched nav, like this. I was in bed at the time, I spotted it about midnight and was on my phone. As soon as I saw this I jumped out of my bed and turned my Mac on. I love stuff like [...]]]></description>
			<content:encoded><![CDATA[<p><a href="https://twitter.com/ilmv">Ben Everard</a> <a href="https://twitter.com/ilmv/status/133640383568678912">tweeted last night</a> asking if anyone knew how to build a notched nav, like <a href="http://cl.ly/1h1S3E2G3H1X06193r08">this</a>. I was in bed at the time, I spotted it about midnight and was on my phone. As soon as I saw this I jumped out of my bed and turned my Mac on. I love stuff like this!</p>
<p>Anyway, <a href="http://jsfiddle.net/csswizardry/ZDNu7/21/embedded/result/">this is my solution</a>, and <a href="http://jsfiddle.net/csswizardry/ZDNu7/21">this is its code</a>.</p>
<p><span id="more-3393"></span></p>
<p>Basically there are two main parts to this technique; the punching-the-hole-through-the-nav and the masking-the-hole-to-be-a-triangle. Both techniques couldn&#8217;t be simpler, and we use pseudo elements to do it.</p>
<h2>Punching holes through elements</h2>
<p>This whole technique can only work by taking advantage of the behaviours of fixed background images. The exact same <code>background:;</code> applied to two elements give an odd result if both are also fixed. It gives the effect of a hole having been <a href="http://jsfiddle.net/csswizardry/7BXUf/show/">punched right through your page to the background&#8230;</a></p>
<p>What we do here, then, is create a pseudo element with <code>.nav .current a:before</code> and sit this at the bottom of the current list item. We then apply the fixed background to this <em>as well as</em> the page. This is our punched hole already sorted, <a href=http://jsfiddle.net/csswizardry/ZDNu7/27/>only it&#8217;s square&#8230;</a> we want a triangle.</p>
<h2>Masking the hole</h2>
<p>To mask the hole to appear like a triangle we use another pseudo element and the CSS triangle hack to cover things up.</p>
<p>The triangle hack works by selectively applying borders to zero width/height elements, take a look at <a href=http://jsfiddle.net/csswizardry/ZDNu7/28/>this version with the triangles highlighted</a>. All we need to do here is make some of them the same colour as the nav and <a href=http://jsfiddle.net/csswizardry/ZDNu7/21/>we&#8217;re done</a>!</p>
<p>So, by cleverly using a pseudo element we can spoof a hole through elements and then using a second one we can mask the corners off!</p>
<p>The full, commented CSS:</p>
<pre><code>.nav{
	overflow:hidden; <span class=code-comment>/* To clear floats */</span>
	background:#111;
	margin:0;
	padding:0;
	list-style:none;
}
.nav li{
	float:left;
}
.nav a{
	display:block;
	padding:2em <span class=code-comment>/* <-- This is our magic number, this defines how large our notch can be! */</span> 1em;
	color:#fff;
	text-decoration:none;
}
.nav a:hover{
	text-decoration:underline;
}
.nav .current a{
	position:relative;
	text-decoration:underline;
	cursor:text;
}
.nav .current a:before,
.nav .current a:after{
	content:"";
	display:block;
	width:2em; <span class=code-comment>/* Must match our magic number... */</span>
	height:2em; <span class=code-comment>/* ...our notch will end up being half this size. We define it in ems to scale it up with the text size. */</span>
	position:absolute;
	bottom:0;
	left:50%;
	margin-left:-1em; <span class=code-comment>/* Half of our magic number. */</span>
}
body,
.nav .current a:before{
	background:url(http://farm5.static.flickr.com/4102/4876702379_82fe2bd7a8_b.jpg) top left no-repeat fixed; <span class=code-comment>/* Apply to the notch and the relevant container (this case, body). */</span>
}
.nav .current a:after{
	width:0;
	height:0;
	border:1em solid #111; <span class=code-comment>/* Half of our magic number and same colours as our nav's background. */</span>
	border-bottom-color:transparent;
}</code></pre>
<h3>Drawbacks</h3>
<p>There are drawbacks here; you <em>have</em> to have a fixed background image and you have to have a solid background colour for your nav, but they are reasonable trade-offs, considering this doesn&#8217;t use any extra markup at all <em>and</em> works in IE8+!</p>
]]></content:encoded>
			<wfw:commentRss>http://csswizardry.com/2011/11/create-a-notched-current-state-nav/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Build-along #1, reflection and reasoning</title>
		<link>http://csswizardry.com/2011/11/build-along-1-reflection-and-reasoning/</link>
		<comments>http://csswizardry.com/2011/11/build-along-1-reflection-and-reasoning/#comments</comments>
		<pubDate>Sat, 05 Nov 2011 16:03:54 +0000</pubDate>
		<dc:creator>Harry Roberts</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Build-along]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[OOCSS]]></category>
		<category><![CDATA[Progressive Enhancement]]></category>
		<category><![CDATA[Responsive web design]]></category>

		<guid isPermaLink="false">http://csswizardry.com/?p=3355</guid>
		<description><![CDATA[Here are, in no particular order, just a few thoughts about the build-along I did last night. These thoughts cover the thinking and reasoning behind the decisions I made. The build-along was a single, small PSD, but the following should apply to builds of any size. Get into the habit of doing the following on [...]]]></description>
			<content:encoded><![CDATA[<p>Here are, in no particular order, just a few thoughts about <a href="http://csswizardry.com/2011/11/css-wizardry-build-along-1/">the build-along</a> I did last night. These thoughts cover the thinking and reasoning behind the decisions I made. The build-along was a single, small PSD, but the following should apply to builds of any size. Get into the habit of doing the following on tiny sites and you&#8217;ll be well equipped to build that next Facebook meets YouTube with a dash of LinkedIn that that prospective client just emailed you about&#8230;</p>
<p>Here is <a href=http://dl.dropbox.com/u/2629908/build-along/index.html>the final build</a> and its code is on <a href=https://github.com/csswizardry/build-along-1>GitHub</a>.</p>
<p><span id="more-3355"></span></p>
<h2>HTML first</h2>
<p>I built this HTML-first. No CSS other than the UA&#8217;s <em>whatsoever</em>. No images, no styles, no JS, no classes, no containers, nothing. I started with pure text-level and content semantics. No <code>div</code>s, no <code>span</code>s, nothing that would in any way aid styling. Nail your pure, raw HTML first before even <em>thinking</em> about CSS. This ensures you&#8217;re thinking fully about the most important aspect of any site; its content.</p>
<h2>No IDs</h2>
<p>The build uses no IDs for styling. This was quite an odd shift for me to make, and I made it <a href=http://csswizardry.com/2011/09/when-using-ids-can-be-a-pain-in-the-class/>a number of weeks back</a>. The main drawback of using IDs is that they introduce a specificity wild card not unlike using <code>!important</code> (though obviously not as horrible). By not using them it means that I can&#8217;t really get tripped up by overly specific selectors as easily as I could if I <em>was</em> using IDs. I&#8217;ve not removed the chance completely, but really easily and quickly lessened it.</p>
<h2>&#8216;Ugly&#8217; classes to do lots of heavy lifting</h2>
<p>There are quite a few classes that some might deem ugly; the media and grid classes instantly spring to mind. The thing here is that <strong>classes aren&#8217;t semantic or insemantic</strong>, they&#8217;re merely sensible or insensible. If a <code>div</code> is 6 columns wide then a class of <code>.grid-6</code> is totally sensible, if it needs to change then change it. If you ever redesign you&#8217;ll be touching the markup anyway; I&#8217;m convinced the pure CSS redesign (in a commercial world) is a myth.</p>
<p>These classes also bring performance benefits, once a class gets called once it becomes free to use again; a performance freebie. If you use <code>grid-6</code> once on a page, every subsequent usage is totally free, from a performance point of view.</p>
<h2>Portable sprite icons</h2>
<p>The <code>.s</code> class is a theoretically horrible class, but as we outlined above, nothing (except micro-formats and similar) reads or even cares what you name your classes. <a href="http://jsfiddle.net/csswizardry/YdhEU/">You can name a class anything you wish and a browser has to honour it</a>, just pick wisely!</p>
<p>So, the <code>.s</code> class is one good example. Whenever you want to use an icon background image you ideally want to sprite it, but in fluid elements this isn&#8217;t possible. Enter a <i>spriting element</i>.</p>
<p>This is just an empty element that gets the sprite applied to it in its own fixed width/height box.</p>
<p>Chris Coyier uses <a href="http://css-tricks.com/13224-pseudo-spriting/">pseudo elements for this</a> which is great as it&#8217;s really clean, but the major drawback for me here is that they&#8217;re not very portable. A pseudo element is tied explicitly to an element in the CSS, so you can&#8217;t just drop the icon wherever you wish. Using an empty element means you <em>can</em> drop an icon wherever you wish. It&#8217;s six-of-one and half-a-dozen of the other; cleanliness versus portability; pick which one you&#8217;d rather have.</p>
<p>I can imagine that 75% reading this still think it&#8217;s a horrible, but we need to remember that an empty element affects <em>nothing</em>. It&#8217;s empty so it has no content, if it has no content then screen readers don&#8217;t encounter anything in it.</p>
<p>You&#8217;re probably also thinking that it&#8217;s heavily presentational, but there&#8217;s really no difference between:</p>
<pre><code>&lt;i class="s star"&gt;&lt;/i&gt;
</code></pre>
<p>And:</p>
<pre><code>&lt;img src=star.png alt=""&gt;
</code></pre>
<p>The first is just out-and-out better in that it allows you to sprite that image up!</p>
<p><ins datetime="2011-11-05T16:49:52+00:00" id="addendum:using-i-element">I was asked why an <code>i</code> and not a <code>span</code>. I&#8217;m almost ashamed of the answer but it&#8217;s purely because <code>i</code> is shorter, it&#8217;s as simple as that. I know that a <code>span</code> is probably better suited as it&#8217;s devoid of semantics but as there&#8217;s no content in the <code>i</code> nothing is affected by semantics anyway. Feel free to use whatever element you prefer though, like I said, my reasoning is kind of shameful!</ins></p>
<h2>Mobile first, lay the foundations</h2>
<p>I did this build mobile first, sort out the content, the type, the general <em>feel</em> of the site first, then used <code>min-width</code> media queries to build <strong>up</strong> the desktop version.</p>
<p>Incidentally I don&#8217;t use <a href=https://github.com/scottjehl/Respond>respond.js</a> or similar to get media queries working in IE et al, they get the mobile version. The layout of the site is not that important because <strong>a PSD is a clue, not a contract</strong>. A PSD tells you how a site should generally appear; the type, the colours, any brand treatments, that kind of stuff.</p>
<p>If you spend enough time on the mobile version that should be good enough to serve as the baseline, anything on top is a bonus for browsers that support media queries.</p>
<h2>Grid systems just make life easier</h2>
<p>In a similar vein to the above, grid systems are typically frowned upon as being insemantic, but the joy is that, as we covered, classes are neither semantic or in semantic. Plus&mdash;even better than that&mdash;a <code>div</code> is devoid of any semantics, it&#8217;s a generic container element. Adding these to your markup comes free-of-charge. Using a grid system allows developers to quickly construct consistent, robust and efficient layouts in seconds. </p>
<h2>&#8216;Extraneous&#8217; divs actually make builds far more robust and extensible</h2>
<p>You should <em>never</em> add markup where avoidable, but that doesn&#8217;t mean you should avoid it at all costs. Sometimes adding an extra <code>div</code> will make components a lot less brittle, rather than relying on unpredictable style rules and overly slim markup, sometimes it&#8217;s just far better to add another <code>div</code> to ensure a more robust build.</p>
<p>Take for example <a href="http://www.stubbornella.org/content/2010/06/25/the-media-object-saves-hundreds-of-lines-of-code/">the media object</a>. You could probably build that construct using far less HTML, but then would it be as extensible? You could just assume there will only ever be an <code>img</code> floated left with a <code>p</code> to the right of it, but if you do that and a client asks for a list with that <code>p</code> and a caption under the <code>img</code> you&#8217;re in a bit of a pickle; if you just start out with a <code>div</code> on each side to start with then you have the ability to build whatever the client throws at you, and it will always be predictable.</p>
<p>So, add extra markup where it saves you headaches. I&#8217;m more impressed by powerful and extensible code than I am with lean and brittle solutions, and I can guarantee which the client will prefer&#8230;</p>
<h2>Don&#8217;t measure stuff</h2>
<p>Throughout this build I only measured one thing; the grid system. I honestly think that, in web design, the pixel&#8217;s days are numbered. Build everything without a care as to its dimensions. Everything in the build can be moved around and dropped anywhere else without you needing to worry if it&#8217;ll fit. All your components should always be fully fluid and only constrained by their parent, in this case the grid system.</p>
<p>Here&#8217;s a challenge, next time your designer sends you a PSD designed on, say, the <a href="http://960.gs">960GS</a>, resize your browser to 800px wide and build it like that. That&#8217;ll really put your fluid, responsive skills to the test!</p>
<h2>Be resourceful</h2>
<p>During this build I copied and pasted <em>loads</em> of code. I used <a href="https://github.com/csswizardry/vanilla">my vanilla boilerplate</a>, I copied and pasted <a href="http://www.stubbornella.org/content/2010/06/25/the-media-object-saves-hundreds-of-lines-of-code/">the media object</a>, <a href="csswizardry.com/2011/09/the-nav-abstraction/">the nav abstraction</a>, <a href="csswizardry.com/2011/10/fully-fluid-responsive-css-carousel/">the carousel</a>. If it already exists somewhere then reuse it! There are no prizes for writing more lines of code; be resourceful.</p>
<h2>Take away?</h2>
<p>Even on tiny sites, powerful markup is far more quick, robust, extensible and sensible than convoluted, brittle stuff. No one reads your classes except other developers. Users appreciate fast UIs, clients appreciate stable and robust sites and you, the developer, like to save time, be efficient and only solve problems once.</p>
<p>What may at first seem like an ugly class or bloated markup is actually a really quick, predictable and reusable construct.</p>
<p>Websites make us money, so let&#8217;s make them as quickly as possible and in the most predictable, future proof way we can.</p>
<h2>Finally</h2>
<p>There is a video of the build to go with this, but I need your opinions, do you want the full, several hour epic or do you want it sped up to, say, double speed? I&#8217;m going to get that processed as soon as possible.</p>
<h2><ins datetime="2011-11-07T11:12:59+00:00" id="addendum:video">Update</ins></h2>
<p>As promised, I recorded the whole thing warts and all. You might be interested to know a few things non-code related about the build-along.</p>
<ul>
<li>The whole lot was done on an 11&Prime; MacBook Air with no external mouse or keyboard.</li>
<li>My good friend Jake, who is wanting to learn a little about web development, was next to me the whole time.</li>
<li>I cooked and ate my (now famous (in tiny, tiny circles)) chili during the build-along.</li>
<li>Refreshment was courtesy of <a href="http://twitpic.com/78e9yd">Matusalem and Fentimans</a>.</li>
<li>The songs you saw on my Spotify are not necessarily wholly representative of my taste in music ;)</li>
</ul>
<p>The normal, non-sped-up recording is now on YouTube. <a href="http://www.youtube.com/watch?v=dH-KgnepMUw&#038;hd=1">Part 1</a>, <a href="http://www.youtube.com/watch?v=9NToqlCJzfQ&#038;hd=1">Part 2</a>. The sped-up version is proving a little more troublesome; crunching over 4 hours of video on an 11&Prime; Air is taking a while&#8230;</p>
<h3><ins datetime="2011-11-07T13:19:59+00:00" id="addendum:fast-video">Sped-up video</ins></h3>
<p>To view the faster video right away, simply opt in to <a href="http://www.youtube.com/html5">YouTube&#8217;s HTML5 Trial</a>, open the YouTube URLs above in Chrome and then select the playback speed options that now appear on the player.</p>
]]></content:encoded>
			<wfw:commentRss>http://csswizardry.com/2011/11/build-along-1-reflection-and-reasoning/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>CSS Wizardry build-along #1</title>
		<link>http://csswizardry.com/2011/11/css-wizardry-build-along-1/</link>
		<comments>http://csswizardry.com/2011/11/css-wizardry-build-along-1/#comments</comments>
		<pubDate>Thu, 03 Nov 2011 19:59:14 +0000</pubDate>
		<dc:creator>Harry Roberts</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[Build-along]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[HTML]]></category>

		<guid isPermaLink="false">http://csswizardry.com/?p=3315</guid>
		<description><![CDATA[Okay, so here we are, about to kick off the build-along. Make sure you are following me on Twitter at around 7pm UK time on the 4 November as I&#8217;m hoping to start the build-along around then. Thanks to Levi Flair for this PSD:


Thanks to everyone who sent a design in! I feel bad that [...]]]></description>
			<content:encoded><![CDATA[<p>Okay, so here we are, about to kick off the build-along. Make sure you are <a href="http://twitter.com/csswizardry">following me on Twitter</a> at around <a href="https://www.google.com/search?q=Time+in+the+UK&#038;ie=utf-8&#038;oe=utf-8&#038;aq=t&#038;rls=org.mozilla:en-US:official&#038;client=firefox-a#pq=time+in+the+uk&#038;hl=en&#038;sugexp=kjrmc&#038;cp=8&#038;gs_id=b&#038;xhr=t&#038;q=Time+in+UK&#038;tok=y6_ahb4u2yl-RL_lp8cxLw&#038;pf=p&#038;sclient=psy-ab&#038;safe=off&#038;client=firefox-a&#038;hs=Q63&#038;rls=org.mozilla:en-US%3Aofficial&#038;source=hp&#038;pbx=1&#038;oq=Time+in++UK&#038;aq=0&#038;aqi=g2g-c1g1&#038;aql=f&#038;gs_sm=&#038;gs_upl=&#038;bav=on.2,or.r_gc.r_pw.r_cp.,cf.osb&#038;fp=282a81e04a27f8a1&#038;biw=1024&#038;bih=583"><strong>7pm UK time on the 4 November</strong></a> as I&#8217;m hoping to start the build-along around then. Thanks to <a href="http://www.leviflair.com/">Levi Flair</a> for this PSD:</p>
<p><span id="more-3315"></span></p>
<p><a href="http://csswizardry.com/wp-content/uploads/2011/11/hnpw_home.jpg"><img src="http://csswizardry.com/wp-content/uploads/2011/11/hnpw_home-626x1023.jpg" alt="Build-along PSD screenshot"></a></p>
<p>Thanks to everyone who sent a design in! I feel bad that I can&#8217;t do them all because the quality was really high. Sorry if I didn&#8217;t pick you, a lot more went into the decision than <em>just</em> looks; I needed to consider time-frames, aesthetics, complexity and suitability to the build-along format.</p>
<h2>Follow along</h2>
<dl>
<dt>Live code</dt>
<dd>You can watch the actual build take shape by <a href="http://dl.dropbox.com/u/2629908/build-along/index.html">periodically refreshing this page</a>.</dd>
<dt>Video</dt>
<dd>I&#8217;m actually going to be recording my screen throughout the whole thing, code, Spotify, Twitter, you&#8217;ll see it all. I&#8217;ll be putting this online a few days after (I won&#8217;t be doing a live screen-cast).</dd>
<dt>GitHub</dt>
<dd>I&#8217;m going to commit build milestones to <a href="https://github.com/csswizardry/build-along-1">a GitHub repo</a> periodically so you can see each major step of the build as it happens.</dd>
<dt>Twitter</dt>
<dd><a href="http://twitter.com/csswizardry">Follow me</a> and keep an eye on <a href="https://twitter.com/search/%23buildAlong">the #buildAlong hashtag</a> (please also use this for anything you Tweet about the event).</dd>
</dl>
<p>Feel free to Tweet at me whilst I&#8217;m working. This should be fun!</p>
]]></content:encoded>
			<wfw:commentRss>http://csswizardry.com/2011/11/css-wizardry-build-along-1/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

