Another way to deal with css differences

I wrote about using condition comments to deal with the styling differences between across browsers.  Nikhil Kothari suggests another method:

The trick is to programmatically set the class attribute on the HTML tag based on the browser, and you're done. This is obviously where a little bit of javascript comes into play to do detect the browser along with its version and programmatically set the class value as appropriate.

The css would then look like:

div#foo {
  background-color: red;
}
HTML.IE div#foo {
  background-color: yellow;
}
HTML.IE7 div#foo {
  background-color: blue;
}

While its good to know this approach, I would still prefer using IE conditional comments since it just seem clearer to me. There is a main css for Firefox and separate css files for IE6 and IE7 that only contain the css to address the css differences.

posted on Friday, September 15, 2006 7:29 AM by AlanL

Comments

# re: Another way to deal with css differences

Monday, September 18, 2006 10:45 AM by Eric Cherng
I agree, the conditional comments method seems to be a more elegant way to handle CSS differences than the javascript one. You have one CSS for all css-conformant browsers, then make exception CSS files for non-css-conformant browsers.