October 16, 2006 2

Fixing Cross-Browser CSS

By in Web Development

Please. I beg you. If you are developing any kind of web-based software, read this.

We all know that in magical gum-drop land “standards” like those put out by the W3C would save developers from even having to think about cross-browser development issues. And I’m happy to see that with the modern browsers these days, the standards have gotten us about 80% of the way.

But is 80% consistency across browsers good enough? Hell no! Sure, you can easily make something work in each of the major browsers, but I think we should strive to having our software look and behave identically the same across browsers, wherever possible.

This used to be a much harder debate for me to carry on until I discovered this little trick that I’m about to describe. You’d have to put yourself through agonizing trial and error to uncover how you could structure your CSS so that it looked the same across browsers. I’m talking things like vertical text alignment of labels beside controls, some border treatments, table cells, and so on.

But now, with this little trick, it’s so easy it’s embarrassing. Which means that there’s no good excuse for not using it! I honestly can’t remember where I read it. I didn’t blog about it sooner because frankly, I thought it would make the rounds instantly and I wouldn’t need to (I tend not to report on the same ol’ stuff going around on its own). But no. So here it is:

  1. Check the User Agent string on the server during a page request. You’ll get something like “IE5.5 Win blah blah blah”. Search for UserAgent strings and you’ll find an exhaustive list.
  2. Based on the UserAgent string, add some CSS class names to the Body element in your HTML, like “Windows IE” or “Mac Firefox” based on whatever you can discern from the UserAgent string. You can even go so far as to stick versions in there like “v1″ or “v5_5″.
  3. Now that you’ve marked up the Body element, in your CSS files, when you have a selector like “div.ThisSection {color: red}”, you can simply prefix it with whatever UserAgent based CSS class you added at the server, like “.IE div.ThisSection {color: orange}” to write browser, O/S or even version specific rules. You can even combine them like “.Mac.Firefox.v2 div.Section {color: blue}” to grab specific combinations of O/S, vendor and version specific rules
  4. Now, code your site or app for your browser of choice, and at the end of each file, you can have the 3 or 4 browser specific exceptions that tidy up the look & feel where there are browser inconsistencies.

I find that by doing this AS I CODE (not when I’m all done a site or app), it adds almost no extra development time. If I wait ’til the end of a project, then it’s a royal pain in the butt.

It’s important to do this on the server before the page is served to the client. If you add the CSS classes to the Body element in Javascript during the OnLoad event, the browser will likely render the page first, then re-render it after the OnLoad event, which gives you a little flicker.

It’s a wonderful trick, hack free and wish I could remember where I stumbled across it so I could give credit.

2 Responses to “Fixing Cross-Browser CSS”

  1. Memz says:

    craig, this is a great piece of writing but i’m guessing that it doesn’t apply to website design (or am I just making myself look daft :-( ). I am a university student on a placement year and have created a website for the secondary school which I work for – but it has a problem (though for seem it seems small for some, for me its a big issue)
    My website works great with IE7, just as it should do, but when it comes to Firefox or Safari or any other browser (even IE6) everything is all over the place.
    Any tips?
    p.s. i want to try using the steps you provided but I dont know how to get round this “User Agent String” thing…
    could you help me? please?
    tia

  2. Hi Tia,
    A few of tips:
    > Don’t leave your browser testing for the end. I usually have all 3 open the whole time I’m coding and reload my page in all 3 every time I make a significant change, just to make sure they don’t deviate too much.
    > I would code your HTML/CSS for Firefox first, then fix for IE and Safari. Firefox is the only real cross-platform browser out there and it tends to be a middle of the road rendering engine between some of the extremes found in either IE or Safari.
    > Checking the Agent string doesn’t NEED to be done on the server, though it is better. You can also have a little JavaScript attached to the OnLoad event which will detect the browser and then apply the appropriate CSS class names to the Body tag – however, there may be a brief (milliseconds) shift on screen as the browser first renders the default CSS rules, then runs the script that makes the fixes. If it’s done at the server, then the document will render correctly the first time.
    If you google ‘browser detect’, you’ll likely find scripts in your language of choice that can detect what browser you are running.
    Hope this helps!
    Craig.

Leave a Reply