<?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; hack</title>
	<atom:link href="http://www.thebrightlines.com/tag/hack/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thebrightlines.com</link>
	<description>HTML, CSS, Javascript and more</description>
	<lastBuildDate>Thu, 29 Jul 2010 22:44:34 +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>Hack: how to enable :first-child in IE6</title>
		<link>http://www.thebrightlines.com/2010/01/31/hack-how-to-enable-first-child-in-ie6/</link>
		<comments>http://www.thebrightlines.com/2010/01/31/hack-how-to-enable-first-child-in-ie6/#comments</comments>
		<pubDate>Sun, 31 Jan 2010 00:43:14 +0000</pubDate>
		<dc:creator>Wouter Bos</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[first-child]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[IE6]]></category>
		<category><![CDATA[jQuery]]></category>

		<guid isPermaLink="false">http://www.thebrightlines.com/?p=433</guid>
		<description><![CDATA[How to implement the CSS selector :first-child in IE6 with a little help from Javascript.]]></description>
			<content:encoded><![CDATA[<p><strong>UPDATE:</strong> The pure Javascript routine is updated. It is now as fast as jQuery.</p>
<p>One of the things I most dearly miss in IE6 is the lack of support for the CSS selector <code>:first-child</code>. Some shortcomings in browsers can be patched with Javascript. And <code>:first-child</code> is one of them for sure. If you use a Javascript library like jQuery or Prototype it&#8217;s just one line of Javascript:</p>
<pre name="code" class="xhtml:nogutter">&lt;!--[if lte IE 6]&gt;
    &lt;script type="text/javascript"&gt;
        jQuery("*:first-child").addClass("firstChild")
    &lt;/script&gt;
&lt;![endif]--&gt;</pre>
<p>If you don&#8217;t like Javascript libraries you can use this pure js-code:</p>
<pre name="code" class="xhtml:nogutter">
var tags = document.getElementsByTagName("*"); var nTags = tags.length;
for (var i=0;i<nTags;i++) {
	if (tags[i].firstChild) {
		var el=tags[i].firstChild; while ((el.nodeType!=1)&#038;&#038;(el.nextSibling)) el=el.nextSibling;
		if (el.nodeType==1) el.className += " firstChild";
	}
}
</pre>
<p>Now every first child has the class <code>firstChild</code> that can be used in CSS selectors. You'll get something like this:</p>
<pre name="code" class="css:nogutter">
ul li:first-child,
ul li.firstChild {
    font-weight: bold;
}
</pre>
<h2>The speed</h2>
<p>Throwing around classes through the whole HTML tree is not without a price. Certainly not if you keep in mind that Javascript in IE6 is much slower than in Firefox or Safari. So I tested what would happen if the Javascript would run in the homepage of amazon.com, a page with almost a 1000 tags. Here are the results:</p>
<ul>
<li><strong>Adding firstChild with jQuery:</strong>
<ul>
<li>Amazon.com homepage (1000 tags): +- <strong>215</strong>milliseconds</li>
<li>Five times the Amazon.com homepage (5000 tags): +- <strong>742</strong> milliseconds</li>
</ul>
</li>
<li>Adding firstChild with plain Javascript:
<ul>
<li>Amazon.com homepage (1000 tags): +- <strong>129</strong> milliseconds</li>
<li>Five times the Amazon.com homepage (5000 tags): +- <strong>798</strong> milliseconds</li>
</ul>
</li>
</ul>
<p>As you can see there is not much performance difference. Especially when you keep in mind that every time I ran my test page I got different results. With a page of 5000 tags it could run as fast as 481 milliseconds or as slow as 931.</p>
<h2>Code crafting</h2>
<p>The first test I did with the pure Javascript code gave different results because of a inefficient routine. It ran about 1/3 slower in the 1000-tag page and 4 times slower in a 5000-tag page. After consulting a colleague I was able to find a more efficient routine that could match with jQuery.</p>
<h2>Conclusion</h2>
<p>I think a lag of about 200 milliseconds during a page load with a 1000 tags can be acceptable. Especially since a web page of 1000 tags is already a large website to my standards. Most websites I develop have about 300 tags a page at best. If you want to use this hack, I guess it's best to test the script in the largest pages of the website. The gain isn't that spectacular: just support for :first-child. But on the other side it could really make your life as a web developer just a bit easier.</p>
<p>You might be tempted to patch other shortcomings of IE6 with Javascript. Don't start coding, but consider the <a href="http://code.google.com/p/ie7-js/">IE7.js-script</a>. But I must say up front that I don't have good experiences with it. The script works good in small pages with small CSS files, but you'll experience serious performance issues with medium or large websites.</p>
<h2>Putting it in perspective</h2>
<p>Chrome 4, by the way needed just 37 milliseconds to run the Javascript in the 5000-tag page. That's 20 times faster then IE6.</p>


<p>Related posts:<ol><li><a href='http://www.thebrightlines.com/2010/01/04/alternative-for-nth-of-type-and-nth-child/' rel='bookmark' title='Permanent Link: Alternative for :nth-of-type() and :nth-child()'>Alternative for :nth-of-type() and :nth-child()</a></li>
<li><a href='http://www.thebrightlines.com/2009/10/28/for-the-sloppy-clickers/' rel='bookmark' title='Permanent Link: For the sloppy clickers'>For the sloppy clickers</a></li>
<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/2009/11/08/how-to-overcome-the-limits-of-css-in-internet-explorer-6/' rel='bookmark' title='Permanent Link: How to overcome the limits of CSS in Internet Explorer 6'>How to overcome the limits of CSS in Internet Explorer 6</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.thebrightlines.com/2010/01/31/hack-how-to-enable-first-child-in-ie6/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Alternative for :nth-of-type() and :nth-child()</title>
		<link>http://www.thebrightlines.com/2010/01/04/alternative-for-nth-of-type-and-nth-child/</link>
		<comments>http://www.thebrightlines.com/2010/01/04/alternative-for-nth-of-type-and-nth-child/#comments</comments>
		<pubDate>Mon, 04 Jan 2010 21:17:14 +0000</pubDate>
		<dc:creator>Wouter Bos</dc:creator>
				<category><![CDATA[CSS]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[nth-child]]></category>
		<category><![CDATA[nth-of-type]]></category>
		<category><![CDATA[zebra]]></category>

		<guid isPermaLink="false">http://www.thebrightlines.com/?p=334</guid>
		<description><![CDATA[Replace a CSS3 selector that's not supported in IE with a CSS2 selector that has support.]]></description>
			<content:encoded><![CDATA[<p><a class="button demo" href="/article-data/demo/nthChildAlternative.html"><span> </span>View demo</a></p>
<p>One thing I really like about jQuery is the ease by which you can select the <em>nth</em> item of a list of items like this: <code>jQuery("ul &gt; li:eq(3)")</code>. In CSS you would select an <em>nth</em> item like this: <code>ul &gt; li:nth-of-type(3)</code>. The problem is that <code>:</code><code>nth-of-type</code><code>()</code> is not supported at all in Internet Explorer. IE9 <em>will</em> support all CSS3 selectors, including <code>:</code><code>nth-of-type</code><code>()</code>, but by the time IE9 is a mature browser, Webkit and Gecko-based browsers are be able to do everything except your dishes.</p>
<p>So if you want to make an exception for, say, the third menu item, you have to either add a class to that menu item or select that item through Javascript. But if you don&#8217;t care for IE6, there&#8217;s a reasonable alternative:</p>
<p><code>ul &gt; li:</code><code>nth-of-type</code><code>(3)</code> equals <code>ul &gt; li:first-child + li + li</code>.</p>
<p>Although both selectors are different, they do select the same element. But the first is not supported in IE and the second <em>is</em>. From IE7 and up! Sure, it&#8217;s not feasible to select the 100th item in a list, but not all lists are that long. And you can always use Javascript as a fallback.</p>
<p>This alternative selection method is by the way by no means equal to <code>:</code><code>nth-of-type</code><code>()</code>. With <code>:</code><code>nth-of-type</code><code>()</code> you can make a cool <a href="http://www.alistapart.com/articles/zebratables/">zebra striping</a> without the use of Javascript.</p>
<p>And finally, as suggested in the title, there is also a way to handle <code>:nth-child</code><code>(3)</code> in an almost similar fashion:</p>
<p><code>ul &gt; li:</code><code>nth-child</code><code>(3)</code> equals <code>ul &gt; *:first-child + * + *</code>.</p>


<p>Related posts:<ol><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>
<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/2009/11/08/how-to-overcome-the-limits-of-css-in-internet-explorer-6/' rel='bookmark' title='Permanent Link: How to overcome the limits of CSS in Internet Explorer 6'>How to overcome the limits of CSS in Internet Explorer 6</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.thebrightlines.com/2010/01/04/alternative-for-nth-of-type-and-nth-child/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
