views:

216

answers:

12

I'm wondering if it's better to make one or several files for CSS files ?

I always see websites with a plenty of css files, but it seems better to use only one large file.

What's your advice ?

+17  A: 

Performance wise, you are better off with a single file, as it results in one connection and request to the server (these tend to be expensive operations, time wise).

This is why minifying frameworks exist, that merge together all the CSS (and JavaScript) files for each page and serve them in one request.

Oded
I fixed a typo and some casing. I hope you don't mind. As for the answer... absolutely correct. :)
Gert G
@Gert - I always welcome such corrections. This is one of the things that make SO great :)
Oded
+1  A: 

Well, the same as Yahoo!'s: Use one to reduce the number of HTTP requests.

musiKk
+2  A: 

Different HTTP requests are hardly the bottleneck here, file size ultimately is. The reason it's best to split things up as much as possible is because if you want to change a certain thing of your site's feel, let's say, the font of all headers, you want to change one file / setting only for that, and want that file to be as small as possible.

For large and encompasing CSS, I would make different CSS documents for all different things like the layout, the treatment of classes, and so on, another advantage is that if you've multiple pages that need a slightly different look from the main page, they only have to link to one other CSS file, not to a completely different one, the majority they an share.

Lajla
Yes, once all css files have been loaded and cached in the browser, it only needs to load the small one that has actually changed instead of loading everything.
Martin Wickman
Indeed, a thing I had yet overlooked, it also doesn't have to reload a lot after updates or if some parts of your site use a slightly different CSS.I do this just for organisation though, but I usually have my CSS split up between frame.css (placing) colours.css, fonts.css et cetera.
Lajla
I would disagree with this. The overhead of HTTP connections in most cases *vastly* outweighs the amount of CSS you're downloading. Even a 30k css file would see up to 50% of the total download time as request overhead, splitting it up would only increase that.
womp
A: 

Multiple files are good for organization, but one request to the server is definitely best. If you watch the performance videos from Google they suggest the least amount of HTTP requests possible. Each HTTP request has overhead in the handshake that you do not want to incur if you wish your site to be fast.

Check out this great script which will take your multiple CSS/JS files and turn them into one file:

http://code.google.com/p/minify/

TomWilsonFL
+1  A: 

For the quickest download and rendering of a page, the Yahoo performance rules are correct. You want as few http requests as possible.

However, on many sites, it's simply not convenient to have a single large CSS files. Your best bet is to organize your CSS into as many files as you'd like, and then use a server side script to concatenate the files. GZIP'ing that file goes a long way, too.

Chris Henry
One thing to remember to minify the files before the user ever gets there. Minifying on page load can often times be slower than just loading several files would have been.
GSto
Absolutely, I normally use http://groups.google.com/group/minify to deal with minification, and it creates a cache of concatenated, minified files, and serves those, so it's never done on the fly.
Chris Henry
+1  A: 

you should use more than one css file rather using one big file. It helps you while maintaining your site also use different definitions (classe or id names) in different css otherwise it will take the one which declared later.

But for performance reasone you can use one large file because, One large CSS file leads to fewer HTTP requests, which can improve performance.

Several smaller files leads to easier organization which will make development and maintenance cheaper and easier.

Salil
+2  A: 

Use not too much different css files or at least try to put them on other domains to speed up downloading them by the browser. I also suggest you use a minification tool.

Grz, Kris.

XIII
+2  A: 

Having one CSS file doesn't just help with HTTP requests, it will also give you better compression (compressing one big file should give you better results than compressing multiple smaller files).

Brendan Long
+1  A: 

If performance does matters to you

Then

If your site is small but gets huge traffic then go for one css file

if site is small personal or business sites then but with less traffic then go to multiple css


If CSS files maintainability does matters to you

Then

If your site is small with less different pages then go for one css file.

if site is big then go for multiple css http://www.killersites.com/blog/2008/how-to-organize-css/


HTTP request of CSS files will not make big difference in performance of small site.

metal-gear-solid
+3  A: 

My strategy on this is simple. I separate production from development, both in CSS files and in JS files. in development, I can have up to 20 JS files and 10 CSS files, organization is super slick and easy, I always know where everything is.

In production, all files are minified into 1js and 1css file, changes are always made in development and then "staged" to production so I gain the maintainability of the application and the performance in production.

I use Yahoo minifier to minify my files but you can use whatever is convenient for you.

Good luck

Avi Tzurel
+1. This is the approach I've taken, and it's the best of both worlds, great maintainability/organization and great performance. The only exception I make to this is pulling jQuery from the Google server, as it's often already cached on people's machines.
GSto
A: 

Weigh it up.

The advantages of one CSS file

  • Reduced latency. Each downloadable component comes with a small amount of latency. Less files => less latency
  • Single point of compression

Advantages of multiple

  • Change one file won't require all css to be re-downloaded. The other css can be served from the cache
  • Structure
  • Only download what you need. If you don't have any forms on your page for example you don't need to download forms.css
Duncan