Posted By

pckujawa on 01/20/07


css hack ie ie7

Versions (?)

Who likes this?

4 people have marked this snippet as a favorite


CSS hack to target IE7 and under

 / Published in: CSS


I pulled this off of Nicholas Gagne's blog at All rights belong to him.

  1. The IE7 CSS Hack
  2. Saturday, January 28, 2006 by Nicholas Gagne
  4. The developers of Microsoft Internet Explorer 7 claimed to have removed all of the CSS hacks, I had a feeling that, based on Microsoft's reputation of having one of the poorest QA process, a CSS hack for IE7 must exist. I am aware that we could ues Internet Explorer's conditional comments to deliver a seperate stylesheet, but there are known problems with that method if someone has more than one version of IE installed, so for this article we will focus on a CSS hack.
  6. I happened upon a Beta 2 Preview build of Internet Explorer 7, and set up a test page. Here is what my CSS looks like:
  8. #item {
  9. width: 200px;
  10. height: 200px;
  11. background: red;
  12. }
  14. Here is the corresponding HTML:
  16. <div id="item">some text here</div>
  18. Now I begin by adding a lang attribute to my body element:
  20. <body lang="en">
  22. Now, add the CSS to target our div element and start filtering out browsers:
  24. *:lang(en) #item{
  25. background:green !important;
  26. }
  28. I've used !important to override the background property previously set. The :lang selector is not supported by IE7 and therfore all versions of Internet Explorer will not read these styles. Unfortunately, Safari also does not support this attribute. We will need to add some more CSS to target Safari:
  30. #item:empty {
  31. background: green !important
  32. }
  34. The :empty selector is part of the CSS3 specification, and although it is not accurately supported by Safari, it will still select the element (whether it is empty or not). Now, the green box will show up in every browser except all versions of Internet Explorer. With this IE7 CSS hack you will be able to apply specific styles that will only affect all versions of Internet Explorer including Internet Explorer 7. The IE7 CSS hack has been tested, and found to be working correctly, in the following browsers:
  36. * IE7 Beta 2 Preview/Win
  37. * IE5.01+/Win
  38. * Firefox 1.5/Win
  39. * Opera 8.5/Win & Linux
  40. * Netscape 7.01, 8/Win
  41. * Mozilla 1.7.12/Win & Linux
  42. * Safari 2/Mac
  43. * Firefox 1.0.4/Linux
  44. * Epiphany 1.4.8/Linux
  45. * Galeon 1.3.20/Linux

Report this snippet  

You need to login to post a comment.