Why would font names need quotes?

CssFontsConventions

Css Problem Overview


As far as I know, one needs to use double or single quotes for fonts if they contain spaces, like:

font-family: "Times New Roman", Times; 
font-family: 'Times New Roman', Times;

But on Google Fonts (http://www.google.com/webfont), I also see

font-family: 'Margarine', cursive;

Some even use it like so:

font-family: 'Margarine', 'Helvetica', arial;

I find this weird, as the following works as well:

font-family: Arial, Helvetica, sans-serif;
font-family: Cambria, serif;

So what is the correct usage of quotes around font names in CSS?

Css Solutions


Solution 1 - Css

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”

Solution 2 - Css

When to quote font-family:

Optional

font-family: Times New Roman;         /* These are equivalent */
font-family: 'Times New Roman';
font-family: "Times New Roman";

font-family: Unique Ode™ 😊 Épopée;   /* These are equivalent */
font-family: "Unique Ode™ 😊 Épopée";

font-family: "Answer #42"             /* These are equivalent */
font-family: "Answer \23 42";
font-family: Answer \23 42; 

As long as a font name contains only:

then quotes are optional. Single or double quotes. Case ignored. There are some weird edge cases: unquoted words must not start with a digit, dash-dash, or dash-digit.

Must

font-family: "Intro Rust 2"           /* Unquoted words must not start with numbers. */
font-family: "Serif";                 /* For your own Serif, not the generic serif. */
font-family: "Yin/Yang";              /* Quote or code most ASCII punctuation. */

Should

font-family: "Initial Seals JNL"      /* `initial` and `default` are reserved words. */
font-family: "Omar Serif"             /* Generic names might also be reserved words? */

Must Not

font-family: monospace;               /* Generic fonts must NOT be quoted. */
font-family: serif;
font-family: sans-serif;
font-family: cursive;
font-family: fantasy;

Thanks:

Solution 3 - Css

I've just learned myself that the quotes aren't ever necessary but rather recommended. We all learn something new every day.

The other place you see them is in css properties that require a url

background:url('hithere.jpg');
background:url(hithere.jpg);

Both those statements are going to work exactly the same. Same goes for the fonts, which type of quote you use is irrelevant in this case, just be consistent in how YOU do things and that is all that really matters.

Solution 4 - Css

For two reasons;

  1. When an actual font-family name shares the same name as a generic family name, to avoid confusion with keywords with the same names e.g.

p {font-family: 'Shift', sans-serif;}

  1. When there are more than one word in a font-family name e.g.

p {font-family: 'Times New Roman', ..... , a generic family name here ;}

Solution 5 - Css

You have to use quotes when there is a space in the font name. If there is no space in the font name, it's best practice to leave them off, although it won't hurt anything but your file size to have them still.

Examples:

font-family: "Times New Roman", Times; // ok, and best practice
font-family: Times New Roman, Times; // incorrect, Browser won't be able to find Times New Roman
font-family: "Times New Roman", "Times"; // ok, but has extraneous quotes around Times

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBennView Question on Stackoverflow
Solution 1 - CssJukka K. KorpelaView Answer on Stackoverflow
Solution 2 - CssBob SteinView Answer on Stackoverflow
Solution 3 - CssRick CalderView Answer on Stackoverflow
Solution 4 - CssbizworldView Answer on Stackoverflow
Solution 5 - Cssgreg5greenView Answer on Stackoverflow