<head> 中的 Google 字体还是带有 @import 的 CSS?

chr*_*ris 5 html css fonts google-font-api

(我是新来的所以如果这不是问它的地方,请告诉我)

通常我添加<link href='http://fonts.googleapis.com/css?family=Open+Sans:300italic,400,400italic,700' rel='stylesheet' type='text/css'>到<head>我的页面。对于多个页面,总是有可能出现不一致/错误,而且更新每个页面可能会令人头疼。

Can I instead just use @import url(http://fonts.googleapis.com/css?family=Open+Sans); at the first line of my main CSS file?

Advantage to this is updating one CSS file rather than updating every single page where it's in the <head>. But I've read some answers that say there may be a resource loading problem...but that discussion was 3 years ago. Can't find a current answer addressing this.

EDIT
To avoid SO from thinking this is duplicate, I am asking which is better method for 2015. I am not asking how to add Google fonts to a site under either method.

Ale*_*rov 4

如果您在 CSS 中使用@import规则,浏览器将无法并行下载引用的脚本,因为在执行任何下载之前必须解析携带的脚本!

例子#1

style1.css并style2.css使用标签加载<link>:

例子#1

例子#2

style1.css使用标签加载<link>并style2.css使用规则加载@import:

例子#2

要启用并行下载,请改用<link>html 标签。

或者,您可以@import在不使用规则的情况下内联 CSS;样式表预处理器可以帮助您(例如 Sass)。您可以尝试使用 Node.js 任务运行程序(gulp、grunt)来自动执行此类任务。