Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would font names need quotes?

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?

like image 342
Benn Avatar asked Dec 06 '12 19:12

Benn


People also ask

Why would you need to put quotes around a font name?

Quotes are recommended in the spec with "font family names that contain white space, digits, or punctuation characters other than hyphens". Quotes are required around font-family names when they are not valid CSS identifiers.

Why font-family is used?

The font-family property specifies the font for an element. The font-family property can hold several font names as a "fallback" system. If the browser does not support the first font, it tries the next font.

Is it reasonable to have two generic fonts in a font-family rule?

Web authors should always add at least one generic family in a font-family list, since there's no guarantee that a specific font is installed on the computer or can be downloaded using a @font-face at-rule.


2 Answers

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”

like image 120
Jukka K. Korpela Avatar answered Oct 18 '22 09:10

Jukka K. Korpela


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:

  • letters
  • digits
  • dash
  • underscore
  • non-ASCII Unicode
  • single inner spaces
  • backslash hexadecimal character codes

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:

  • Article on font names by Mathias Bynens, linked by Stephan B's comment.
  • bizworld's point about conflict with generic fonts.
  • Mozilla's description of Custom Identifiers.
  • W3C spec for font-family and identifiers, linked by Jukka K. Korpela's answer
like image 31
Bob Stein Avatar answered Oct 18 '22 09:10

Bob Stein