The Underscore Hack

The Underscore Hack is a useful looking CSS hack. IE/Win ignores an underscore (_) at the beginning of any CSS property name, interpreting the declaration as if the underscore wasn’t there. Other browsers do not recognise the property and ignore the declaration. Note that underscores used to be illegal in CSS identifiers and properties, but that was changed in CSS 2.1. (via Mike Davidson)

Posted on November 6, 2004 in CSS, Quicklinks

Comments

  1. The exact wording of the specs is that a leading underscore or dash designates a proprietary extension, so now you can have _scrollbar-face-color (or -scrollbar-face-color, it’s the same): #fff without feeling guilty. Neat.

  2. I’d rather suggest using

    • html foo {…}

    instead of the underscore hack for injecting the rule to IE only…

  3. Damn, it should have been ‘*’ - an asterisk…

  4. Pretty old and well-known in Central Europe, but still useful. There are some doubts about the validity though.

  5. I´ve been using the “*” html foo hack for some time now. Think it works ok, by using the _ hack instead I would save 5 characters per hack. Goddie ;)

  6. November 8, 2004 by Roger Johansson (Author comment)

    I’ve been using the “*” html hack too. Not sure if I’ll use this instead — I just wanted to make a note of it.

    djn: You may still need to feel guilty for messing with your visitors’ browser UI ;)

  7. Unfortunately, the underscore hack makes the W3’s CSS validator throw an error or two. I’d prefer not to use that one.

    I’ve had success with “* html”, “html>body”, and “w\idth” used together or separately.

  8. The underscore hack has a step-brother, asterisk attribute, i.e. asterisk margin. We’ve gotta watch these things with IE7!

    Start avoiding the good ol’ ‘*’ html filter, that is certainly going to be ignored by IE7.

Comments are disabled for this post (read why), but if you have spotted an error or have additional info that you think should be in this post, feel free to contact me.