Most of us have been around CSS for long enough to know the good and the not-so-good of the business. Many of us have been frustrated at times at the behavior of seemingly straightforward in unruly browsers, namely Internet Explorer. As a result, the web development community has found numerous solutions around these problems. CSS hacks have received a lot of attention in the last two years as a relatively safe way to tame the problems these browsers present to us - but is it really a good idea?

A Brief Introduction to CSS Hacks

For those who don’t know a lot about CSS hacks, here’s a simple explanation: they take advantage of browser bugs (or browser “weaknesses”) to hide certain styling rules from specific browsers, using special characters/words that are interpreted differently. The use of these hacks is a subject of much debate in the blogosphere these days.

Various standardistas and web designers such as Roger Johansson and Peter-Paul Koch have spoken out against the use of CSS hacks, saying conditional comments are the way to go. In my opinion this the best option for every designer who needs to assign different CSS property values for different browsers. It rids our stylesheets of the messy hacks and workarounds used to tame the pages, and it helps to organize and streamline our CSS.

Code for the Future

Abstaining from using CSS hacks is the way to prepare for the bright future of CSS. This was mentioned by Roger Johansson a while ago in an interview that was posted on Fadtastic, and I want to reinforce that point again. In this uncertain world we code in, there’s really no telling what bugs will be fixed and what bugs won’t be. There must not be any place in the semantic web for a mess of “!important” hacks and weird selectors.

I think it’s pretty obvious that the clean, straightforward stylesheet (with no hacks) is the ideal setup, and the closest we can get to this for the time being is conditional comments. This is for the time being, leaving room for browser improvements and less hassle when cleaning up our code to comply with the new breed of browsers.

Do conditional comments fall into the “hacks” category? If you consider a CSS “hack” to be anything that gives special instructions to certain browsers, the answer is yes. But since conditional comments don’t utilize one browser bug to fix another one (as all real CSS hacks do), they can be put into a category of their own.

Conclusion

Finally, I’ll say that every web designer should use CSS hacks as a last resort only, and not as common practice. If your pages have a rendering problem in Internet Explorer, spend as much time as you can searching for a semantic solution that doesn’t use a CSS hack to solve the problem. If this fails, use conditional comments to correctly apply the proper rules to the misbehaving browser. Keep hacks out of your code, and you’ll set an example for the design world to follow (and save yourself some grief down the road).