Sub topic 11
Naming families in CSS: font stacks and generic fallbacks
This chapter assumes you can already tell the five families apart and choose one for a context. It also assumes you have read and edited a basic CSS rule. Here you turn those choices into CSS that still works when a font is missing.
How a font stack works
The font-family property takes a comma-separated list called a font stack. The browser checks the list from left to right. It uses the first family that is installed on the device or loaded by the page. If none of the named families are available, it uses the last entry, which should be a generic family.
A generic family is a keyword, not a font name. It tells the browser to pick any available font of a broad style. Keywords are never quoted. Family names that contain spaces are quoted.
body {
font-family: Georgia, "Times New Roman", serif;
}
h1, h2 {
font-family: "Helvetica Neue", Arial, sans-serif;
}The body stack asks for Georgia first, then Times New Roman, then any serif. The heading stack follows the same pattern for sans serif. The fallbacks come from the same family on purpose. If Georgia is missing, you still want a serif, so the page keeps its intended character.
Which families have a generic keyword
CSS defines generic families such as serif, sans-serif, monospace, cursive, and fantasy. They do not line up one-to-one with the five families from this tutorial.
| Family | Generic keyword | Notes |
|---|---|---|
| Serif | `serif` | Direct match. |
| Sans serif | `sans-serif` | Direct match. |
| Slab serif | `serif` | No generic of its own. |
| Script | `cursive` | Loose match. |
| Display | `sans-serif` (a common choice) | No reliable generic. |
The fantasy keyword exists, but it resolves differently across systems, so it is a poor stand-in for display type.
Script and slab examples
Script maps to cursive. Slab serif has no generic, so its stack falls back to serif.
.script-accent {
font-family: "Brush Script MT", cursive;
}
.slab-headline {
font-family: Rockwell, "Roboto Slab", serif;
}If Rockwell and Roboto Slab are both missing, the slab headline becomes an ordinary serif. The text stays readable but loses the heavy, low-contrast serifs. That is an acceptable fallback, because the result is still a serif.
The cursive keyword is less predictable. On some systems it gives a casual handwriting face rather than a formal script. Use script only for short accents, and make sure the page still reads well if the fallback looks different.
Display example
CSS has no generic family that reliably means display. You choose the fallback by what the title needs. A bold, condensed headline face usually falls back well to a heavy sans serif.
.display-title {
font-family: Impact, "Arial Black", sans-serif;
font-size: 4rem;
line-height: 1;
}Use a display face only at a large size and for a short title. If the fallback is used, the title should still hold its weight and impact.
Common errors
- No generic family at the end. Without it, the browser uses its default font when every named family is missing. The default is often a serif, even for a sans serif design.
- Quoting the keyword.
"serif"in quotes is read as a font name, not the generic keyword. Writeserif. - Forgetting quotes on names with spaces. Names such as
Times New Romanshould be quoted. Single-word names such asGeorgiado not need quotes. - A fallback from a different family. A script stack that ends in
monospacechanges the mood completely when the fallback is used. - Assuming a font is installed. Georgia and Arial are widely available, but none of the named fonts is guaranteed on every device. Web fonts loaded with
@font-facehelp, but the stack still needs fallbacks while the file loads or if it fails.
A practical stack-writing routine
- Pick the family for the role, using the choices you made in earlier chapters.
- List your preferred font first.
- Add one or two common fonts of the same family as backups.
- End with the closest generic keyword.
- Test by renaming your first font to a name that does not exist. Check that the page still looks acceptable.
Exercise
A stylesheet sets .quote { font-family: "Snell Roundhand", monospace; } for an elegant script pull quote. Name two problems with this stack and write a better one.
Show answer
The stack has only one named font, so there is no intermediate backup. It also ends in monospace, a fallback from a different family that gives the quote a code-like look if Snell Roundhand is missing. A better stack is .quote { font-family: "Snell Roundhand", "Brush Script MT", cursive; }, which backs up with another script and ends in the cursive generic.
Exercise
You need a stack for a slab serif headline in Rockwell. Which generic keyword do you end with, and why?
Show answer
End with serif. CSS has no generic family for slab serif, and a slab serif is a kind of serif, so serif is the closest fallback.