When to Split Style Sheets
Post from March 5, 2009 (↻ January 5, 2017), filed under Web Development.
This and many other posts are also available as a pretty, well-behaved e-book: On Web Development. And if you’re only interested in a robust overview on coding guidelines, check out my little book on it: The Little Book of HTML/CSS Coding Guidelines.
Three factors influence whether or not it makes sense to split style sheets: probability, meaning (aka semantics), and granularity. Thinking aloud:
Probability has to be considered for all web development decisions. When it’s about splitting up and using separate style sheets, probability is important to e.g. estimate the likelihood of the style sheet to get (too) big, the likelihood of the rules in question to be only temporary, and the likelihood of more people working with the style sheet.
So if you’re dealing with one style sheet but anticipate it to be growing a lot over the next few months, split it up in meaningful chunks. If you’re just adding a couple of rules for testing, don’t split it up. If you’re the only one working with a style sheet, you might be familiar enough with it to deal with a big one.
The meaning of style sheets or style sheet sections is decisive for split-ups as well. It can make sense to e.g. separate a part of a style sheet that just deals with forms, as forms can make up an own “unit of meaning.” It probably doesn’t make much sense to split that form style sheet up into two style sheets, unless it’s very big to form other meaningful chunks, or includes other, semantically different types of forms.
Also, there’s granularity. You could create a style sheet for every single CSS rule, even rules that just consist of one declaration. That doesn’t make sense because it’s too granular. You could also put all your corporate identity into one big style sheet, saving a couple of HTTP requests and leveraging caching, but understanding and maintaining 15,000 lines of CSS code of which maybe just 200 are relevant for each request make that approach questionable, too.
(It’s all about the thresholds set for these three variables. Give them good thought.)
An example, you might prefer absolute certainty before adding something to your core style sheet, think that all border styles are a good, meaningful thing and should go into another style sheet, and that you don’t mind fine granularity at all. This could suggest that you’re using a main style sheet, have five others defining fonts and what not, and even accept Conditional Comments. (The maintenance penalty for these particular decisions can be costly, however.)
The way I usually treat these variables is a mix of not worrying too much about volatile conditions, thus going for standalone style sheets that might have a “test” section in them. I only (but then very much) split style sheets by meaning when otherwise more than 50% of the rules of the overall style sheet would not be needed in there. And overall, I don’t mind large granularity up to, say, 500 different declarations.
I’m taking this approach because I find it more efficient: On the one hand, separating style sheets means more HTTP requests (unless you have a system in place that combines or “builds” them). On the other hand, style sheet size is not decisive for understandability; this largely depends on CSS organization, like reasonable use of modules or comments.
About the Author
Jens Oliver Meiert is a technical lead and author (sum.cumo, W3C, O’Reilly). He loves trying things, including in the realms of philosophy, art, and adventure. Here on meiert.com he shares and generalizes and exaggerates some of his thoughts and experiences.
If you have any thoughts or questions (or recommendations) about what he writes, leave a comment or a message.
I used to split stylesheets but kept finding myself in odd situations where I couldn’t really classify whether it was a structural or presentational set of declarations. It backfired quickly.
Suturing them together at build time for big sites seems to be the way to go.
We have developed our own CSS framework which we use to build our applications and websites. We’ve ordered them logically, using internal conventions. We love using our framework, because our team always knows where to look for a certain setting. This makes working with a team much easier.
As for the amount of stylesheets: in our case it doesn’t matter. When the application / website is live, everything is merged together and presented as a single optimized stylesheet and based on the User Agent.
For whatever reason I have found myself splitting stylesheets up into layout.css and text.css. I think having all the typography in its own file makes it easier for me to work with and read in. Ideally I would combine these before the site goes live, or have a system in place to combine them automatically, but that usually isn’t the case.
When using a style switcher with for instance an alternative with higher contrast, I find it useful to have one ’shared’ file for structure, one for type and one for colour & images. That way you’ll only have to serve (and edit) one file at the time. But still, the problem exists on what to put where. When for some reason using line-height instead of a padding, does it change from typography to structure..?
@Judd I have totally had that problem lately! I am ALWAYS experimenting with the layout but the typography and colors (two distinct stylesheets) have some overlap.
Here is a system that I have recently started using. Um, it’s a bit anal.
If you already split your stylesheets into separate files such as layout.css, text.css etc. you could just as easily combine all of those in one file and just separate them within the larger stylesheet. I like to place a table of contents at the top of my stylesheet and then number my sections with comments. I prefer to just include everything in one.
I like to keep as much as I can in one stylesheet, but within it I separate things in three parts; typography, structure, and font stacks.
I do like Tom all serperated into 3 files typography, structure, and font stacks, this gives a clear and sorted structure - but i think this works only for small or middle sized projects.
keep on seperating…regards
On March 8, 2009, 17:29 CET, Jonathan said:
Arg, your article seems interesting, but come on… All small-CAPS ?!!
My eyes hurt 😔
The dicision on which rule belongs in which stylesheet is mainly made for the developer in conventions. Everything else is decided on the go by the developer. Then again, the consequenses of placing a CSS-rule into a ‘wrong’ stylesheet, are nihil anyway.
The “based on the User Agent” line was basically pointed at IE. We’ve written some server-side scripts (PHP) that enhance user experience in IE (mainly IE6 and lower). IE6 has a hard time rendering modern websites, so we’ve decided to do some of the rendering work on the server if an IE-user visits the website… It’s way more detailed ofcourse, but that would go offtopic 😉
I normally do not split them. I always work alone on projects (when it comes to css) and I always find my way in my css file. But I can imagine when working with more people things need to get more structered and it might be handy to split things up into for example navigation, content, etc…
I agree Thomas, I’m fine working myself on a single css sheet. But when I asked my bro to help me out with something it wasnt the style he was used to working and I would have been aswell just taking the time to do it myself.
As a web developer of 10 years++ experience my advice is this; If things are starting to get messy in your CSS, then it’s time to separate out the styles. Give your separate style files (and sytles), sensible names, the idea is to help yourself remember what is where at a later date (That’s the whole point of CSS). Basically, it’s down to the developer, the complexity of the site and whether or not someone else will be expected to come along and start working with the design at a later stage.
I find that even the most complex of designs can be dealt with in one CSS. But maybe that’s just me.
Nice article BTW.
On May 28, 2009, 0:15 CEST, Glen said:
Although some of these comments contradict each other, I have to agree with them all. If I’m working on a clients site and the single style sheet is massive, it does a while to get used to. But if it’s my site, I can work with one sheet no problem. I guess it just comes down to what you’re used to. A more structured approach would be needed if working as part as a team though.
On May 28, 2009, 10:33 CEST, Virginia said:
Thanks for the interesting article. This is sure to help many people. Over the years I have found that web design (CSS included) is an art rather than a science! In the real world when you get down to the nitty gritty of design you tend to just go with what you feel (but based on a proven methodology for sure).
On May 1, 2010, 2:45 CEST, Mark said:
I think splitting only becomes necessary (or even useful) when sites become highly complex and have teams of developers. Even then I don’t think splitting is done so much out of productivity as actual necessity!
On February 8, 2011, 5:01 CET, Martin said:
I always aim for one stylesheet wherever possible. It minimises http requests and helps a little in speeding page load times.
You could alternatively use a caching plugin though that minifies css.
Have a look at the most popular posts, possibly including:
Perhaps my most relevant book: CSS Optimization Basics (2018). Writing CSS is a craft. As craftspeople we strive to write high quality CSS. In CSS Optimization Basics I lay out the, at least some of the most important aspects of such CSS.
Looking for a way to comment? Comments have been disabled, unfortunately.