基本上,我想知道使用@import将样式表导入到现有的样式表中,而不是仅仅添加另一个样式表的优势/目的是什么…

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

文件的开头?


当前回答

使用link方法,样式表可以并行加载(更快更好),而且几乎所有浏览器都支持link

import加载任何额外的css文件一个接一个(较慢),并可以给你Flash的无风格的内容

其他回答

它们非常相似。有些人可能认为@import更易于维护。但是,每个@import都将花费您一个新的HTTP请求,其方式与使用“link”方法相同。所以在速度的背景下,它并不更快。正如“duskwuff”所说,它不能同时加载,这是一个缺点。

几乎没有理由使用@import,因为它会分别加载每个导入的CSS文件,并会显著降低网站的速度。如果你对处理CSS的最佳方式感兴趣(当涉及到页面速度时),以下是你应该如何处理所有CSS代码:

打开所有CSS文件,复制每个文件的代码 将所有代码粘贴到页面HTML头中的单个STYLE标记之间 永远不要使用CSS @import或单独的CSS文件来交付CSS,除非你有大量的代码或有特定的需要。

更多详细信息请访问:http://www.giftofspeed.com/optimize-css-delivery/

上面的效果最好的原因是因为它为浏览器创建了更少的请求,它可以立即开始呈现CSS,而不是下载单独的文件。

有时你必须使用@import而不是内联。如果你正在开发一个复杂的应用程序,它有32个或更多的css文件,而且你必须支持IE9,那么你别无选择。IE9忽略前31个之后的任何css文件,这包括内联css。但是,每个表可以导入31个其他表。

出于速度考虑,最好不要使用@import在页面中包含CSS。看看这篇优秀的文章,了解为什么不去:http://www.stevesouders.com/blog/2009/04/09/dont-use-import/

此外,通过@import标记来缩小和合并css文件通常更困难,因为缩小脚本不能从其他css文件中“剥离”@import行。当你将它们包含为<link标签时,你可以使用现有的minify php/dotnet/java模块来进行缩小。

所以:使用<link />代替@import。

引自http://webdesign.about.com/od/beginningcss/f/css_import_link.htm

The main purpose of @import method is to use multiple style sheets on a page, but only one link in your < head >. For example, a corporation might have a global style sheet for every page on the site, with sub-sections having additional styles that only apply to that sub-section. By linking to the sub-section style sheet and importing the global styles at the top of that style sheet, you don't have to maintain a gigantic style sheet with all the styles for the site and every sub-section. The only requirement is that any @import rules need to come before the rest of your style rules. And remember that inheritance can still be a problem.