All in the <head>

– Ponderings & code by Drew McLellan –

– Live from The Internets since 2003 –

About

CSS Underscore Hack

17 December 2003

Update: note that this article is from 2003. The CSS hack described is outdated and shouldn’t be used.

I learned another CSS hack today – the underscore hack. You can read all about it in detail, but in essence it’s very simple.

Browsers are supposed to simply ignore CSS properties that they don’t understand. This much should be obvious. However, IE/Win does its usual trick of trying too hard to cope with user error and will read and process any valid CSS property with an underscore tacked on to the front. All other browsers will ignore the mystery property. Example:

p{
   color: black;
   _color: blue;
}

All browsers save IE/Win will display the paragraph text as black – IE/Win displays it as blue. It reads the _color property and allows it to replace the one that came before.

I discovered this technique whilst looking for a solution to IE’s lack of support for min-height to specify the minimum height of an object. Decent browsers like Mozilla support this property, but IE doesn’t. Thanks to another IE bug (one that results in overflow being treated strangely), it’s possible to set a minimum height for both IE and proper browsers in a fashion such as this:

div#content{
    height: auto;
    min-height: 400px;
    _height: 400px;
}

Not a new technique, but new to me, and helped me out of a layout problem. Be sure to read Simon’s discussion of the pros and cons. With the appropriate care, it’s a useful tool to add to your hack list.

- Drew McLellan

Comments

  1. § Jesse: ooo new to me too. That could save me a lot of time...

    Is there a good site that keeps track of all these CSS hacks?
  2. § Nathan Pitman: If only CSS included some clever system whereby you could easily define a style for an explicit browser within the actual CSS...
  3. § Brian: Explicit browser CSS... now there’s an idea.
    Just imagine MS making all their pages render white on white for anyone not using IE. ;0)
  4. § Eivind Lie Nitter: Very nice and new to me too. I have really needed this the last few weeks.. but ah well, better late than never.
  5. § Adam: Nathan - Well now, isn’t the whole point of CSS and standards to avoid explicit browser massaging? Hacks are annoying, but hopefully their only temporary. Standards is what we want, man! Standards!

    Having said that, I love the underscore hack. I’ve already used it in several sites. The only problem is that, even though it’s proper CSS, the W3C’s validator doesn’t read it as such. So, some may mistakingly think your code doesn’t validate, but there are certainly worse problems than that.
  6. § Eric TF Bat: I’d love to see something like:


    #sidebar {
    width: 20%;
    min-width: 100px;
    }


    translated automagically into


    #sidebar {
    width: 20%;
    _width: 19.9%; /* fix IE6 flicker */
    min-width: 100px;
    _width: expression(yadayadayada); /* IE5 min-width hack */
    }


    We could treat CSS as an output format! Run it through a compiler before it’s used!

    What a bloody awful idea. Forget I spoke...
  7. § Eric TF Bat: Hmmm... sorry, that should have been w\\idth: 19.9%; /* fix IE6 flicker */ of course. Not that it would matter since the next one would override it... See how stupid these hacks get?
  8. § Nathan Pitman: I agree, the whole point is standards, but we all know that Microsoft are never going to release a totally standards compliant browser, so why not incorporate built in hack methods allowing developers to target specific browsers. :)

    I guess it does send the wrong message though.
  9. § Jesse: IE 5.2 for OS X is probably the best browser MS has made.. Why can’t they make the same quality software for windows as they do for OS X?
  10. § Rad S.: IE5/Mac being Microsoft’s best browser isn’t saying much. IE5/Mac is very slow with the DOM, and it has some very stupid bugs just like IE. The only reason I consider IE5/Mac a better browser is its support for PNG alpha transparency.
  11. § Ric: “IE 5.2 for OS X is probably the best browser MS has made.. Why can’t they make the same quality software for windows as they do for OS X?”

    Hahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahaha (breath in) (breath out) Hahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahahaha

    Jesse, you kill me man… really you kill me!
  12. § oli young: For once, i commmend microsoft for making buggy, non-compliant browsers, at least this hack is elegant :)
  13. § creativehavoc: hey.. i thought of a way that will run through validators too. I was going to use the _hack but my buds would kill me if i had invalid code – so i decided to go with
    @
    .quotebox {
    background-image: url(stucture/images/sidebar_quotebox.gif);
    background-position: top center;
    background-repeat: no-repeat;
    min-height: 96px;
    height: 96px;
    padding: 15px 25px 0px 20px;
    }
    html>body .quotebox {
    height: auto;
    min-height: 96px;
    }
    @
    IE ignors html>body .quotebox { so i can redefine height as auto (back to normal) and let the real browsers use the min-height as they should
  14. § Calum Morton: ie for mac is possibly the worst browser i have ever had to deal with. its horrific and it crashes. the png alpha thing is pretty minor considering hardly any windows browsers detect this, so its fairly useless.

    because of ie i have now been up working on my website project for an additional 8 hours, im tired and bored. if it works in firefox and safari im happy, but i guess everyone uses ie. even on a mac too, absolute madness.

    i really hope i get this underscore thing to work… thanks for the tip
  15. § Rob Jefferson: After attempting to work with a couple of other hacks for this problem I found that this one was Ronseal, did exactly what I said on the tin. Cured about 9 hours of wasted work.

    Thanks Drew, much appreciated.
  16. § Lansing: That man has an aardvark on his head. Oh and kudos on the ’_‘

Photographs

CSS Training Course: 18th July

We're running another CSS course aimed at beginners (or those wanting to freshen up!) on 18th July. Places are limited, so book soon to be sure of a place.

Work With Me

edgeofmyseat.com logo

At edgeofmyseat.com we build custom content management systems, ecommerce solutions and develop web apps.

Recent Links

Affiliation

  • Web Standards Project
  • Britpack
  • 24 ways

About Drew McLellan

Photo of Drew McLellan

Drew McLellan has been hacking on the web since around 1996 following an unfortunate incident with a margarine tub. Since then he’s spread himself between both front- and back-end development projects, and now is Director and Senior Web Developer at edgeofmyseat.com in Maidenhead, UK (GEO: 51.5217, -0.7177). Prior to this, Drew was a Web Developer for Yahoo!, and before that primarily worked as a technical lead within design and branding agencies for clients such as Nissan, Goodyear Dunlop, Siemens/Bosch, Caburys, ICI Dulux and Virgin.net. Somewhere along the way, Drew managed to get himself embroiled with Dreamweaver and was made an early Macromedia Evangelist for that product. This lead to book deals, public appearances, fame, glory, and his eventual downfall.

Picking himself up again, Drew is now a strong advocate for best practises, and stood as Group Lead for The Web Standards Project 2006-08. He has had articles published by A List Apart, Adobe, and O’Reilly Media’s XML.com, mostly due to mistaken identity. Drew is a proponent of the lower-case semantic web, and is currently expending energies in the direction of the microformats movement, with particular interests in making parsers an off-the-shelf commodity and developing simple UI conventions. He writes here at all in the head and, with a little help from his friends, at 24 ways.