When Should CSS Font-Family Value Use Quotes

When should CSS font-family value use quotes?

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)

Why would font names need quotes?

You can always put a specific font family name in quotes, double or single, so Arial, "Arial", and 'Arial' are equivalent. Only the CSS-defined generic font family names like sans-serif must be written without quotes.

Contrary to popular belief, a font name consisting of space-separated names such as Times New Roman need not be quoted. However, the spec recommends “to quote font family names that contain white space, digits, or punctuation characters other than hyphens”

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

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.

Do fonts with spaces need to be enclosed in quotes?

Per the spec, no it isn't required, but it is recommended.

Font family names other than generic families 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.

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

Can CSS property values be quoted?

No, CSS properties in general cannot be quoted. Some CSS properties can have quotes in their values though, like:

background: url('quoted/path');

Certain CSS properties also accept a string as their value, which of course must be surrounded by quotes. font-family is an example of a property which can accept a string.

Escape quote with font family

Use single quotes:

<div style="font-family: 'Times New Roman', Times, serif"> Hello world! </div>

or alternatively (Thanks @Matt):

<div style='font-family: "Times New Roman", Times, serif'> Hello world! </div>


Related Topics



Leave a reply



Submit