SiteKickr Web Development

Cross Browser CSS: No JavaScript, No Conditionals

You may or may not be familiar with the various methods of forcing non-compliant browsers to conform to your style needs. JavaScript and Conditional IE statements are among the most popular.

This methods however, are both difficult to maintain across a large site with many different stylesheets.

I'm presently considering a method which I have not extensively tested, nor considered any negative effects. But, on the surface, it appears to make sense and provide a much cleaner solution than either of the above.

It relies on your web servers ability to determine the clients web browser. Using ColdFusion as an example, the following gives us the "raw" user agent string.

<cfoutput>#CGI.HTTP_USER_AGENT#</cfoutput>

If we do just a little string manipulation, we can come up with a less clunky string that we can use a a CSS class.

<cfset user_agent_class = LCase(IIF(Find("MSIE", CGI.HTTP_USER_AGENT), DE(ListFirst(Replace(ListGetAt(CGI.HTTP_USER_AGENT, 2, ";"), " ", "", "ALL"), ".")), DE(ListFirst(ListLast(CGI.HTTP_USER_AGENT, " "), "/"))))>

Keeping in mind that this code will be executed with each page request, my goal is to keep it somewhat simple, even if it doesn't cover every possible browser combination. We're trying to deal differences in browser vendors and major releases only.

Given the assignment statement above, we can drop this into a class attribute on our body tag:

<body class="<cfoutput>client-#user_agent_class#</cfoutput>">

Now, we see it coming together! You can now tailor your CSS to a given browser, simply by using the class above, i.e.:

p { margin-top: 0; }
.client-msie7 p { margin-top: 1em; }