Using Conditional CSS Stylesheets For Easier Maintenance

Want to help support this blog? Try out Oh Dear, the best all-in-one monitoring tool for your entire website, co-founded by me (the guy that wrote this blogpost). Start with a 10-day trial, no strings attached.

We offer uptime monitoring, SSL checks, broken links checking, performance & cronjob monitoring, branded status pages & so much more. Try us out today!

Profile image of Mattias Geniar

Mattias Geniar, August 18, 2008

Follow me on Twitter as @mattiasgeniar

Because not every browser has a 100% correct CSS implementation, it often requires ‘hacks’, or workarounds to get something working in a variety of browsers. Think Firefox & IE6, Opera & Safari or Conqueror & IE7. Each of those browsers will behave differently when given the same CSS stylesheet.

You can create several stylesheets, and include them depending on the user’s browser. Or you can use Conditional CSS, and keep all your CSS in one file – talk about avoiding triple work, eh?

The idea is simple, yet brilliant. A CSS file that is included will be parsed, server side, for “special commands”. In those commands, you can determine output for IE6, and other output for Firefox. It will keep your CSS organized, as you don’t have to keep track of changing 4+ different stylesheets, whenever you need a change.

Here’s an example, blatantly stolen from their website.

// Conditional-CSS box example 
div.box { 
    width: 400px; 
    [if IE 5] width: 600px; 
    padding: 100px 0; 
}

Be sure to have a look at their usage-guide, it’s definately something worth taking your time for. Feel free to check out their website: Conditional-CSS.com.



Want to subscribe to the cron.weekly newsletter?

I write a weekly-ish newsletter on Linux, open source & webdevelopment called cron.weekly.

It features the latest news, guides & tutorials and new open source projects. You can sign up via email below.

No spam. Just some good, practical Linux & open source content.