Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are we not using one CSS file instead of many CSS files?

I recently stumbled across a project that has 27 different CSS files used by the same theme, and they are for specific sections in the application, rules split like this: there's a CSS file for the menubar, there's two more files for contact forms, another one does the footer, specific-page formatting, two more for galleries, so on.

So I asked lead what's the reasoning for so many CSS files and if it's safe to concatenate.

His reply was that all files sum 126KB, bandwidth matters and there's 500+ CSS rules, so best not concatenate to prevent selector collisions.

I know that 126KB of uncompressed CSS files is an awful lot, but I also know (from the best practices guide) that all these files should be downloaded single shot, so browsers will cache one biggie instead of downloading them one by one across the browsing session.

Why should I not keep myself from gluing all these files together? Is it a big deal?

like image 728
Silviu-Marian Avatar asked Sep 06 '12 21:09

Silviu-Marian


People also ask

Is it better to have one CSS file or multiple?

Having only one CSS file is better for the loading-time of your pages, as it means less HTTP requests.

Should there be multiple CSS files?

A web app with different rather “siloed” sections probably need two CSS files. One global with the most common design patterns and then a section-specific CSS file with the less common design patterns that section needs. Sites with many vastly different styles of pages likely need two stylesheets.

Why might it be preferable to store CSS in a separate file rather than including it inside our .HTML files?

The main advantage is that your browser will cache your CSS file and therefore your website pages will load faster. Using separate CSS files is also better because you can have your whole CSS code in a separate file and this helps focusing properly on your work.

Does CSS need to be in a separate file?

The best practice in web design is to separate out the CSS into a separate stylesheet. The reason for this is that the CSS stylesheet exists for the purpose of defining the presentation style of the document. The HTML file exists to define the structure and content of the document.


1 Answers

Due to how cascading works, there is no possible way to cause harm just by concatenating the files together, provided you keep them in the order they appeared in the source. This will ensure that later files overriding earlier ones will still do so.

Then, you can consider minifying the CSS. This won't change the function of the selectors, but will compress them to reduce bandwidth.

All in all, having 27 CSS files is just stupid. The user would have to wait for 27 connections to be established, requests to be made, and downloads to be completed, just to see the page. Now, this does improve somewhat with proper cacheing, but even so...

like image 121
Niet the Dark Absol Avatar answered Sep 23 '22 01:09

Niet the Dark Absol