When should CSS font-family value use quotes? [duplicate]

梦想的初衷 提交于 2020-01-10 19:32:13

问题


When should the value for CSS 'font-family' have quotes around it?

I've seen both font-family: arial and font-family: "arial".

As a specific example, I declare a font this way:

@font-face {
    font-family: 'entypo';
    font-style: normal;
    font-weight: 400;
    src: url('/css/fonts/entypo.woff') format('woff');
}

What would be the correct CSS to have an element use this font-family?


回答1:


You only need quotes when the font itself has a space such as "Times New Roman".

Arial does not need quotes, but some people use quotes such as "Arial" to be more consistent. It is simply personal preference.

Seen in Justin's below comment: font-family: times new roman; works without quotes (JSFiddle).

You can call your new @font-face using font-family: 'entypo'; as you would normally expect. (link)




回答2:


Just going to answer from this:

http://www.w3.org/TR/CSS2/fonts.html#font-family-prop

To avoid mistakes in escaping, it is recommended to quote font family names that contain white space, digits, or punctuation characters other than hyphens:

body { font-family: "New Century Schoolbook", serif }

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.




回答3:


By the CSS 2.1 spec, a font name needs to be in quotes if it contains characters other than identifier characters (which is a broader concept than just “Ascii letters and digits”) and spaces. So font-family: foo bar is correct, and so is e.g. font-family: entypo of course.

Coding style is a different issue. It is always correct to quote a specific font family name (as opposite to generic names like sans-serif), so font-family: "entypo" is correct, too.

Very theoretically, a font name also needs to be quoted if a specific font family name coincides with a generic name (I don’t think anyone ever created such a font) or if its name contains leading or trailing spaces or consecutive spaces (but no one in his sense would name his font that way).



来源:https://stackoverflow.com/questions/15228792/when-should-css-font-family-value-use-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!