我需要在 CSS 中的字体系列名称周围加上引号吗?
我记得很久以前就听说过,在 CSS font-family 属性中包含多个单词的字体名称周围加上引号被认为是“最佳实践”,如下所示:
font-family: "Arial Narrow", Arial, Helvetica, sans-serif;
到底如何,我尝试从 “Arial Narrow”
和 Safari 和 Firefox 渲染它没有任何问题。
那么,这个经验法则有任何逻辑吗,还是只是一个神话?是否是旧版浏览器的问题不再适用于当前版本?我已经这样做了很长时间,以至于我从未停下来思考这是否真的有必要。
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:
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
CSS 2.1 规范告诉我们:
它接着说:
所以是的,存在差异,但不太可能引起任何问题。就我个人而言,当字体名称包含空格时,我总是引用它们。在少数(可能非常罕见)情况下,绝对需要引号:
另请注意标点符号,例如 / 或 !在标识符中可能还需要被引用或转义。
The CSS 2.1 spec tells us that:
It goes on to say:
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:
Also note that punctuation such as / or ! within an identifier may also need to be quoted or escaped.
根据 CSS 字体模块级别 3 规范 2013 年 10 月,“除通用系列之外的字体系列名称必须作为字符串加引号,或者作为一个或多个标识符的序列不加引号”。 因此您不需要将它们括在引号中。
但是,如果您不这样做,“每个标记开头的大多数标点字符和数字都必须转义”。 为了避免转义错误,W3C 实际上建议引用包含空格、数字、标点符号或关键字值的字体系列名称(“inherit”、“serif”等)。
通用字体系列名称(“serif”、“sans-serif”、“cursive”、“fantasy”和“monospace”)不得加引号,因为它们实际上是关键字。
According to the CSS Fonts Module Level 3 spec of 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.
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.).
The generic font family names (‘serif’, ‘sans-serif’, ‘cursive’, ‘fantasy’, and ‘monospace’) MUST NOT be quoted as they are actually keywords.
如果样式是内联的,例如
some texte
,则它可以工作。但是,如果警察字体的名称包含一些特殊字符,或者以数字开头包含引号或其他奇怪的东西(例如“01 Digitall”或“a_CityNovaTitulB&WLt”或“Bailey'sCar”),则必须使用特殊的字体" 语法可以应用于各种奇怪的字体名称:
在 FireFox 中,源代码将显示" 如下: "
如果没有这个技巧, this:
不会自动在每个浏览器中工作。
对于以数字开头的字体名称非常有用,例如“8 Pin”。
If style is inline, like
<font style="font-family:Arial Narrow">some texte</font>
, it works.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:
In FireFox,the source will show the " as this: "
without this trick, this:
doesn't automatically work in every browser.
It's useful for font name witch start by a number to, like "8 Pin".