Showing CSS to IE only (The Underscore Hack)

January 23rd, 2005 , ,

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

Sample Page

More Information on CSS filtering

John Serris has done an indepth write-up on various other methods of filtering CSS. It is definitely worth a visit:
  1. GUIWOOD.COM » Blog Archive » IE6 Underscore Hack pingbacked Posted December 19th, 2007, 19:37
  2. Showing CSS to IE only (The Underscore Hack) | Likoma pingbacked Posted December 20th, 2007, 23:52
  3. 3by9 » CSS Hacks Are Just A Way Of Life pingbacked Posted May 06th, 2008, 21:08
  4. nic’s blog » Blog Archive » Apply CSS to IE7 only pingbacked Posted August 03rd, 2008, 22:56

Comment pages: « 1 [2]

  1. 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

  2. I love this trick, and miss it in IE7 :)

  3. nice tip, works in IE7 and IE8 beta 2 ( Browser Mode: IE7 / IE8 compatibility view / IE8 with Document Mode:Quirks ( Page Default ) ) also.

  4. it is compatible with ie7 and ie8 in quirks mode as I mentioned above. This means you need a non valid HTML page :( for this to work.

Comment

(required)

(required)

Formatting Your Comment

The following XHTML tags are available for use:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

URLs are automatically converted to hyperlinks.

Textile

Textile is a method that uses simple symbols to quickly write rich text markup. These are the most common:

  • _emphasis_
  • *strong*
  • -deleted text-
  • !imageurl.gif!

Preview: