At one of my previous jobs we had a massive amount of CSS to handle; we had the global styles, then section/page specific styles and on top of both of those we had different styles for various versions of the application (as the same application and content ran many sites).
The solution we came up with was not too dissimilar to the one discussed in this months A List Apart article Progressive Enhancement with CSS and that article is well worth a read. But I also thought I’d write a little about the setup that we came up with and how it worked out for us.
The basic setup was as follows:
- Split the styles into their three components of the CSS make-up: layout, typography, skin (with the skin file containing the colour and graphics). As this kept things nicely compartmentalised and meant that we could re-use some styles from one site (e.g. the layout generally was the same for most content across all the sites) while applying extra changes specific to that site.
- The stylesheets were organised into directories based on the section (or sometimes specific page) of the application. So for example home/layout.css, home/skin.css, news/layout.css and news/skin.css etc.
- We could then also import specific stylesheets for each of our sites on top of these (both at a global and section level) which usually just overrode a few style declarations to keep these site-specific style rules to a minimum.
- All of this was managed server side using a configuration schema which knew where to look for the stylesheets for a given section of the application (as well as the global ones) and include them as appropriate for the given site.
We also had common comment styling for breaking the larger stylesheets into sections (which makes it easier to find things) and tried to alphabetise our properties within each style rule (although it’s not something that I personally could always stick to).
This was quite a good system, it may have looked complex based on the number of stylesheets that were imported for any given page but it worked out really well and we never really had any problems with our CSS system.
I’ve even used it on a few of my own larger side projects with minor tweaking to the system (e.g. dropping the configuration schema and following a Rails-like "Convention over Configuration" setup), and as I say it is very similar to the one described in the A List Apart article Progressive Enhancement with CSS — that has a really good idea for handling the different media types, which we handled via our schema.
Comments
There have been 4 comments so far, join the discussion.
1. michael - 11th Dec 2008 - 11:57 am
I always wondered where I picked up the habit of alphabetising my style rules =D I too have tried to replicate this CSS system a few times using plugins to analyse the current URI.
2. Lewis Litanzios - 31st Jan 2009 - 9:54 am
Hey Dave! Hope you’re well. Just getting back to my blogs here, after a bit of a break.
CSS organisation has been big with me over the past few months. Take a look at this (http://tinyurl.com/cekzv5). I’ve found, working on top of Andy Budd’s suggestion of splitting your CSS into categories, that using indentation, rather like writing XHTML, is very helpful too.
@importing CSS is extremely useful, but I find having CSS in separate files is too long, and, rather like pulling in a lot of JS files, it’s slower.
Hope that’s useful.
PS. That first link is going down soon, as it’s at a beta location ATM, so apologies if it doesn’t work when you read this. It’ll be live for the next week I would think anyway :]
3. Dave - 31st Jan 2009 - 11:30 am
Hi Lewis,
Yes CSS organisation can be a trade off between keeping things neatly organised and having only a handful of HTTP requests to get the CSS. When working on highly trafficked websites I think it is useful to optimise this by having either a build script or a server side script that combines your nicely encapsulated CSS files into a single CSS file to reduce the number of HTTP requests – as opening a new request actually has more overhead than you’d think.
On a separate note, I just took a look at your site – loving the new design, especially your info bubbles and some of the hidden bits/easter eggs.
-D
4. Lewis Litanzios - 31st Jan 2009 - 11:57 am
I use PHP include()’s with all my Javascript, perhaps I’ll come round to your way of thinking with the CSS soon enough :P
Thanks for the compliments. Yea, it was [that] site (I started it in March 2008!), and the intricate CSS going on, that made ‘CSS organisation’ big with me.
All the best, will speak soon no doubt. I’m keeping Saturdays free for blogging, now I’m single again :]
Leave a comment
No HTML please, only textile. For code please use [lang]...[/lang] tags (e.g. [html]...[/html] for HTML)