Simon Willison’s Weblog

Subscribe

The underscore hack

23rd November 2003

Via Web-Graphics, Petr Pisar’s Underscore Hack provides a new way of targetting CSS rules specifically at Internet Explorer on Windows. As with all such hacks, the pros and cons of using this approach need to be closely examined before deploying it. The hack takes advantage of the fact that adding an underscore to the start of a property name causes that declaration to be ignored by every browser except IE for Windows. However, the hack takes the dangerous step of using one bug to solve another. Peter-Paul Koch explained why this is a risky thing to do in a recent column for Digital Web magazine:

A certain browser has a certain CSS bug. Good to know. This same browser has another bug, usually in its parsing of CSS selectors or comments. This, too, is important information. However, a CSS hacker proceeds to use the second bug to “solve” the first one.

Solving one bug by another is not my idea of keeping Web development simple, but the matter goes beyond bad coding style. These hacks are inherently unsafe.

In an ideal world the next release of the browser would solve both bugs. In an uncaring world the next release of the browser would solve neither. In the uncertain world we live in the next release could solve one bug but not the other!

Therefore you could end up with a hack that applies an extra rule you no longer need, or with a necessary extra rule that isn’t applied any more.

In my opinion, hacks like this are safe for use on sites that are being actively maintained. If you use them in a “fire and forget” project you could well find it breaking in new browsers in a few years time, when the site is no longer being maintained but remains online and broken for all to see. If on the other hand you use it for a living, breathing site such as a constantly changing commercial project or a personal weblog errors that crop up in future browsers can be taken on as and when they appear.

When all is said and done, a large proportion of hacks in use today exist to combat the infamous box model problem—and the best advice for coping with that can be found on Dave Shea’s CSS Crib Sheet: Try to avoid applying padding/borders and a fixed width to an element. Do that, and box model hacks just stop being necessary.

This is The underscore hack by Simon Willison, posted on 23rd November 2003.

Next: Blogmarks

Previous: Status Notification

Previously hosted at http://simon.incutio.com/archive/2003/11/23/underscore