Showing CSS to IE only (The Underscore Hack)
Recently, I stumbled upon (via Mr. van Kesteren) the best CSS “hack” you’ll ever use in CSS sites.
It allows you to show CSS to Internet Explorer only, omitting Mozilla, Safari and Opera.
This is different from conditional comments in the following way:
- It allows you to write conditional CSS directly in your stylesheet file, and doesn’t require inline styles
- It works for IE 5.0, 5.5 and 6.0
- It’s a hack
- It’s very simple to work with.
The hack is called the “Underscore Hack”, and apparently it’s been around for a while. For the benefit of those of us who didn’t know about it, I’m posting it here:
body {
background: green; /* show to Mozilla/Safari/Opera */
_background: red; /* show to IE */
}
Cons
- The CSS won’t validate
- The hack won’t work for IE5/Mac. For this platform, I recommend the IE5/Mac Band Pass Filter hack
Wow, this is one of the best tips I have EVER come across. Seriously, if I only knew this a year ago, days of my life would have not been lost! Thank you for sharing this! Awesome!
I am using in this way: _height:100%; _overflow-y:auto;
…so that I can use position:fixed bottom:0 in IE
I love this trick, and miss it in IE7 :)