Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS on a separate file or not?

Tags:

css

dynamic

Which is a better option: to store CSS on a separate file or on the same page?

Let's forget the fact that changing the CSS on a file makes it to apply all HTML pages directly. I am using dynamic languages to generate the whole output - so that does not matter.

A few things I can think of:

  1. CSS on a separate file generates less bandwidth load.
  2. CSS on a separate file needs another HTTP request.

On the other hand, if I compress the data transmission with Zlib, the CSS on the same page should not matter in terms of bandwidth, correct? So, I get one less HTTP request?

like image 562
Tower Avatar asked May 30 '09 12:05

Tower


People also ask

Does CSS have to be in a separate file?

Yes, It is possible to include one CSS file in another and it can be done multiple times. Also, import multiple CSS files in the main HTML file or in the main CSS file.

Is it better to have one CSS file or multiple?

It depends on your use case. Usually everyone just smash a single css into the page and load it everywhere. If the site small and the css is only a few kB should be fine. once you get to the 100kb+ u probably better sppliting files into the most critical css and fonts so the first content is available without delays.

Should CSS be in a different file than HTML?

Keep it separate. HTML for centent, CSS for style, JavaScript for logic.

Should I have a separate CSS file for each page?

you should keep only one css file. Let me tell you in simple one line, once your website loads in client web browser the static resource can be cached that helped your website to boost and number of web request can be reduce when user browse multiple pages of your website. Save this answer.


1 Answers

There is a method that both Google and Yahoo apply which benefit's from inline CSS. For the very first time visitors for the sake of fast loading, they embed CSS (and even JavaScript) in the HTML, and then in the background download the separate CSS and JS files for the next time.

Steve Souders (Yahoo!) writes the following:

[...] the best solution generally is to deploy the JavaScript and CSS as external files. The only exception I’ve seen where inlining is preferable is with home pages, such as Yahoo!'s front page (http://www.yahoo.com) and My Yahoo! (http://my.yahoo.com). Home pages that have few (perhaps only one) page view per session may find that inlining JavaScript and CSS results in faster end-user response times.

like image 191
viam0Zah Avatar answered Sep 28 '22 03:09

viam0Zah