10

I have read different information about this question, and I always see that people suggest using one big css file, from the performance point of view. But I don't understand, won't it take more time to load the css file for all of the pages then to load each one for each page, when it is requested? (examples of what I have seen: Multiple css files or one big css file? and Single huge .css file vs. multiple smaller specific .css files?). Yes, maybe there will be multiple http requests, but they will happen in the different periods of time, so basically isn't it better to load each page when it is needed, then to waste time on loading css for every page in the beginning and then displaying them on the fly?

3 Answers 3

15

There is pros and cons of both approaches.

Having multiple CSS files will allow you to organize and group your CSS files properly in development. However, this also means that there are multiple HTTP requests to make. HTTP requests are more expensive in terms of loading time as it has to contact the server and fetch the file.

Also once a file is loaded, it is cached by the browser. Which means, even-though it might be initially slower to load the huge.css, it doesn't need to be loaded again when you navigate around the site.

In my experience and adapted by most of the developers, you could do something like below to get most of the both worlds.

Use css pre-processers like SASS or LESS. Don't ask me which one is better, there is already enough arguments going around the web on that topic. Just pick one which you are comfortable with. My preference is SASS.

CSS pre-processers allows you to divide your CSS files into smaller more organized files. For example, you could have a main.sass which includes menu.sass, footer.sass, etc.

main.sass

include _menu
include _footer
include _header
...

_ tells sass not to compile seperate files for each of these sass files. So they all will only be compiled to a one main.css. These pre-processors come with a functionality to compile the given sass files into a css file that browser can read. You can also use tools like [livereload][4] to compile them in real-time.

You will have these sass files in your development code. But in your production code, you can simply use the compiled single css file.

If you are feeling more advantageous and want to take things further, you can use tool like Grunt or Gulp. They allow to automate your development tasks or build processes. So ideally, in development you could have a grunt task that watches all your sass files and automatically compiles them into the main.css file. In your index.html you can have reference to this main.css. Once you are happy, you can also have another task called build task, which can automatically compile all your css files and minimize them.

To clarify your question:

It depends what is best in case by case basis, what kind of site you have and how you have built it.

If you have a website where visitors are most likely to never navigate around the site than some particular pages, then its better to load css specific to that particular page and not combine it. You can load these css files in the partials specific to these page.

In my experience building many many sites,

  1. its almost always best to load one combined css.
  2. if a particular page requires css is not likely to be visited often, include a page specific css in its templete seperately with a conditional script tag.
  3. Minimize all css files almost 100% of time

Also, I will suggest you to spend more time improving efficiency of your server side code or database, then worrying too much about the css. It will be more productive as most of the in-efficiency lies in server side.

Sign up to request clarification or add additional context in comments.

4 Comments

Few things to say: 1) I can swear I have read the similar beginning of your question to some other question of the same type. 2) I have used styling tools for a while and I like LESS much more than SaSS (Stylus is also great, by the way). 3) I understand that in the development, it is easier to use multiple CSS files and (as I have already said) I have read that in the production it is better to use only one. 4) The question is basically: "What is more efficent - to load the Big CSS file with content for every page, in the beginning OR load for every page it's separate file when needed?"
I see that you have basically answered my question, saying that it is better to use one big file for every page on the website, but please read again the last sentence of my question. Are you sure that it is the most efficient way? Because one of the best web applications (vk.com) uses ajax to load css files, when a user visits the page and then stores it in the file in case a person will visit it again
@Alex I am not entirely not sure about your question, but I guess you are asking about the responsive site. In css you can have responsive site based upon the breakpoints (fixed size based) or fluid (percentage based). In css you would have different style rules for same elements based upon these breakpoints. I would suggest you to create a different question for this as this conversation is irrelevant to this topic and might not be helpful for people looking for help on the topic.
Though I see why some think it's better to load one big CSS file, as it'll probably save the user more time in the long run, I think the Google Page Speed tool, and other tools, disagree ("remove used CSS!" and when I do I get much better scores). It does kinda make sense to me that the separate CSS files for different pages is better... When one does some research of page load time vs conversion rates, one could be persuaded that it's better to have each page loading in 1.1s rather than the first page taking 3s and subsequent pages 0.8s.
0

In answer to this question, it's best to detect what browser the user is viewing the page on, then loading in x.css, y.css dependent on that. This can also massively reduce the number of CSS errors that are displayed in the browser which, rumor has it is good for SEO.

14 Comments

What difference would it make?
Rather than reading one massive .css file with a large amount of rules for multiple browsers, it would load in a condensed .css file with rules for that browser (reducing loading time). Also as previously mentioned, it reduces CSS errors which are given by the fact that certain attributes have to be ignored by different browsers.
I mostly use css that fits every browser, and not browser specific
You're most probably getting CSS errors in certain sites then. The CSS error can be as simple as the browser ignoring a certain rule.
Not sure if trolling or serious. What you are writing is simply wrong
|
-1

I use multiple css files, divided by function. There is one global one, one for the page layout, one for the menus, one for the tab controls etc. I also have cascaded ones for desktop and mobile - only one gets applied. And then I have further cascaded ones for skinning by user preference. And I pick and choose which ones to load based on page, platform and user. That's what the PHP is for.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.