Quick Tip: How to Target IE6, IE7, and IE8 Uniquely with 4 Characters
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!