最好的方法来包括CSS?为什么使用@import?
基本上我想知道使用 @import
将样式表导入现有的样式表,而不是仅仅添加另一个样式表的优势/目的...
Basically I am wondering what is the advantage / purpose of using @import
to import stylesheets into an existing stylesheet versus just adding another ...
<link rel="stylesheet" type="text/css" href="" />
到文档头部?
从页面速度的角度来看,CSS文件中的 @import
几乎从不使用,因为它可以防止样式表被下载同时。例如,如果样式表A包含文本:
From a page speed standpoint, @import
from a CSS file should almost never be used, as it can prevent stylesheets from being downloaded concurrently. For instance, if stylesheet A contains the text:
@import url("stylesheetB.css");
,那么在下载第一个样式表之前,第二个样式表的下载可能无法开始。另一方面,如果两个样式表都在主HTML页面的< link>
元素中引用,则两者都可以同时下载。如果两个样式表总是加载在一起,也可以将它们简单地合并到一个文件中。
then the download of the second stylesheet may not start until the first stylesheet has been downloaded. If, on the other hand, both stylesheets are referenced in <link>
elements in the main HTML page, both can be downloaded at the same time. If both stylesheets are always loaded together, it can also be helpful to simply combine them into a single file.
有时候会出现 @import
是合适的,但它们通常是例外,而不是规则。
There are occasionally situations where @import
is appropriate, but they are generally the exception, not the rule.