About Us

Bulletpoint StarImulus® is a technology focused design + interactive agency.

In addition to our client services we also have a few products in the works. Our office is always filled with chatter and this blog is an outlet for our creative energy, rants and ideas.

Podium

StacksStacks ®
A group task management solution that finally makes sense.

Sign Up For A Free Trial »

Featured Project

Nov11

The problem with using multiple CSS files for layout.

CSS in one fileEarlier today I was reading an A List Apart article titled Progressive Enhancements with CSS. The main idea behind it being that breaking out styles into a multitude of files is beneficial. For example, creating a typography.css for type styles, a layout.css for positioning styles, and a colors.css for colors and graphics. At face value this sounds great because abstraction, for the most part, works well on computers (utilizing folder hierarchy’s, categorizing types of media, tagging web articles, etc).

Fine and dandy. But there seems to be a real world problem.

There are three big reasons I see not to break out CSS into multiple files.

First, when you break out CSS into multiple files you are forced, no matter what to write a lot of duplicate code. For instance, if I want to have a certain container have a typeface, background, and color in a single main.css file I can do the following piece of code:

#element {
     position: relative;
     float: left;
     width: 20em;
     color: #f0f;
     background: url(/images/background.gif) repeat-x 0 0;
     font: 120% Arial, Helvetica, Verdana, Sans-serif;
}

However, if I break this out into: typography.css, layout.css, and colors.css files I have to do the following:

/* typography.css */

#element {
     font: 120% Arial, Helvetica, Verdana, Sans-serif;
}

/* layout.css */

#element {
     position: relative;
     float: left;
     width: 20em;
}

/* color.css */

#element {
     color: #f0f;
     background: url(/images/background.gif) repeat-x 0 0;
}

Okay, pretty easy to see that the amount of code being used favors having one central CSS file. Now, let’s talk about style management and the second argument I have for not using multiple CSS files.

A single CSS file for a website can grow to be quite a large file. Most I’ve written fall in the range of 1200 to 2000 lines. The problem with this is that making small changes can be a bit of a hassle. However, I would argue that the single best way around a complicated CSS file is to clearly comment code, use shorthand css, and make sure there is good style structure. I.E. Don’t go styling something in two different parts of the file and don’t write five lines of CSS when one will do.

But, in no way is the answer to break the CSS into multiple files. Why? Because the worst possible thing you can have to do is deal with the above problem three different times. Granted, if you only have to make a small color change than you only have to edit one file. But, if you use only one file to begin with then you only have to edit one file anyway.

Last, there’s a reason a site like digg loads in one 2500 line CSS file. The answer is that the less page requests the better. Doing multiple page requests to get different styles that are separated is inefficient. If a site gets a lot of traffic or a large traffic spike the less page requests the better. Granted, this may not be common but when it happens there’s nothing more important than trying to keep the site up.

Want more assurance?

Neither Dan Chederholm simplebits.com, Veerle Pieters veerle.duoh.com, or Jeff Zeldman zeldman.com use broken out CSS for type and color.

It has to say something when the big wigs in the bizz don’t follow the advice of A List Apart eh?

Exceptions?

As always, there are a few exceptions to this. For instance, if the website is like MTV.com and has a constantly changing color scheme then it could be useful to break out individual styles into a separate CSS file. One that overwrites the default styles of the base design and can be updated without disrupting the primary styling of the site. Also, microsites that have completely different layouts from their parent sites almost always deserve a new CSS file.

Conclusion

Basically there’s no need to break out your styles into a multitude of files. While the idea of abstraction might sound good the benefits just don’t add up. All you really end up with is a waste of time and resources.

posted in: CSS, productivity, rant, web development

This post was published on Tuesday, November 11, 2008 at 11:03 pm

Leave a comment


Comments

1

h3

November 12, 2008 at 9:53 am

I’ve been splitting up my stylesheet but only for really big projects, most of the time web applications.

I don’t split them up in term of typography, layout and color. I think it would be silly, a huge waste of time and a maintenance nightmare.

I’d rather split them up by component, ex: events.css, datepicker.css, calendar.css etc..

One of my biggest project has more than 30 stylesheets. If you ever find yourself including 30 stylesheets on a web page you’ll notice that you don’t need a huge traffic to trip off your server. This is an instant performance killer because the server has to spawn a threads and create a socket connection for each stylesheets. More than 30 on each requests that is.

We’ve finally opted for simple solution. We load them and compress them dynamically.

We use the django, so in the template it looks something like this:

{% loadcss
“events.css”
“calendar.css”
“datepicker.css” %}

The loadcss method does three things, first it check if debug mode is on, if yes it will load all the CSS files separately like it would normally because, well, it’s easier to debug..

In production mode all the CSS files are globed into a single file, cached and then served.

We’ve done that for JavaScript files too, it works pretty well.

2

Bruce

November 14, 2008 at 3:55 pm

H3, thanks for sharing this tip, it’s an awesome suggestion. We’ve also recently started to utilize some widgets that have separate CSS files (datepicker, calendar, etc.) that I think should be separated out. However, if we get to the point of having more than 5 loads we’ll look at moving to a solution similar to yours.

Awesome.

3

Trevor Green

June 20, 2009 at 10:35 am

I would tend to agree with your assessment in a single user environment, but I’ve recently started considering how you deliver styles to a client in a project of any size where you don’t have control over the end product.

Where you have comps to deliver but the resulting site is not under your control. How do you deliver page styles that are going to function as they are intended without full control. And then how do you effectively update and deliver those styles to the end product.

It that case I think its essential that you do the additional coding work work in order that the end product doesn’t end up being hacked.

I’m think you should deliver:
normalize.css – recommended normalization
pagelayout.css – page template css
page.html – page html
(classes).css – all custom styles for different areas.
class.html – html for displaying classes ( in template or outside)
layout.css – layout changes.
font.css – font changes.
colors.css – color changes.
(or one file for all three that the developer can edit)

In the classes.css file you would include your .class style and a .class normalization that enforces the normalization rules that you recommended, so that if the developer wants to cherry pick a given element you can be assured that your settings don’t get hosed.

The layout.css, fonts.css, and color.css should be used by the developer to tweak the styles on the page with their recommendations and then potentially passed back to the designer and then back through the style builder.

If the end result is that you compile the whole deal into one file that is great, but I have always felt that mutliple css files for development (when the is more than one person involved) is the only way that makes sense, and that a lack of good server components to compile and compress your css for performance is the true problem. And the ultimate solution.

Making your code incomprehensible to others and/or spending a bunch of time creating non-standard commenting that you think conveys what you’ve done seems wasteful. You have a class or id that your style should reference and a recommended piece of html that it applies to. Additional comment should be unnecessary at that point.

Granted, with the appropriate comments breaks a single file is as good as multiple files if you have everyone on the same page and they know where they should make their edits and no one is coloring outside the lines. Unfortunately that never seems to be the case. As everyone has their own idea of how things should be done.

I also imagine a time when styles are abstracted even further and exist in the cloud under management of the designer and the developers are forced to make their tweaks in a separate file for approval or disapproval by the project manager and the resulting file that is served is a single file but is custom generated and cached on a per page basic for maximum optimization.

Geez. Now I’m just talking crazy.