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>
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>
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).
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.