Now that everyone is all giddy about CSS3 and all of the coolness it offers, lets get back down to earth. We still have older browsers to support so while using some of the new nifty features is definitely cool, we need to ensure we understand that at least 80% of web users won't be on a cool new browser.

<h2>Enter Selectivizr, the Library Formerly Known as ie-css3.js</h2>

Internet Explorer is definitely one of those that needs special attention which is where a new library called <a href="http://selectivizr.com/">Selectivizr</a> comes in. It's a JavaScript library that allows you to take advantage of CSS3 selectors now, even for users of Internet Explorer 6 through 8, by emulating these pseudo-selectors via JavaScript. It's become such a hit that it was even nominated for Innovation of the Year in <a href="http://www.thenetawards.com/">.net magazine's 2010 awards</a>.

The inclusion of <a href="http://selectivizr.com/">Selectivizr</a> is extremely easy. You add it in using a standard <em>script</em> tag just like any other JavaScript file. Notice that we're using IE conditional statements to determine the browser version and if it's less than IE9, the library gets included. Makes sense since IE9 should include many of these pseudo-selectors.

[code language="html"]
&lt;script src=&quot;jquery-1.4.2.min.js&quot;&gt;&lt;/script&gt;
&lt;!--[if lt IE 9]&gt;
	&lt;script src=&quot;selectivizr.js&quot;&gt;&lt;/script&gt;
	&lt;noscript&gt;&lt;link rel=&quot;stylesheet&quot; href=&quot;css/ie-fallback.css&quot; media=&quot;screen, projection&quot;&gt;&lt;/noscript&gt;
&lt;![endif]--&gt; 	
&lt;link href=&quot;style.css&quot; rel=&quot;stylesheet&quot;&gt; 
[/code]

Once included, you're now free to add CSS3 pseudo-classes to your style sheet. For example, I can now use the nth-child() pseudo-class to set the text to every odd numbered paragraph within my container div to red:

[code language="css"]
#container p:nth-child(odd) { color: #ff0000; }
[/code]

and it will render correctly in Internet Explorer versions 6-8. 

<a href="http://reybango.com/demos/selectivizr/noselectivizr.html">Check it out in Internet Explorer with Selectivizr <strong>NOT</strong> included.</a>
<a href="http://reybango.com/demos/selectivizr/selectivizr.html">Check it out in Internet Explorer with Selectivizr included.</a>

In order to have done this previously, I would've have to had to rely on complex JavaScript or using methods included in a JavaScript library (e.g. <a href="http://api.jquery.com/nth-child-selector/">jQuery's nth-child method</a>) to be able to grab the DOM element and style it. For example:

[code language="javascript"]
        $(document).ready(function () {

            $(&quot;#container p:nth-child(odd)&quot;).css(&quot;color&quot;, &quot;#ff0000&quot;);

        });
[/code]

<a href="http://reybango.com/demos/selectivizr/noselectivizr-jq.html">Check out the jQuery version in Internet Explorer with Selectivizr <strong>NOT</strong> included.</a>

While that will style the paragraphs the way I want, it's not a portable and reusable solution. Using Selectivizr is substantially easier because I can now specify reusable CSS rules where they belong; in a style sheet.

The main thing that you NEED to have is a JavaScript library with a DOM selector engine. Selectivizr has support for <a href="http://jquery.com/">jQuery</a>, <a href="http://mootools.net/">MooTools</a>, <a href="http://www.dojotoolkit.org/">Dojo</a>, <a href="http://www.prototypejs.org/">Prototype</a>, <a href="http://developer.yahoo.com/yui/2/">YUI</a>, <a href="http://www.domassistant.com/">DOMAssistant</a> and the <a href="http://javascript.nwbox.com/NWMatcher/">NWMatcher</a> selector engine. Considering that most sites leverage at least one of these libs, that's pretty good coverage.

One of the things I did notice when doing a <a href="http://selectivizr.com/js/ie.js">view source on the Selectivizr website</a> is something that I had already thought would be a good idea. The author, Keith Clark, combines his Selectivizr code with <a href="http://remysharp.com/2009/01/07/html5-enabling-script/">Remy Sharp's html5shiv</a> which makes a lot of sense considering html5shiv allows you style HTML5 tags in non-supporting browsers. So rolling up these two libs (or Modernizr) is a good idea. One less http request to make.

<h2>How Does this Work?</h2>

I pinged Keith so I can get a better understanding of the magic going on under the hood and here's what he said:

<blockquote>Basically, the script scans the page looking for &lt;link&gt;‘d style sheets which are downloaded using a XmlHttpRequest (or ActiveX for IE6). The style sheets are then parsed for rules containing CSS3 selectors. Any matching rules are extracted and passed to a JavaScript library (such as jQuery, Prototype, NWMatcher etc.) which selects matching elements from the page. These elements are then given a unique className and, if necessary, event handlers are added (if interactive pseudo-classes such as :hover, :focus, : checked etc. need to be emulated). The original CSS rule is then replaced with the same className and once all rules are processed, the original style sheet content is replaced with the modified one.</blockquote>

So if you're wondering why there's a dependency on a JS lib, there ya go. For a deeper dive into the how-to's check out his <a href="http://www.keithclark.co.uk/labs/ie-css3/">more thorough explanation here</a>.

