Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

@import vs link

Tags:

import

css

First I know the title says this is a duplicate question as asked here, here, and here. Everything I read on this topic is over two years old. A lot has changed in that time period so are the same thoughts still advisable?

Here is an example, I use @import inside a stylesheet to bring in my reset CSS and a couple other styles. Should I change that from @import to <link>? According to this article I should use link. So I ask other developers, what is truly the best way as to date (August 25, 2011)

like image 376
L84 Avatar asked Aug 26 '11 02:08

L84


People also ask

Is it better to link or @import?

<link> is preferred in all cases over @import , because the latter blocks parallel downloads, meaning that the browser will wait for the imported file to finish downloading before it starts downloading the rest of the content.

What is role of @import url ()?

Definition and Usage. 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.

Why is @import only at the top?

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 is the purpose of @import in CSS please Google it?

The @import CSS at-rule is used to import style rules from other stylesheets.


1 Answers

Not much if anything has changed in the past year or two, and we're still dealing with a lot of the same browsers from then, so you should not change your practice.

<link> is preferred in all cases over @import, because the latter blocks parallel downloads, meaning that the browser will wait for the imported file to finish downloading before it starts downloading the rest of the content.

You can see this in great detail here:

http://www.stevesouders.com/blog/2009/04/09/dont-use-import/

So, while @import may be convenient, that's all it offers. If you really want to take advantage of fast loading times, use the minimum number of stylesheets (probably one in most cases), write good CSS with efficient selectors (the usual stuff), minify it, and use a <link> tag.


This was going to be a comment but it got too long:

Instead of @import (I know it is very convenient), you should combine the files into one when your site goes live. You shouldn't be tweaking at that point anyways, and there are a number of tools to help minify it. Personally, using PHP, I have a config file where I define all the CSS files that are written to a separate CSS file (the one I will reference in the <link> tag), then if the cached version is old (either determined manually or automatically), it combines/minifies them and writes the content to the "cache" file, and returns a timestamp query string to append to the CSS file name to force a fresh download.

If you are using PHP as well, I highly recommend cssmin, it can parse stylesheets for @import and pull the content into one file, as well as handle all aspects of minification.

like image 198
Wesley Murch Avatar answered Sep 18 '22 13:09

Wesley Murch