77

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?

Lucas
  • 16,930
  • 31
  • 110
  • 182
Benn
  • 4,840
  • 8
  • 65
  • 106

5 Answers5

95

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”

Juan Marco
  • 3,081
  • 2
  • 28
  • 32
Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390
26

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:

Bob Stein
  • 16,271
  • 10
  • 88
  • 101
7

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.

Rick Calder
  • 18,310
  • 3
  • 24
  • 41
  • 8
    This is not true, you **don't need** quotes for font names containing spaces. – Stephan Bönnemann-Walenta Dec 06 '12 at 20:40
  • 1
    I wasn't aware of that, amended my answer to reflect this. Thanks for that. – Rick Calder Dec 07 '12 at 12:58
  • Two year old answer being downvoted because people can't read >.< lol. I love this site some days... – Rick Calder Dec 09 '14 at 15:39
  • 1
    @Rick Calder. You said you amended your answer, however the first line still reads `The quotes are only necessary to encapsulate names with spaces` - which is simply false. Anyone who skims for answers will just read the beginning, of your answer, which is incorrect, so don't wonder why your answer gets downvoted. Amending that line would help the overall quality of the article. – Kevin M Jul 24 '17 at 02:20
  • That is why that line is in italics and it is explained if you read the rest of the paragraph that they aren't necessary. I don't create answers for people that skim them, I create them looking for a whole answer. – Rick Calder Jul 25 '17 at 19:28
  • `font-family: 'Cheese 42';` requires quotes. Otherwise **words starting with digits** are a syntax error in CSS. – Bob Stein Aug 25 '20 at 13:10
3

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 ;}

Nicolapps
  • 819
  • 13
  • 29
bizworld
  • 51
  • 6
  • 2
    Based on the other answers, it doesn't appear #2 is true. That is, quotes aren't needed even if there is more than one word in the family name. – Crashalot Jan 04 '20 at 23:13
-6

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
greg5green
  • 400
  • 1
  • 7
  • That is what I thought but like said I see it everywhere even if there is no space and trying to figure out the difference if there is any if I do 'Margarine', 'Helvetica',arial; or 'Margarine', Helvetica,arial; – Benn Dec 06 '12 at 19:57
  • There is no difference. (I've added an example that hopefully helps.) – greg5green Dec 06 '12 at 19:58
  • I might suggest reading the article that Stephan posted as a comment to your original question. It goes a lot more in-depth on the issue, whereas I just did a quick explanation. Link in case his comment disappears: http://mathiasbynens.be/notes/unquoted-font-family – greg5green Dec 06 '12 at 20:01
  • To explain your down-votes: it's not as simple as "space in the font name". The link you yourself posted explains that this is simply not the case, except if certain conditions apply. So I would suggest you remove this incorrect answer or edit it to better comply with the article you linked to. – Victor Zamanian Nov 09 '18 at 10:30