Selected Entry
5 months, 11 days agoCSS 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.
Disclaimer
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.
Comments
Help make this post better
Notes: Standard BBCode for links, bold, italic and code are supported. rel="nofollow" is added to all links. Your email address will never be displayed on the site. The audio CAPTCHA quality is not yet of as high a standard as I'd like it to be. If you're having problems with both the visual and audio CAPTCHA please accept my apologies and consider sending your comment to me at me@ejeliot.com. I'll ensure it's added promptly to the relevant blog post.
Menu
RSS Feeds
Blog Posts by Email
Recent Posts
- Web Typography and Font Units
- Tips for Writing Nicer Site Badges
- Web Standards Group (London) - Accessibility Meetup
- Delicious Stumble
- SimpleRss - a class for retrieving and caching RSS feeds
- Web Development Roles at Yahoo!
- Happy New Year
- Creating a server side del.icio.us badge with PHP
- Adding JSMin to my CSS/JS merging script
- Automatic merging and versioning of CSS/JS files with PHP
Last 12 Months
| Month | Posts |
|---|---|
| March 2007 | 1 |
| February 2007 | 2 |
| January 2007 | 3 |
| December 2006 | 6 |
| November 2006 | 3 |
| October 2006 | 5 |
| September 2006 | 3 |
| August 2006 | 6 |
| July 2006 | 2 |
| June 2006 | 3 |
| May 2006 | 3 |
| April 2006 | 4 |
Connect/Save
I am ejeliot on del.icio.us.Add me to your network.
Latest Quick Links
Seen an interesting site? Tag with for:ejeliot in del.icio.us to let me know about it.
20th March 2007
- Using PHP CLI for TextMate commands This article explains how to make use of PHP within TextMate. It also points out that TextMate sets environment variables and explains how they can be read from a PHP script.
- Oh Yeeaahh! Khoi Vinh has written a detailed post about his grids presentation at SXSW as well as posting a link to his slides. The presentation was easily one of the best I saw at SXSW so I'd definitely suggest heading over there to take a look.
17th March 2007
- Unobtrusive connected select boxes - yet another solution approach A really nice idea developed by Christian Heilmann and Bradley Wright.
13th March 2007
- Web Typography Sucks Slides from a presentation Richard Rutter and Mark Boulton gave on why web typography sucks and why less attention is being paid to detail on the web versus print.
11th March 2007
- Yeeaahh! At SXSW today Mark Boulton and Khoi Vinh took at look at working with layout grids and demonstrated applying it to a hypothetical re-design of a very well known front page. The resulting page looks great.
2nd March 2007
- Performance Research, Part 3: When the Cookie Crumbles This article discusses the impact of cookies on the overall user response time.
23rd February 2007
- Free Hosting of YUI Files from Yahoo! If you're using YUI for your own project, Yahoo! will serve the files for you - gzipped, with good cache-control, using their state-of-the-art network, for free.
- Yann Arthus-Bertrand This photographer has produced a really interesting set of photographs taken from the air.
21st February 2007
- OpenID at the Future of Web Apps Simon's talk on OpenID was well presented and very interesting. Look out for the slides and notes on this post.
18th February 2007
- Top Five Articles for Presenters An interesting list of articles to help you improve your presenting skills.
You can view an archive of Quick Links here.

Nice hack, you can take this a step further for IE 5.0, and IE 5.5 by using comments in your rules. So your example would be:
Andy Beeching - 9th October 2006
If you're just worried about targeting specific IE versions, conditional comments are probably safer. They've been around since IE 5, and you can even use them to target non-ie browsers using the ! (not) syntax. The official About Conditional Comments page may be of use if you want to dig into the details.
As an aside, please fix the comment area size (it's tiny! 32 characters across is hardly big enough for writing comments). Also, the cursor is practically invisible because of the gray background color in the comment box. In general, the site could benefit from a little more contrast between text and background color, it's tiring to read more than a few lines on this color scheme.
Mark Reeder - 9th October 2006
Andy - thanks for the additional tip, really interesting.
Mark - re conditional comments, agree to some extent hence the section on them in my post.
The comment forms definately needs some work and I'll be looking at improving it shortly.
Ed Eliot - 9th October 2006
No worries, it's useful for development purposes at least as you can quickly isolate rules down to a particular version, and then stick them in their respective conditional comment stylesheets later (though I only have one and use @import to pull in separate sheets for different browsers)
Andy Beeching - 10th October 2006
Thanks for sharing this with us! It is important to know how to feed IE7 only with CSS rules, as I have information that IE7 is not as standard compliant as FF is.
We will still need to use these hacks... unfortunately...
Stefan Isarie - 11th October 2006
Hi
Can you help me with some advice? I am trying to get this site to look right in IE 5. Another consultant said it was unsupported but i can see other blogs in 5 just fine.
sorry to be a noob idiot but i am trying to learn as much as fast as i can thank you
jeff - 1st December 2006
jeff - which site exactly? IE 5.0 is a really old browser now so it's likely that many sites won't actively support it.
Ed Eliot - 1st December 2006
Really interesting and informative, pleased to know the facts.
Jayanta Banerjee - 9th January 2007
Man thanks for this info.
IE hacks make my brain hurt and this probably just saved me a few hours of Google time!
Ian - 15th February 2007