Start Using CSS3 Pseudo-Classes in Internet Explorer 6, 7, and 8 using the Selectivizr JavaScript Library

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.

Enter Selectivizr, the Library Formerly Known as ie-css3.js

Internet Explorer is definitely one of those that needs special attention which is where a new library called Selectivizr 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 .net magazine’s 2010 awards.

The inclusion of Selectivizr is extremely easy. You add it in using a standard script 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”]
<script src="jquery-1.4.2.min.js"></script>
<!–[if lt IE 9]>
<script src="selectivizr.js"></script>
<noscript><link rel="stylesheet" href="css/ie-fallback.css" media="screen, projection"></noscript>
<link href="style.css" rel="stylesheet">

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; }

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

Check it out in Internet Explorer with Selectivizr NOT included.
Check it out in Internet Explorer with Selectivizr included.

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. jQuery’s nth-child method) to be able to grab the DOM element and style it. For example:

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

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


Check out the jQuery version in Internet Explorer with Selectivizr NOT included.

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 jQuery, MooTools, Dojo, Prototype, YUI, DOMAssistant and the NWMatcher 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 view source on the Selectivizr website is something that I had already thought would be a good idea. The author, Keith Clark, combines his Selectivizr code with Remy Sharp’s html5shiv 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.

How Does this Work?

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

Basically, the script scans the page looking for <link>‘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.

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 more thorough explanation here.

Rey Bango


  1. Hi Rey – How does the use of this compare with the script from, which some Bomb guy posted on Ajaxian back in July? CSS3PIE works very well and is fast, though it’s a pain putting in a behavior: line in each CSS selector.

    • I think CSS3PIE can be complementary to Selectivizr since they provide different functionality. Selectivizr, for example, doesn’t provide support for border-radius or box-shadow while CSS3PIE doesn’t offer pseudo-class support.

  2. Rey – Yes, I noticed the difference as soon as I started digging into Selectivizr. Dang me for a hasty reply to your post. Again, I appreciate your bringing both of these to our attention. css3PIE has been great; seeing a site render in IE 6 as it does in modern browsers is a joy to behold.

  3. Rey,
    Thank you for the information.
    Just thought that a post on the subject should have mentioned Dean Edwards’ IE7 library which dealt the task successfully several years ago.

    • Thanks Alexei. Dean is an amazing developer and his contributions to web dev are too numerous to count. In this case, I wanted to focus on a specific, new tool and show developers how to use it. It certainly doesn’t diminish Dean’s previous work and I know that developers will continue to learn from that as they build new things.

Comments are closed.