<?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>The Bright Lines &#187; Optimization</title>
	<atom:link href="http://www.thebrightlines.com/category/optimization/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thebrightlines.com</link>
	<description>HTML, CSS, Javascript and more</description>
	<lastBuildDate>Tue, 17 Jan 2012 22:00:35 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Considerations on building a frontend codebase</title>
		<link>http://www.thebrightlines.com/2011/09/22/considerations-on-building-a-frontend-template-in-html-css-and-javascript/</link>
		<comments>http://www.thebrightlines.com/2011/09/22/considerations-on-building-a-frontend-template-in-html-css-and-javascript/#comments</comments>
		<pubDate>Thu, 22 Sep 2011 20:46:34 +0000</pubDate>
		<dc:creator>Wouter Bos</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[Documentation]]></category>
		<category><![CDATA[HTML]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[Web development in general]]></category>

		<guid isPermaLink="false">http://www.thebrightlines.com/?p=933</guid>
		<description><![CDATA[This article is about my thoughts on how to build a solid codebase for your (frontend) web development.]]></description>
			<content:encoded><![CDATA[<p>Every developer at one time stops looking for code from previous built websites and starts building his own standardized code to cut down on production time and repetative work. Call it a boiler plate, codebase, template, scaffolding, framework or whatever. The bottom line is that every developer needs one.</p>
<h2>Problems with my current codebase</h2>
<p>Well, I already have a codebase, but it has some problems. The important one is that it only covers the very basics. That&#8217;s because designs can be very diverse and as a developer I want to stay true to the design. So it&#8217;s hard to build a codebase with grids and blocks if all design differ from eachother.</p>
<p>Another problem major issue is something I have more control over: the codebase and the documentation is a bit scattered. The last problem I have is that I have no demo page of my standard CSS code, so there&#8217;s no easy way to check the template on browser compatibilities.</p>
<p>Now my <a href="/onopager/website/">jQuery plugin project</a> has reached version 1.1, I had the time to do some thinking about a new codebase that would be solid, extendable, easy to deploy and would replace all other codebases and documentation.</p>
<p>Let me make it clear: this article is about my considerations about building a solid framework for myself. It may not apply to you and my ideas not unique either. It still may be helpful for you to get an idea of what sort of codebase you need.</p>
<h2>Existing examples</h2>
<p>The most important thing about a codebase is to decide how to set it up and how to store it. I like the way open source grids like <a href="http://960.gs/">Grid960</a> and <a  href="http://developer.yahoo.com/yui/grids/">YUI</a> have organized their code into little reusable bits and have created a demo and test page for each and every component.</p>
<p>I must admit: I hardly ever used these grids. Most of those grids talk the language of the web developer, not of the designer. And the designers I know don&#8217;t have to limit themselves to a 24-column grid system. Not to mention the designers that are totally ignorant of such a system and whose task is to design a website and mail the Photoshop file to some other company that has to build it. Although I have my reservations towards grids, I like the way these codebases are organized. It can serve as a good <a href="http://www.blueprintcss.org/">Blueprint</a> for my own codebase.</p>
<p>I also get my inspiration from Nicole Sullivan&#8217;s talk on Object Oriented CSS (OOCSS). OOCSS basically means that you build your components in such a way that they become extendable objects. Just like in real object oriented programming. Just have a look at <a href="http://www.stubbornella.org/content/2009/03/23/object-oriented-css-video-on-ydn/">her talk about OOCSS on Yahoo! Developer Network</a>.</p>
<h2>Prefix classes</h2>
<p>By adding a (relatively) unique string before the name of the class you have created your own CSS namespace. It will minimize any interference of other CSS styling that is not part of your own code. Some real life examples can be found in the code that is generated by the CMS systems I have experience with: <a href="http://www.sitefinity.com">Sitefinity</a> and <a href="http://www.sitecore.com">Sitecore</a>. The CMS Sitefinity for example prefixes all CSS classes with &#8220;sf_&#8221;.</p>
<h2>Grids, blocks and text</h2>
<p>All CSS code will be split into different categories that work relatively independently of each other. Until now I can come up with 3 of them.</p>
<h3>Grids</h3>
<p>Grids are the frameworks of placeholders in which we put our content. You know: header, visual, column1, column2, column3 and footer. There are lots of grid templates around like <a href="http://960.gs/">Grid960</a> and <a href="http://www.blueprintcss.org/">Blueprint</a>. My view is that those templates only work if the designer agrees to work within the limits of that framework. My ambition is to write a framework that makes my work easier while still being flexible enough to keep the designers happy.</p>
<h3>Blocks</h3>
<p>There are as many block as the designer can come up with. I think it will be a real time saver to talk with the designer and write down all the types of blocks he thinks he&#8217;ll like to use often. Or even better: just open the last 20 or so web designs he/she made and destil the common blocks and its common features. like menus, news article lists, homepage spotlights, etc. Finally you create basic blocks of it in your template system.</p>
<p>Standarization is not always popular with designers but if it means that projects can be created cheaper there may eventually be budget left to build cool stuff. At least, I hope&#8230;</p>
<h3>Text</h3>
<p>Basic text styling is also a relatively independent component in CSS code. You always define a basic font color, family, size and line height that serves as a base styling for the whole websites. More specific text styling in blocks should be kept to a minimum and preferably relatively to the base text style. So if you have a base style of <code>font: 1em/1.5em normal Arial, sans-serif</code>, you ideally should style a header in a block like this: <code>font-size: 1.5em; line-height: 1.2em;</code>.</p>
<p>Although <code>font-weight</code> is not something you can set relative to a base style, it&#8217;s not likely that it will look ugly in combination with some design if you add <code>font-weight</code> styling to a block. Setting the <code>font-family</code> in the styling of blocks on the other hand will most likely interfere with design, since designs tend to depend heavily on a particular font.</p>
<h2>Create base templates and extend themes</h2>
<p>The code for each grid system and each block will be stored in a seperate base CSS file. Each grid or block can be extended with extra themes or styles. Each style extension will have its own CSS file and presumes that the base file is already loaded.</p>
<h2>Optimizing CSS</h2>
<p>Having all code seperated in small snippets is great for maintaining an overview. As long as you can see what a CSS file does by looking at its filename, its will be easy to add and extend styling to a website.</p>
<p>The template code is not optimized for speed, though. If we don&#8217;t do anything about the fragmentation, the server will have to load a bunch of little CSS files. It&#8217;s important to stress this: It&#8217;s not just the total filesize that matters, each http request costs time. Check out <a href="http://developer.yahoo.com/yslow/">YSlow</a> if you want to know more about this and if it affects your site.</p>
<h3>YUI</h3>
<p>That&#8217;s why all CSS in the website has to be exported, merged and its code minified to one single CSS file. Normally I use <a href="http://developer.yahoo.com/yui/compressor/">YUI compressor</a> for it.</p>
<h3>Less</h3>
<p>But you can do more than just minification. <a href="http://lesscss.org/">Less</a> is a system that extends the CSS syntax with stuff like variables, nested rules and mixins. Less was originally built in Ruby, but there&#8217;s also an <a href="http://www.dotlesscss.org/">ASP.NET port</a>. I hope it will help me create very flexible grids.</p>
<p>By exporting CSS into optimized code, you can focus on making the most readable CSS source file because all comments and spaces in the code will be removed in the end. This is also what I do with JavaScript code and certainly will continue to do.</p>


<p>Related posts:<ol><li><a href='http://www.thebrightlines.com/2009/12/24/too-advanced-css-selectors/' rel='bookmark' title='Permanent Link: Too advanced CSS selectors'>Too advanced CSS selectors</a></li>
<li><a href='http://www.thebrightlines.com/2011/09/08/the-last-resort-in-css-debugging/' rel='bookmark' title='Permanent Link: The last resort in CSS debugging'>The last resort in CSS debugging</a></li>
<li><a href='http://www.thebrightlines.com/2011/11/21/debugging-less-css/' rel='bookmark' title='Permanent Link: Tips for using and debugging of Less CSS code'>Tips for using and debugging of Less CSS code</a></li>
<li><a href='http://www.thebrightlines.com/2012/01/17/1028/' rel='bookmark' title='Permanent Link: Crucial stuff for your company&#8217;s internal projects'>Crucial stuff for your company&#8217;s internal projects</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.thebrightlines.com/2011/09/22/considerations-on-building-a-frontend-template-in-html-css-and-javascript/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>CSS performance, who cares?</title>
		<link>http://www.thebrightlines.com/2010/07/28/css-performance-who-cares/</link>
		<comments>http://www.thebrightlines.com/2010/07/28/css-performance-who-cares/#comments</comments>
		<pubDate>Wed, 28 Jul 2010 05:11:40 +0000</pubDate>
		<dc:creator>Wouter Bos</dc:creator>
				<category><![CDATA[Browsers]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Optimization]]></category>
		<category><![CDATA[efficiency]]></category>
		<category><![CDATA[rendering]]></category>
		<category><![CDATA[selector]]></category>

		<guid isPermaLink="false">http://www.thebrightlines.com/?p=648</guid>
		<description><![CDATA[You probably didn't notice it, but the way you write CSS affects the rendering speed of the browser. Should you care about that? Most likely not, but it is possible to freeze your web page with inefficient selectors.]]></description>
			<content:encoded><![CDATA[<p>Lately I read the following text in <em>Building iPhone Apps with HTML, CSS, and JavaScript</em> (O&#8217;Reilly) on using ID&#8217;s in CSS:</p>
<blockquote cite="http://oreilly.com/catalog/9780596805784?cmp=il-orm-ofps-iphoneapps"><p>&#8220;There are differences between class and id. Class attributes should be used when you have more than one item on the page with the same class value. Conversely, id values have to be unique to a page.</p>
<p>When I first learned this, I figured I’d just always use class attributes so I wouldn’t have to worry about whether I was duping an id value. However, selecting elements by id is much faster than selecting them by class, so you can hurt your performance by overusing class selectors.&#8221;</p></blockquote>
<p>Huh? I never use ID&#8217;s in CSS selectors, but I never experienced slow rendering either. I wanted to test if it&#8217;s stupid not to use ID&#8217;s.</p>
<p>First I wanted to see if it was even able to create a CSS that was hard to parse. Google has a nice article about <a href="http://code.google.com/speed/page-speed/docs/rendering.html#UseEfficientCSSSelectors">writing efficient CSS selectors</a>, so that seemed like a good start before setting up a test. In short there are 2 things that make a fast CSS selector:</p>
<ul>
<li><strong>Make rules as specific as possible</strong><br />
This means that <code>div.content</code> is faster than <code>*</code>.<br />
You have to be especially specific with the rightmost selector, since the browser reads CSS selector from right to left.</li>
<li><strong>Avoid (large) descendant rules</strong><br />
An example of a descendant rule is <code>div.content div.column1 div.columnHeader h2</code>. Descendant rules are less efficient than rules with a single selector, like <code>h2</code>.</li>
</ul>
<h2>Test 1: setting up extremely inefficient CSS</h2>
<p>The most inefficient selector by far is <code>*</code>. Combined with large descendant rules you get extremely inefficient selectors:</p>
<pre name="code" class="css:nogutter">
* * * * * * * * * {
	background: #ff1;
}
* * * * {
	background: #ff2;
}
* * * * * * * {
	background: #ff3;
}
</pre>
<p>I created a CSS file of about 20KB in size with 3600 asterisks characters (*). I combined it with a very large HTML file (1MB) with 9000 opening HTML brackets (<). Because I only wanted to test the CSS rendering, I added the following JavaScript to be able to load the CSS file on my local computer <em>after</em> the page was loaded.</p>
<pre name="code" class="javascript:nogutter">
	<a href="javascript:loadCSS('default.css')">Load CSS</a>
	<script type="text/javascript">
		function loadCSS(url) {
			var fileref=document.createElement("link");
			fileref.setAttribute("rel", "stylesheet");
			fileref.setAttribute("type", "text/css");
			fileref.setAttribute("href", url);
			document.getElementsByTagName("head")[0].appendChild(fileref);
		}
	</script>
</pre>
<h3>Results Test 1</h3>
<table>
<thead>
<tr>
<th>Browser</th>
<th>Render time (sec)</th>
</tr>
</thead>
<tbody>
<tr>
<td>Firefox 3.6</td>
<td>2</td>
</tr>
<tr>
<td>IE 8</td>
<td>4</td>
</tr>
<tr>
<td>IE 7</td>
<td>6</td>
</tr>
<tr>
<td>IE 6</td>
<td>11</td>
</tr>
<tr>
<td>Chrome 6</td>
<td>4</td>
</tr>
</tbody>
</table>
<p><em>All tests were done on my computer (Vista with Intel Duo Core @ 2.4GHz) except for IE7 and IE6. IE7 was tested on a Vista machine with Intel Core Duo SPU @ 2.67Ghz. IE6 was tested on my machine with Virtual PC and on a PC with Windows XP SP3 and a Pentium 3 CPU 3GHz.</em></p>
<p>The results show that browsers freeze for at least 2 seconds in order to render the CSS. So it <em>is</em> possible to screw up your rendering, but you really have to make an effort for it with large HTML files and unrealistic CSS selectors.</p>
<h2>Test 2: setting up a more realistic scenario</h2>
<p>In this test I created a more realistic scenario. I took an existing CSS file and altered it until it was 113KB in size and contained about 1050 selectors. I changed the CSS rules until the bulk of the code looked like this:</p>
<pre name="code" class="css:nogutter">
.Homepage .containerText {
	position: relative;
	margin: 15px auto 0px auto;
	width: 960px;
	background:#ffffff;
}
.Homepage .containerText .containerLeft {
	float:left;
	width:450px;
	padding:10px 20px 45px 10px;
}
.Homepage .containerText .containerLeft p {
	margin:0px 0px 5px 0px;
	color:#414a49;
	line-height:1.286em;
}
</pre>
<p>So most of the code in the CSS file consisted of descendant selectors with classes and hardly any tag names. The corresponding HTML page was also created from existing code and copy-pasted until the file was 107KB in size with almost 600 opening HTML brackets (<). Now we have a relatively large HTML and CSS, but not unrealistic. Once again I tested it in different browsers:</p>
<h3>Results Test 2</h3>
<table>
<thead>
<tr>
<th>Browser</th>
<th>Render time (sec)</th>
</tr>
</thead>
<tbody>
<tr>
<td>Firefox 3.6</td>
<td>0</td>
</tr>
<tr>
<td>IE 8</td>
<td>0</td>
</tr>
<tr>
<td>IE 7</td>
<td>0.5</td>
</tr>
<tr>
<td>IE 6</td>
<td>1.5</td>
</tr>
<tr>
<td>Chrome 6</td>
<td>0</td>
</tr>
</tbody>
</table>
<p>All modern browsers responded so fast that I couldn&#8217;t really notice it needed rendering time. This means that the CSS file does not need any optimization by utilizing ID&#8217;s. The only browser that could profit from more efficient CSS selectors is IE 6. But since I don&#8217;t have to support IE 6 anymore, it&#8217;s not worth my time.</p>
<h2>Conclusion</h2>
<p>Writing fast CSS certainly will help some browsers on slow PC&#8217;s. But that is a minority of the users. So unless you have special requirements for your website, you&#8217;ll be better off focusing on writing logical, maintainable code than trying to get the browser to render a few milliseconds faster. There are some considerations though:</p>
<h3>Being more specific</h3>
<p>A few years ago I switched from writing <code>.foo</code> to writing <code>a.foo</code>. Adding the tag name in the selector gives me a better idea <em>what</em> I&#8217;m styling. Now I know that this coding style is also faster in rendering.</p>
<h3>Descendant selectors</h3>
<p>Descendent selectors (using more than one element in a CSS rule) are inefficient in rendering, but I won&#8217;t give that up. I like to be specific about what element I want to select in CSS. I rather write <code>div.column1 div.article span.date</code> than <code>span.date</code>. The latter is more efficient, but that only works with very small websites because only then you&#8217;d still have an overview of the all classes that are in the HTML code.</p>
<p>When I write JavaScript code I try not to &#8216;pollute&#8217; the global namespace. I try to do the same with CSS by using descendent selectors.</p>
<p>If you need faster rendering, you could alter descendant selectors by making one class that incorporates the whole path. So you&#8217;ll get <code>span.column1_article_date</code> instead of <code>div.column1 div.article span.date</code>. Although it would render faster, it also has some drawbacks. Renaming a class afterwards is tedious work, the class names can become very long and the classes can be used <em>outside</em> it&#8217;s intended containers, so a class called <code>div.column1_article_date</code> could be used anywhere in the DOM and not just inside div with the class <code>column1</code>.</p>
<h2>ID&#8217;s and JavaScript</h2>
<p>I do use ID&#8217;s by the way, Not in CSS though, but for JavaScript for these reasons:</p>
<ul>
<li>Specific elements can be easily accessed in plain JavaScript with <code>getElementById</code>.</li>
<li>If an element has an ID, I can be pretty surre that there is a bit of JavaScript attached to it.</li>
<li>Using ID&#8217;s in your jQuery selectors makes the selectors less dependent on the current HTML structure. It&#8217;s easy to break existing jQuery selectors by removing a class in the DOM.</li>
</ul>


<p>Related posts:<ol><li><a href='http://www.thebrightlines.com/2009/12/24/too-advanced-css-selectors/' rel='bookmark' title='Permanent Link: Too advanced CSS selectors'>Too advanced CSS selectors</a></li>
<li><a href='http://www.thebrightlines.com/2010/01/31/hack-how-to-enable-first-child-in-ie6/' rel='bookmark' title='Permanent Link: Hack: how to enable :first-child in IE6'>Hack: how to enable :first-child in IE6</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.thebrightlines.com/2010/07/28/css-performance-who-cares/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

