Jump to content

@import vs. <link ... />


Arty Ziff

Recommended Posts

What is the difference / purposes between:

<style>
        @import "/some/path/somefile.css";
</style>

verses

<link rel="stylesheet" href="/some/path/somefile.css" />

I'm working with a MooTools modal app that does not work without the @import and only the <link ... />. I find this odd.

Link to comment
Share on other sites

@import is only convenient if you have multiple stylesheets.  It's pretty much the equivalent to PHP's include function.

Using link is pretty much the standard way to get external stylesheets, but using @import instead isn't really looked down upon (at least I think so).  It may or may not save you a few milliseconds on load time.

 

And the most obvious differences...

- @import is a CSS function

- is embedded in the

of the HTML.  Meaning it's probably cached.
Link to comment
Share on other sites

 

@import is only convenient if you have multiple stylesheets.  It's pretty much the equivalent to PHP's include function.

Using link is pretty much the standard way to get external stylesheets, but using @import instead isn't really looked down upon (at least I think so).  It may or may not save you a few milliseconds on load time.

 

And the most obvious differences...

- @import is a CSS function

- <link> is embedded in the <head> of the HTML.  Meaning it's probably cached.

Interesting... how do you mean by it will be cached properly? Surely the style sheet still gets downloaded and saved in the temp internet files on the clients comp?

Link to comment
Share on other sites

I don't exactly know what I was getting at on that point.. I was more or less just mentioning that link is inside the header, whereas a style tag (i.e the @import) could be placed anywhere else in the page.

 

Ideally, you want everything like that in the header... and if for some reason you have multiple stylesheets... or a snippet of CSS you want to import in case of IE7, then you can use @import for that convenience.

Link to comment
Share on other sites

During the first "Browser Wars", @import was used as a hack for hiding stylesheets from Version4 browsers and lower (varied support for CSS1).  The CSS2 @import rule was not understood by V4 and lower browsers (and therefore the Stylesheets they referenced were ignored).

 

People continued using it even after IE won the wars (like people today still use old deprecated html and invalid code even with transitional doctypes).

 

HOWEVER, the reasons it is better to use LINK instead of @import, are:

 

1. there are certain performance advantages - particularity for IE 6 - 8

 

2. IE loads and parses the referenced files within @import sequentially (first one then another taking 2 seconds each) - this makes the page slower to finish.  LINK, however, loads and parses the files all together in parallel - making the files available quicker and the page ready faster. 

 

3. @import can create problems with JavaScript -  IE loads script tags before style tags; if you have styling in the script that relies on a loaded css file (like getElementsByClassName) the resource is not going to be available and the results unpredictable.  Using LINK ensures that the Stylesheets are loaded in parallel AND in the specified order.

 

Hope this helps put it into proper perspective.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.