为什么字体名称需要引号?

Ben*_*enn 37 css fonts conventions

据我所知,如果字体包含空格,则需要使用双引号或单引号,例如:

font-family: "Times New Roman", Times; 
font-family: 'Times New Roman', Times;
Run Code Online (Sandbox Code Playgroud)

但在Google字体(http://www.google.com/webfont)上,我也看到了

font-family: 'Margarine', cursive;
Run Code Online (Sandbox Code Playgroud)

有些甚至像这样使用它:

font-family: 'Margarine', 'Helvetica', arial;
Run Code Online (Sandbox Code Playgroud)

我发现这很奇怪,因为以下工作原理:

font-family: Arial, Helvetica, sans-serif;
font-family: Cambria, serif;
Run Code Online (Sandbox Code Playgroud)

那么CSS中字体名称的引用的正确用法是什么?

Juk*_*ela 43

您可以随时把一个特定的字体系列名称在引号,双或单,所以Arial,"Arial"'Arial'是等价的.只有CSS定义的通用字体系列名称sans-serif必须不带引号.

与流行的看法相反,字体名称由空格分隔的名称组成,例如Times New Roman不需要引用.但是,规范 建议 "引用包含除连字符以外的空格,数字或标点字符的字体系列名称"


Ric*_*der 7

我刚刚了解到,引号不是必需的,而是推荐的。我们每天都在学习新东西。

您看到它们的另一个地方是在需要 url 的 css 属性中

background:url('hithere.jpg');
background:url(hithere.jpg);
Run Code Online (Sandbox Code Playgroud)

这两个语句的工作原理完全相同。字体也是如此,在这种情况下,您使用哪种类型的引用无关紧要,只要在您做事的方式上保持一致,这才是真正重要的。

  • 这不是真的,你**不需要**包含空格的字体名称。 (8认同)

Bob*_*ein 7

何时引用字体系列:

可选的

font-family: Times New Roman;         /* These are equivalent */
font-family: 'Times New Roman';
font-family: "Times New Roman";

font-family: Unique Ode™  Épopée;   /* These are equivalent */
font-family: "Unique Ode™  Épopée";

font-family: "Answer #42"             /* These are equivalent */
font-family: "Answer \23 42";
font-family: Answer \23 42; 
Run Code Online (Sandbox Code Playgroud)

只要字体名称仅包含:

那么引号是可选的。单引号或双引号。忽略大小写。有一些奇怪的边缘情况:未引用的单词不能以数字、破折号或破折号开头。

必须

font-family: "Intro Rust 2"           /* Unquoted words must not start with numbers. */
font-family: "Serif";                 /* For your own Serif, not the generic serif. */
font-family: "Yin/Yang";              /* Quote or code most ASCII punctuation. */
Run Code Online (Sandbox Code Playgroud)

应该

font-family: "Initial Seals JNL"      /* `initial` and `default` are reserved words. */
font-family: "Omar Serif"             /* Generic names might also be reserved words? */
Run Code Online (Sandbox Code Playgroud)

禁止

font-family: monospace;               /* Generic fonts must NOT be quoted. */
font-family: serif;
font-family: sans-serif;
font-family: cursive;
font-family: fantasy;
Run Code Online (Sandbox Code Playgroud)

谢谢: