Quick Tip: How to Target IE6, IE7, and IE8 Uniquely with 4 Characters
videos

Quick Tip: How to Target IE6, IE7, and IE8 Uniquely with 4 Characters

Tutorial Details
  • Difficulty: Beginner
  • Length: 4 Minutes
  • Program: CSS

Two months ago, I, in a video quick tip, demonstrated how to use the underscore and star hacks to target Internet Explorer 6 and 7 in your stylesheets. In today’s quick tip, we’ll take things one step further, as we introduce a new hack that targets IE8 and below as well. It should be noted that this is not a best practice, and conditional comments should be used instead 98% of the time. With that said, it’s always important to know what you can do – plus it’s fun, right?


IE8 and Below

The key to targeting Internet Explorer 8 and below, with a hack, is to append “\9″ to the end of your style. For example:

body {
 color: red; /* all browsers, of course */
 color : green\9; /* IE8 and below */
}

It’s important to note that it must be “\9″. Unfortunately you can’t replace this with something along the lines of “\IE”, like I attempted to do so. Even “\8″ won’t work; it must be “\9″.


IE7 and Below

As we learned in the quick tip from January, we can use the * symbol to target IE7 and below, like so:

body {
 color: red; /* all browsers, of course */
 color : green\9; /* IE8 and below */
 *color : yellow; /* IE7 and below */
}

IE6

Lastly, we have the underscore hack, which most designers are familiar with by now. Rather than the * symbol, we use the underscore. This will target only Internet Explorer 6.

body {
 color: red; /* all browsers, of course */
 color : green\9; /* IE8 and below */
 *color : yellow; /* IE7 and below */
 _color : orange; /* IE6 */
}

A Note About CSS Hacks

It's worth noting that I'm not advocating the use of hacks in your stylesheets in any way. On the contrary, you should almost always use conditional comments. However, that doesn't mean that it isn't helpful to know what you can technically get away with, whether it be for debugging, or showing off to your friends!

The biggest concern is that hacks aren't future proof, at least not really. For example, what if, with the release of Firefox 4, they, too, recognized properties prepended with the * hack. They probably never would for compatibility reasons, however, if they did, that could potentially ruin a portion of your layout. Ultimately, just be wise when using hacks. If you only need to change one or two properties to make IE6 happy, then I don't see any harm in using the underscore hack directly in your stylesheet. The world won't end. However, if there are a handful of changes, be sure to use conditional comments!

<!--[if lte IE 7]>
Make IE7 happy.
<![endif]-->

Thanks for reading and watching!

Add Comment

Discussion 113 Comments

Comment Page 2 of 2 1 2
  1. Logic says:

    It is not obvious but what I think is happening in IE 8 and below is they are converting \9 to its literal meaning, a tab. The tab is 9 in the ASCII table. In turn IE trims off the whitespace (tab) so it only sees “red” while other browsers see “red\9″. Unfortunate the specs do not indicate if escape codes should or should not be transformed in non-string literals

    • Logic says:

      Alright, Firefox does turn “\9″ into a “tab” but it does not trim whitespace.

      This is the error it gave:
      Warning: Expected color but found ‘red ‘. Error in parsing value for ‘color’. Declaration dropped.

      I would think trimming whitespace would be done…

  2. Rishi Luchun says:

    Great tips, found these really handy

  3. vinay kumar says:

    Hi Sir,
    ple sir aap ek page main browser capbility kaise use hota IE 6,7,8,9 & then mozilla, suffaree, opera, ko kaise html page ke under use karte hai .

    ple sir

    thnaks
    Sir

  4. Mike Hopley says:

    In defence of CSS hacks, they avoid the extra HTTP request for an IE-only stylesheet (a performance cost); and they keep related code in one place, making maintenance easier.

    That’s why Yahoo prefers CSS hacks over conditional comments. See around 1:07:00 in this video: http://video.yahoo.com/watch/4671445/12486762

    The * and _ hacks are stable, since they’ve been fixed in IE8. However, it’s unknown whether this new IE8 hack is stable, because we don’t know whether IE9 will apply it. Using this hack may cause IE9 to be tarred with the same brush.

    Being pragmatic, however, I think I’ll just use the hack and see what happens when IE9 comes out. I prefer this to the performance hit of an extra HTTP request in the [head]. Thankfully, it’s rare that IE8 needs a hack anyway.

  5. Akbar says:

    Thank you very much! :)

  6. Roy says:

    Great tips! Just to add, color:#000000/; targets only IE 8.

  7. Joe Cochran says:

    Despite the invalidity of the css, there are circumstances where conditional comments cannot be used. I worked at a company who’s cms used anything within [ and ] as a means of calling part of the cms, and thus, it would break a conditional comment. as a last resort, I added a *width: to my stylesheet to fix the one bug ie 7 and 6 had with min-width. If conditionals are available though, I would never make invalid css.

  8. Eugene says:

    To use conditional comments CSS should be placed in HTML file, since conditional comments work only with HTML, NOT CSS.
    Also I tried \9 hack for font-family option and it didn’t work. I guess most of you know, that Calibri font exists mostly under Windows platforms so I tried to escape it with ‘\9′ for all IE versions, but it didn’t work. FireFox recognized it without a trouble.

  9. Elron says:

    Thank you! just what i needed

  10. eralper says:

    And what about IE 9 ?
    Does it fail as others do ?
    I hope it has better defence than others against CSS hacks.

  11. Chris says:

    The best trick ever!!!!!!! Thank you

  12. This has been really helpful! Thanks for sharing.

  13. Ricardo Zea says:

    \9 sometimes doesn’t work, instead you’d need to use “/ !important”, for example:

    p { font-family: Arial / !important; }

  14. Abijith says:

    This works well..I tried it :) Much handy..

  15. Hasn says:

    Thanks a lot

    You are a legend

    Cheers
    Hasn

  16. Awesome! Still learning about IE, after all these miserable years… :-)

    One more, this will target IE7 only:
    #color: red;

    On the standards-note, I now use the HTML5 Boilerplate (http://html5boilerplate.com/) approach on ALL sites I develop. So much easier, and cleaner…

    Cheers,
    Atg

  17. Smith says:

    Great tips! Thank you!

  18. Srinivas Rompala says:

    this is ok but we need more examples on defferent concepts like compatiability for height ,width,px,%,font etc

  19. For the one that was wondering. The problem with “\9″ is that it targets ie9 and below. To be able to solve this I found that this worked perfectly:

    background: #ee3e64; /* all browsers */
    background: #0000ff\9; /* targets ie9 and below*/
    background: #eee/; /* targets ie8 and below*/
    *background: #00ff00; /* targets ie7 and below*/
    _background: #ff0000; /* targets ie6*/

    So to target ie8 and below instead of using “\9″ I recommend using “/”. My two cents…

    Javier

  20. antony says:

    life saver, thanks

  21. silvia says:

    thanks for the tip! ie 7 and 8 where driving me nutzz!!

  22. BJ says:

    As others have pointed out, this article is inaccurate: the “\9″ hack also applies to IE9/10 (i.e., all IE) – I’ve just tested with various rules in IE v9.0.8112.16421 and the IE10 platform preview (latter via IETester). I *thought* I saw this working previously, so it’s possible MS updated IE9′s handling to be consistent with IE8?

    BTW – Roy/Javier’s “/;” hack (above) is ignored by all IE (6-10, as well as FF) for me, so doesn’t work. UPDATE: Ah OK – it’s meant to read “\0/” but this comment system strips “\0″ (I entered the backslash as & #92;). Interestingly, this ONLY applies to IE8, not IE7 and below, or IE9+ – that means to target IE8 and below you’d need a duplicate rule :S

    Cheers, Ben

  23. Mark says:

    Hi Jeff, thanks for the good articles…Mark

  24. François says:

    Many thanks for this. Much more economic than using hacks like

    head:first-child+body

    Would be nicer not to have to use hacks at all but that’s another discussion

    Cheers!

  25. Márcio Paz says:

    OMG, thanks a lot. I had to say something, I was crazy about it, I didn’t find help anywhere but here…

    God bless you!

  26. Anu says:

    Hi,
    How to make a template working in IE8 & IE9 to work in IE6 and IE7 ?

    I have page rendering issues in IE6 & IE7 and I have many CSS properties to deal with how can i make it work?

    Kindly advise me. I am a college student working on my current project.. Need a help……

  27. jhansi says:

    Hi Scott Ellis,
    I have a problem while i am designing my web page with image as a button, i was created my menu buttons with images and then i keep a link to this image. when i was run it in Mozilla Firefox and googleCrom it seems good but in internet the image is looking with blue boarder.so, how to remove that linking color in internet browser window..

  28. Ricardo says:

    Well, at the moment of typing this comment, April 4 of 2012, there doesn’t seem to be an ONLY IE8 CSS hack.

    The ones I’ve used and seen around are picked up by IE9 as well.

    I’ve used:

    • color: green\9;
    • color: green/;
    • color: green/ !important;
    • color/*\**/: green\9;

    None of these work ONLY in IE8 :(

    • Ricardo says:

      This thing strips out the code, so the examples above are incomplete.

      Hopefully these get posted right:

      • color: green\9;
      • color: green\ 0 /;
      • color: green\ 0 / !important;
      • color/*\**/: green\9;

  29. stupid says:

    None of these work, dont waste your time

  30. Hey,
    New at coding but, I needed a hack of some sort to alleviate an ie issue.
    Your \9 worked great but, it resulted in the problem now occurring in firefox.
    The issue was auto centering the wrapper on the screen.
    #wrapper {
    background-color: #B3EE5C;
    padding: 10px;
    width: 860px;
    margin-right: auto;
    margin-left: auto;
    }
    I placed the \9 directly after auto on both margins.
    Can I have it both ways? correct on ie and firefox?

  31. Suvi M says:

    Hi, I’ve searched advice to this from everywhere without the result:

    I made Joomla-webpage, and template to it with Artisteer. Artisteer made automaticly template.css, template.ie6.css and template.ie7.css. Now I noticed that in ie8 few background-colours have changed from white to blue. I have already made changes to ie6.css and ie7.css so their colours went right, but where do I do to changes now, when I don’t have ie8.css? (And apparently, I cant add new template.ie8.css to Joomla..)

    I would be VERY grateful from answers! (And sorry about my bad english)

Comment Page 2 of 2 1 2

Add a Comment

To add a code snippet to your comment, please wrap your code like so: <pre name="code" class="html">YOUR CODE</pre>. You can replace the class name with "js," "css," "sql," or "php." If there are any "<" or ">" within your code, please search and replace them with: &lt; and &gt; respectively.