views:

147

answers:

2

Or is it just a personal preference thing? What I'm getting at is, is there a specific benefit to using either method?

<link href="main.css" rel="stylesheet" type="text/css">

versus

<style type="text/css">
@import url('main.css');
</style>
+7  A: 

Short version? @import is not supported in old browsers, in certain situation has problems with certain browsers in common use (IE6 and IE7), can behave differently in different browsers when it does work and offers no advantage over <link>.

Long version? You want to use <link> but there are a couple of scenarios (now mostly irrelevant) where using @import made sense. From What's the Difference Between @import and link for CSS?:

The most common reason given for using @import instead (or along with) is because older browsers didn't recognize @import, so you could hide styles from them.

That's talking about hiding things from IE4, which is why I said "mostly irrelevant". One of those cases hides things from IE6 but that's better done with Conditional comments.

A more modern (and relevant) comparison is in Using the CSS @import Rule:

nternet Explorer (you knew it’d come up eventually) doesn’t deal well with specifying media types – it chokes. Basically, IE (versions 4-7) try to read the media type like it were part of the file name, causing the whole thing to come crashing down. As such, if you don’t want your CSS to have a default media type of “all,” you’re probably better off using a combination of the tag and imports – specifying a media type in your link, and then importing the appropriate CSS within the file you’re linking to. I haven’t yet heard if IE8 suffers from this same problem (if you happen to know, please enlighten me in the comments!).

Another source is Yahoo's Best Practices for Speeding Up Your Web Site:

One of the previous best practices states that CSS should be at the top in order to allow for progressive rendering.

In IE @import behaves the same as using at the bottom of the page, so it's best not to use it.

but doesn't really explain why (hence the previous links).

cletus
I don't see how using `@import` would cause a larger HTML file...
strager
In fact the problem occurs if I recall when you use more than one css file. Then, if you are using @import inside css files, you cause IE to download css files sequentially instead of in parallel when using the <link> tag. Downloading in parallel is faster as two or more files are downloading at once. More explanations here : http://stevesouders.com/blog/2009/04/09/dont-use-import/
MaxiWheat
You're correct there strager. If you're @import-ing and you omit the line breaks then both are exactly the same in terms of the size of the html document. You can make gains by doing a link or an @import and then @import-ing the rest of your css in your main.css document. Cletus, it looks as if you're partly correct in that @import causes the browser to download the CSS sequentially rather than in parallel in a separate request (See William Brendel's response). I don't think caching is actually an issue (anymore?)
Iain Fraser
+8  A: 

According to Yahoo's Best Practices for Speeding Up Your Web Site, always use <link> instead of @import. More detailed information is available in this blog post.

In IE (tested on 6, 7, and 8), @import causes the stylesheets to be downloaded sequentially. Downloading resources in parallel is key to a faster page. This behavior in IE causes the page to take a longer time to finish.

Using <link> allows the browser to open additional connections, thereby decreasing load times.

William Brendel
That answer makes more sense to me.
MaxiWheat
As well as this, Internet Explorer 5 and below will not parse a @import at all - which is what some older hacks would use to have extra rules defined
SuperRoach