Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does @import in CSS result in additional http requests?

Tags:

css

I have an ecommerce site that has about 8 CSS files linked from the header - resulting in 8 separate http requests to the server. I consolidated all the CSS files into 1 big one, resulting in a 67kb (!) file - to cut down the http requests to 1 for our css files.

I'm finding this size a CSS file a little unmanageable in light of the fact I'm performing updates on the site constantly. My concern is my users may catch me in the middle of updating and see a NON-styled page when moving from page to page - b/c 67kb still takes a good 2-3 seconds before it is successfully placed on the remote server via FTP.

My question is: does the use of @import within this large CSS file to break up the files into smaller more manageable sizes (within that CSS file) take us back to the original 8 http-requests when the pages is loaded? Or are @imports in CSS handle differently somehow?

like image 211
Mo Boho Avatar asked May 06 '09 20:05

Mo Boho


People also ask

What does @import do in CSS?

The @import rule allows you to import a style sheet into another style sheet. The @import rule must be at the top of the document (but after any @charset declaration). The @import rule also supports media queries, so you can allow the import to be media-dependent.

Should I use @import in CSS?

Don't use CSS @import It can be used as a way to import CSS scripts within a stylesheet tag in HTML documents or to add extra rules within CSS files.

Why is @import only at the top in CSS?

A style sheet that is imported into another one has a lower ranking in the cascading order: the importing style sheet overrides the imported one. Programmers may recognize this as the same model as in Java, Modula, Object-Pascal, Oberon and other modular programming languages.

What does the @import directive in less do if the file being imported is a plain CSS file?

The @import directive is used to import the files in the code. It spreads the LESS code over different files and allows to maintain the structure of code easily. You can put the @import statements anywhere in the code. For instance, you can import the file by using @import keyword as @import "file_name.


2 Answers

Yeah you will go back to a request per each stylesheet while using @import.

Your best bet is to minify and consolidate the css into a single file for deployment. But you can still develop with seperate files.

like image 189
Corey Downie Avatar answered Sep 28 '22 08:09

Corey Downie


You can prevent the extra http requests by combining the stylesheets with .htaccess. The technique is explained in the HTML5 Boilerplate .htaccess. It works like this:

In .htaccess:

<FilesMatch "\.combined\.(js|css)$">
    Options +Includes
    SetOutputFilter INCLUDES
</FilesMatch>

In style.combined.css:

<!--#include file="reset.css"-->
<!--#include file="style.css"-->

You can use this to combine .js (or any other extension you put in the parenthesis.)

The documentation for Options +Includes is here.

like image 30
ryanve Avatar answered Sep 28 '22 08:09

ryanve