Do I need to wrap quotes around font family names in CSS?
Asked Answered
A

3

109

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.

Apprehensive answered 3/10, 2011 at 17:57 Comment(1)
I think it's a good idea to quote every font family, less the generic ones. It keeps things consistent.Apologize
S
86

The CSS 2.1 spec tells us that:

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.

Also note that punctuation such as / or ! within an identifier may also need to be quoted or escaped.

Sholes answered 3/10, 2011 at 18:4 Comment(1)
initial and default are keywords too (they’re reserved for future use). See Unquoted font family names in CSS.Persevering
H
26

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.

Heighten answered 17/10, 2013 at 15:44 Comment(0)
B
3

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 &quot; which can be applied to all kinds of stranges fonts names:

<font style="font-family:&quot;a_CityNovaTitulB&WLt&quot; , &quot;Bailey'sCar&quot;">some text</font>

In FireFox,the source will show the &quot; as this: "

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".

Borek answered 7/7, 2015 at 2:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.