我需要在 CSS 中的字体系列名称周围加上引号吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7638775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Do I need to wrap quotes around font family names in CSS?
提问by daGUY
I remember hearing a long time ago that it was considered "best practice" to wrap quotes around font names that contain multiple words in the CSS font-family property, like this:
我记得很久以前听说在 CSS font-family 属性中将包含多个单词的字体名称用引号括起来被认为是“最佳实践”,如下所示:
font-family: "Arial Narrow", Arial, Helvetica, sans-serif;
For the heck of it, I tried removing the quotes from "Arial Narrow"
and Safari and Firefox don't have any problem rendering it.
最重要的是,我尝试从"Arial Narrow"
Safari 和 Firefox 中删除引号,渲染它没有任何问题。
So, is there any logic to this rule of thumb, or is it just a myth? Was it an issue with older browsers that no longer applies to the current versions? I've been doing this for so long that I never stopped to think if it was actually necessary.
那么,这个经验法则是否有任何逻辑,或者它只是一个神话?这是不再适用于当前版本的旧浏览器的问题吗?我已经这样做了很长时间,以至于我从未停下来思考是否真的有必要。
采纳答案by James Allardice
The CSS 2.1 spectells us that:
在CSS 2.1规范告诉我们:
Font family names must either be given quoted as strings, or unquoted as a sequence of one or more identifiers. This means most punctuation characters and digits at the start of each token must be escaped in unquoted font family names.
字体系列名称必须作为字符串引用,或者作为一个或多个标识符的序列不引用。这意味着每个标记开头的大多数标点字符和数字必须以不带引号的字体系列名称进行转义。
It goes on to say:
它接着说:
If a sequence of identifiers is given as a font family name, the computed value is the name converted to a string by joining all the identifiers in the sequence by single spaces.
To avoid mistakes in escaping, it is recommended to quote font family names that contain white space, digits, or punctuation characters other than hyphens:
如果将标识符序列作为字体系列名称给出,则计算值是通过用单个空格连接序列中的所有标识符将名称转换为字符串。
为避免转义错误,建议引用包含除连字符以外的空格、数字或标点符号的字体系列名称:
So yes, there is a difference, but one that's unlikely to cause any problems. Personally, I have always quoted font names when they contain spaces. In a few (presumably very rare) cases, the quotes are absolutely required:
所以是的,有区别,但不太可能引起任何问题。就个人而言,当字体名称包含空格时,我总是引用它们。在少数(可能非常罕见)的情况下,引号是绝对需要的:
Font family names that happen to be the same as a keyword value ('inherit', 'serif', 'sans-serif', 'monospace', 'fantasy', and 'cursive') must be quoted to prevent confusion with the keywords with the same names. The keywords 'initial' and 'default' are reserved for future use and must also be quoted when used as font names.
碰巧与关键字值相同的字体系列名称('inherit'、'serif'、'sans-serif'、'monospace'、'fantasy' 和 'cursive')必须加引号以防止与关键字混淆同名。关键字 'initial' 和 'default' 保留供将来使用,在用作字体名称时也必须加引号。
Also note that punctuation such as / or ! within an identifier may also needto be quoted or escaped.
还要注意标点符号,例如 / 或 ! 在标识符内也可能需要被引用或转义。
回答by djip.co
According to the CSS Fonts Module Level 3 specof October 2013, "font family names other than generic families must either be given quoted as strings, or unquoted as a sequence of one or more identifiers". So you DO NOT need to enclose them in quotes.
根据2013 年 10 月的CSS 字体模块级别 3 规范,“除通用系列之外的字体系列名称必须以字符串形式引用,或者不以一个或多个标识符的形式引用”。所以你不需要用引号将它们括起来。
However, if you don't "most punctuation characters and digits at the start of each token must be escaped". To avoid escaping mistakes, the W3C actually recommends to quote font family names containing white space, digits, punctuation or keyword values(‘inherit', ‘serif', etc.).
但是,如果您不“必须对每个标记开头的大多数标点字符和数字进行转义”。为了避免转义错误,W3C 实际上建议引用包含空格、数字、标点符号或关键字值('inherit'、'serif' 等)的字体系列名称。
The generic font family names (‘serif', ‘sans-serif', ‘cursive', ‘fantasy', and ‘monospace') MUST NOT be quoted as they are actually keywords.
不得引用通用字体系列名称('serif'、'sans-serif'、'cursive'、'fantasy' 和 'monospace'),因为它们实际上是关键字。
回答by internetdev
If style is inline, like <font style="font-family:Arial Narrow">some texte</font>
, it works.
如果样式是内联的,比如<font style="font-family:Arial Narrow">some texte</font>
,它就可以工作。
But if the name of the police font contains some special characters, or starts with a number contains a quotes or others strange things (like "01 Digitall" or "a_CityNovaTitulB&WLt" or "Bailey'sCar"), you must use a special syntax with "which can be applied to all kinds of stranges fonts names:
但是如果警察字体的名称包含一些特殊字符,或者以数字开头包含引号或其他奇怪的东西(如“01 Digitall”或“a_CityNovaTitulB&WLt”或“Bailey'sCar”),则必须使用特殊语法" 可以应用于各种奇怪的字体名称:
<font style="font-family:"a_CityNovaTitulB&WLt" , "Bailey'sCar"">some text</font>
In FireFox,the source will show the "as this: "
在 FireFox 中,源将显示“ 像这样:“
without this trick, this:
没有这个技巧,这个:
<font style="font-family:a_CityNovaTitulB&WLt ,Bailey'sCar">some text</font>
doesn't automatically work in every browser. It's useful for font name witch start by a number to, like "8 Pin".
不会在每个浏览器中自动工作。对于以数字开头的字体名称很有用,例如“8 Pin”。