Abstracting To Different CSS Files
Written By Mike on Feb. 8, 2007.
9 Comments
Report Note
+ Clip This
We haven't begun our blog entries about the new design yet, but one of the things that's different from an architecture standpoint is that we no longer have 1 gigantic (2500+ line) CSS file for the entire site, but about a dozen smaller CSS files for each section. Although the new site has almost double the lines of CSS, each page only loads a small fraction of that to keep the pageload time low.

Scrivs
Written Feb. 8, 2007 / Report /
I was a bit hesitant about this approach, but after seeing how large the site was growing it definitely made things easier going into that sections CSS file and tweaking instead of dealing with something large. Also gave us the ability to style the site at the same time without crossing paths.
isdereks
Written Feb. 8, 2007 / Report /
Breaking up stylesheets is something I need to start exploring. Every second counts right guys?
chris
Written Feb. 8, 2007 / Report /
Yeah, I've done this before for sites that had several "sections" with different design styles. It takes about the same effort to edit the CSS for me, but the cost to the user is much less when they make that first visit to the site. Good stuff, mayne.
MikeP_
Written Feb. 8, 2007 / Report /
Are you planning on suturing sheets back together for delivery and zipping it?
Mike
Written Feb. 8, 2007 / Report /
Well we have one CSS file for "structure" and generic things present on most pages (like the community member list in the sidebar for example) so that's loaded across the site. Then, depending on what section the page is in, it loads 1 or more additional CSS files that build on top of the base styles. The most complicated pages on the site still have less than 1000 lines of CSS behind them, which is more than 2x smaller than the previous site. Smaller pages could have as few as 500 lines of CSS, it all depends on the page.
One thing you want to watch out for though is abstracting the CSS to different files, but still using every line of that CSS on every page. Loading 5-10 different stylesheets per pageload puts a strain on your pageload time because the roundtrip to access the file adds seconds to the total load time the user experiences.
Mike
Written Feb. 8, 2007 / Report /
Mike - Ya know I saw that method and thought it was very cool.... maybe!
MikeP_
Written Feb. 8, 2007 / Report /
Yeah, loading a bunch of smaller sheets just ups your requests; and don't get me started on sending half empty packets. Kinda picky stuff, but you'll send more full packets with one big request then a bunch of smaller requests...
karmatosed
Written Feb. 8, 2007 / Report /
I started mid last year using the multi-page approach and now on any new site use it, if it's large enough. It's so much easier from a management point of view. A lot of work I do uses my CMS system now and the templating on that is also worked around using different CSS and pulling them in for modules that are active. I prefer it as an approach now if the site is a larger one.
Oli
Written Feb. 8, 2007 / Report /
In terms of performance, the entire idea of having as few files as possible is key especially if they're cached in the end.
1 file of 100k is better than 10 files of 10k each if the person is going to need each one, for two reasons:
I do understand the maintenance viewpoint -- large CSS files are a bitch to manage. Why not try keeping them separate but named in a certain order eg:
1notes.css
2blog.css
3etc.css
... and writing a 2 minute C exec or batch file that just echos them together into a production file. You could then strip out all the whitespace (that wasn't between : and ;) and call it a day.