Combining CSS files: per site or per page template?

Viewed 690

We all know that we're supposed to combine our CSS into one file, but per site or per page? I've found pro's and cons to both.

Here's the scenario:

  • Large site
  • CSS files broken out into one file for global styles and many for modules

Solution A: Combine ALL the CSS files for the whole site into one file:

Best part is that the one file would be cached on every page after the initial hit! The downside is that naming convention for your selectors (classes and id's) becomes more important as the chance for a namespace collision increases. You also need a system for styling the same module differently on separate pages. This leads to extra selectors in your CSS which is more work for the browser. This can cause problems on mobile devices like the iPad that don't have as much memory and processing power. If you're using media queries for responsive design, you're troubles compound even further as you add in the extra styles.

Solution B: Combine one CSS file per page template:

(By page template I mean one layout, but many different pages, like an article page) In this scenario, you lose most of the issues with selecting described above, but you also lose some of the cache advantages. The worst part of this technique is that if you have the same styles on 2 different page templates then they'll be download twice, once for each page! For instance, this would happen with all your global files. :(

Summary:

So, as is common in programming, neither solution is perfect, but if anyone has run into this and found an answer I'd love to hear it! Especially, if you know of any techniques that help with the selector issue of Solution A.

2 Answers
Related