May 15th, 2006

I Vote Conditional Comments

Many developers agree that CSS hacks are not the way to go. Personally, I am on that side of the fence and have been for a long time. I will spend time reworking the style of a document so that it will be cross-browser compatible with no CSS hacks in place. I do this because using CSS hacks is taking a step back.

Whats Wrong with CSS Hacks?

CSS is supposed to enhance the organization of your document and help as much as possible with maintenence. With the release of a new browser (read IE7) you should not have to worry about site compatibility. I think for those who supported CSS hacks in the past, the release of IE7 will help enforce the fact that they are not such a great idea.

CSS hacks make your style unstable, unclean, and difficult to manage. When a stylesheet is bombarded with CSS hacks it no longer looks like an effective document, but more like a jumbled foreign language. I feel that it would be better to have two separate CSS documents written as intended — hack free.

What is an alternative?

In my personal opinion, I think the way to go would be following Microsoft’s advice and giving Internet Explorer the crutch it needs using conditional comments. I think it is the cleanest way to handle IE’s shortcomings while keeping your primary style clean and organized.

Using conditional comments allows you to write your primary stylesheet for an upper level browser, and then write a separate stylesheet that will only apply to IE via conditional comments. For example:

<link href="style.css" type="text/css" rel="stylesheet" media="screen,projection" />
<!--[if IE]><link href="ie_style.css" type="text/css" rel="stylesheet" media="screen,projection" /><![endif]-->

As you can see, using this method would allow you to first develop in a higher level browser, and then compensate for Internet Explorer using a separate stylesheet which would be applied after the original, and compensate.

Microsoft has also provided a way for version control, which can come in very handy when making adjustments for a new version.

But There Are Already IE7 Hacks

I am dissapointed that people actually put time into researching IE7 hacks. That article was released with the understanding that hacks were not the way to go, but the information is included anyway.

Perhaps it is a good way to look into the pitfalls still present in the latest version of our favorite browser, but that is the furthest the information should go. Unfortunately I see it being used time and time again until the next release of Internet Explorer.

 Comments

9 Comments

  1. jammodotnet May 15th, 2006

    i wholey agree with you on “CSS hacks make your style unstable, unclean, and difficult to manage”.

    when i visit a site that i admire and i want to see the underlying code, many times im turned OFF by their sloppy code.
    they are usually well commented, but with TOO many hacks :(
    and i dont understand ALL the hack methods to get IE to do what i want, so i dont get … WHY use hacks in the first place?!
    there will always be a better, faster, more efficient style to come along.

  2. Fredrik Wärnsberg May 15th, 2006

    If you just use a hack do import a special css-style for one specific browser (like dave shea usually do) then I actually think that it can IMPROVE document structure.

  3. P.J. Onori May 15th, 2006

    Boy, what a pain IE has caused us. Seriously, it’s a lose-lose situation as we either need to write entirely separate CSS files for the browser or use crappy hacks that may not work in the near future.

    IE has caused so much trouble for us. If only the public knew…

  4. Pedro Rogério May 16th, 2006

    Of this form it is better why you can work the styles each to browser better. My congratulations!

  5. patrick h. lauke May 24th, 2006

    i like my markup to be clean. i can’t be bothered to taint it with browser-specific functionality (masquerading as a harmless comment to other browsers). “hey, we still haven’t got our CSS completely right, but we fixed the things you used to exploit for hacks. but don’t worry, just use our proprietary markup to work around that”. pah!

    and the whole point with hacks being fixed in later versions: if the browser also fixes the reasons for which you used the hacks in the first place, there is absolutely no issue here whatsoever.

  6. David Hammond May 27th, 2006

    Hi, I’m the author of that blog post with the new IE7 CSS hacks. I was not specifically trying to find new hacks. I develop a set of web browser standards support tables and found various new bugs when testing Internet Explorer 7 support for CSS. The hacks were natural products of that research, and I figured I might as well publish my findings along with a disclaimer rather than wait for someone else to publish them without.

  7. Taking Conditional Comments Too Far - Monday By Noon July 23rd, 2007

    [...] it’s otherwise impossible to get Internet Explorer to behave desirably. To me, they’re much more acceptable than unstable CSS hacks or extensive !important declarations. They’re much easier to maintain, and it’s the [...]