CSS Tip: Targeting IE 5.x, 6 and 7 Separately

In rare situations it may be necessary to provide different rules, not only to the Internet Explorer family in general, but also to each individual version. We can combine 3 CSS Hacks to achieve this.

Differentiating between IE 6 and below and IE 7

Firstly we can target IE 6 and IE 7 separately using the underscore hack and far less well documented star property hack (commonly mistaken for the star HTML hack).

.box { background: #00f; /* all browsers including Mac IE */ *background: #f00; /* IE 7 and below */ _background: #f60; /* IE 6 and below */ padding: 7px; color: #fff; }

In this example all non IE browsers (which also includes Mac IE) see the first background rule. This sets the box colour to blue. Both IE 6 & 7 then see the next rule (prefixed with a star) which overrides the first rule and sets the background colour to red. Finally IE 6 and below also see the final rule (prefixed with an underscore) and set the background colour to orange.

Differentiating between IE 6 and IE 5.x

We can expand on this ruleset by making use of the backslash part of the Simplified Box Model Hack described here. Escaping any letter within the property name which isn’t in the range a-f, A-F, 0-9 will hide that rule from IE 5.x. We can therefore define a rule for IE 5.x, which will also be seen by IE 6, and then override that with a backslash commented rule for IE 6.

.box { background: #00f;/* all browsers including Mac IE */ 
*background: #f00; /* IE 7 and below */ _background: #0f0; /* IE 6 and below */ _bac\kground: #f60; /* IE 6 only */ padding: 7px; color: #fff; } 

The background colour in IE 5.x will now be green rather than the orange specified for IE 6.

Conditional Comments

Of course IE already provides the ability to target different versions via conditional comments. You should generally favour these over the method described above. There are, however, situations where you may want to consider it, such as when the scope of changes are so small that you don’t want to incur the overhead of an additional HTTP request if included in an external file or if don’t want to include the IE specific rules inline.


You should always be careful when implementing CSS hacks and first make sure that the problem you’re trying to solve is in fact something that needs hacking around and not something you’ve implemented incorrectly. Tantek Çelik article on the Web Standards Project Website provides interesting reading on the history of hacks and when and when not to use them.

a step further for IE 5.0, and IE 5.5 by using comments in your rules. So your example would be:

.box { background: #00f; /* all browsers including Mac IE */ *background: #f00; /* IE 7 and below */ _background/**/: #0f0; /* IE 5.0 */ _background:/**/ #f62; /* IE 5.5 only */ _background/**/:/**/ #f61; /* IE 6 only */ padding: 7px; color: #fff; } 

Sources: http://www.ejeliot.com/blog/63