Scott Isaacs, one of the Microsoft web chaps, is complaining about CSS hacks. His proposed solution is to, instead of applying CSS hacks to make a browser do what you want, instead have details of the users browser inserted into the HTML element either by server-side or client-side script. So, if you visit a page using IE6.0, it would add class="ie m6 d0 win" to the HTML element; using Firefox 1.06 on Linux it would add class="firefox m1 d06 linux", and so on. Then, in your stylesheet, use these hooks to apply style for specific browsers.
I think that this is not a good idea, and here’s why.
Firstly, it requires absolutely that every page be server-parsed, since you can’t rely on client-side scripting being there. Well, unless you’re prepared to make your styling depend on JavaScript, which rather defeats the point of CSS. In some places it seems like the norm is to make all pages, even ones with plain HTML and no dynamic content at all, be server-parsed by making them ASP or PHP, but I don’t like to do that for pages that don’t need it. If you’re thinking “but every page needs a header and that’s normally dynamically included!“, think about automatically generated pages which are generated dynamically when required and then cached, like blog archives. Or think about mailing list archives, or pages where the header and footer are added by your web editing tool like Frontpage or Dreamweaver rather than by the server.
Secondly, it clutters the HTML a bit. Not much, but a bit. This isn’t much of an objection.
Thirdly, how do you establish the platform and short-name-for-a-browser and major and minor versions? Parse the userAgent string? That takes us back to the days of browscap.ini and The World’s Biggest List Of Browsers, which you have to rely on someone to maintain.
In short, I don’t like the idea. I’d be interested in hearing dissenting opinions, though.
(Incidentally, you may be thinking that this should have been a comment over at Scott’s rather than a post here: I agree with you, but to post there you need an MSN passport and I don’t want to get one.)
Yep I’d agree with you on this one.
CSS Hacks should be avoided that’s a given, but to have the server create custom code for each user-agent is not something I’d be likely to use.
What happens when new user-agents that aren’t accounted for are visiting a site based on this method? If they aren’t detected what do they get served?
I would also concur that dealing with hacks in Scott’s method does create issues on the server parsing front and I wouldn’t want to use this method as it would mean I couldn’t have completely static cached content!
Personally I find that the need to use hacks for FireFox/Opera etc. is becoming less and less and I’m mostly having to use hacks for IE. When that becomes necessary I use conditional comments to show those hacks to the IE versions I want to effect.
This means when IE7.0 comes along it won’t see the hacks I’ve got in place for IE 6.0 and below and I can simply leave it at that or create a new IE7hacks.css for IE7 assuming it will support conditional comments in the same way.
Even if I need to hack for other browsers then I would probably still look to using a separate file. That way it becomes clear and makes the site easier to maintain in future.
Before I even get to the point of doing the above I’ll check that my hack is required for an actual rendering bug and not one caused by the differences in default styles or bad coding! In the past I was guilty of the bad practise of hacking everything that wasn’t rendered the same between browsers. Now I reset the default styles with simple rules before I start.