65

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

font-family: "Times New Roman", Times; 
font-family: 'Times New Roman', Times;

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

font-family: 'Margarine', cursive;

有些人甚至像这样使用它:

font-family: 'Margarine', 'Helvetica', arial;

我觉得这很奇怪,因为以下方法也有效:

font-family: Arial, Helvetica, sans-serif;
font-family: Cambria, serif;

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

4

5 回答 5

81

您始终可以将特定字体系列名称放在引号中,双引号或单引号,因此Arial"Arial"'Arial'是等价的。只有 CSS 定义的通用字体系列名称 likesans-serif必须不带引号。

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

于 2012-12-06T20:41:05.000 回答
8

何时引用字体系列:

可选的

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; 

只要字体名称仅包含:

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

必须

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. */

应该

font-family: "Initial Seals JNL"      /* `initial` and `default` are reserved words. */
font-family: "Omar Serif"             /* Generic names might also be reserved words? */

一定不

font-family: monospace;               /* Generic fonts must NOT be quoted. */
font-family: serif;
font-family: sans-serif;
font-family: cursive;
font-family: fantasy;

谢谢:

于 2020-08-29T20:08:03.760 回答
7

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

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

background:url('hithere.jpg');
background:url(hithere.jpg);

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

于 2012-12-06T19:57:28.447 回答
3

有两个原因;

  1. 当实际字体系列名称与通用系列名称共享相同名称时,以避免与具有相同名称的关键字混淆,例如

p {font-family: 'Shift', sans-serif;}

  1. 当字体系列名称中有多个单词时,例如

p {font-family: 'Times New Roman', ..... , a generic family name here ;}

于 2014-12-22T19:13:07.713 回答
-6

当字体名称中有空格时,您必须使用引号。如果字体名称中没有空格,最好不要使用它们,尽管保留它们不会对文件大小造成任何影响。

例子:

font-family: "Times New Roman", Times; // ok, and best practice
font-family: Times New Roman, Times; // incorrect, Browser won't be able to find Times New Roman
font-family: "Times New Roman", "Times"; // ok, but has extraneous quotes around Times
于 2012-12-06T19:56:05.063 回答