Sub topic 11
Naming families in CSS: font stacks and generic fallbacks
This chapter assumes you can read basic CSS (selectors, properties, values) and that you know the five families from earlier chapters: serif, sans serif, slab serif, script, and display. Here you will turn that classification into working CSS.
What a font stack is
A font stack is the ordered list of font names you give to the font-family property. The browser reads the list from left to right and uses the first font it can find on the user's device or load from your page. If a font is missing, it tries the next one. For each character, it keeps going down the list until a font supplies that character.
body {
font-family: Georgia, "Times New Roman", serif;
}
h1, h2 {
font-family: "Helvetica Neue", Arial, sans-serif;
}The first rule sets body text in a serif face. The second sets headings in a sans serif face. Each stack ends with a generic family keyword, which is the safety net if none of the named fonts is available.
A few syntax rules matter:
- Separate names with commas.
- Put quotation marks around any family name that contains spaces or characters other than letters, digits, and hyphens, such as
"Times New Roman". Single or double quotes both work. - Do not quote generic keywords.
serifis a keyword."serif"would ask for a font literally named "serif". - Family names are not case sensitive, but a consistent style is easier to maintain.
Generic families
A generic family is a keyword that stands for a whole category of fonts. The browser or operating system decides which installed font represents it, so the result differs from one device to another. The generic keyword always goes last in the stack, because it always matches something.
| Keyword | Meaning | Typical use |
|---|---|---|
| `serif` | Fonts with serifs | Body text, long reading |
| `sans-serif` | Fonts without serifs | Interfaces, headings, body text |
| `monospace` | Every character has the same width | Code |
| `cursive` | Handwriting or script-like fonts | Short accents |
| `fantasy` | Decorative fonts | Rarely recommended |
| `system-ui` | The operating system's interface font | App-like interfaces |
CSS defines more generic keywords than this table lists, such as ui-serif, ui-sans-serif, ui-monospace, and math. The table covers the ones you will meet most often.
Mapping the five families to generic keywords
The five families from this tutorial do not map neatly onto CSS keywords. Only some have a generic family of their own.
| Family | Generic keyword | Notes |
|---|---|---|
| Serif | `serif` | Direct match |
| Sans serif | `sans-serif` | Direct match |
| Slab serif | `serif` | No generic of its own, so fall back to `serif` |
| Script | `cursive` | Approximate match |
| Display | `sans-serif` or `serif` | No reliable generic, so choose by the look of your named fonts |
Slab serif and script
.script-accent {
font-family: "Brush Script MT", cursive;
}
.slab-headline {
font-family: Rockwell, "Roboto Slab", serif;
}A slab serif is still a serif face, so serif is the closest honest fallback. If Rockwell and Roboto Slab are both missing, the heading will appear in the default serif, which is lighter and has more contrast than a slab. The layout survives, but the personality changes.
The cursive keyword needs a warning. What it maps to varies widely. Some systems pick a formal calligraphic face, others pick a casual handwriting face, and some have no cursive font at all and use a default instead. Treat cursive as a "better than nothing" fallback, and keep scripts to short, large pieces of text where a failure is not a disaster.
Display faces
CSS has no generic family that reliably means display. The fantasy keyword looks like it should, but browsers map it inconsistently, so it is not dependable. Pick the generic that gives the best-looking failure.
.display-title {
font-family: Impact, "Arial Black", sans-serif;
font-size: 4rem;
line-height: 1;
}Impact and Arial Black are heavy and condensed or wide, so sans-serif is the fallback that keeps the headline from collapsing into a delicate serif. A display face is meant for large sizes and short text, so this class pairs the stack with a large font-size and a tight line-height. The fallback will be lighter than Impact, so check that the title still reads well in it.
How the browser resolves a stack
The browser does not choose one font for the whole element and stop. It works through the stack for every character it has to draw.
Because that fence must not be nested, here is the process in the diagram below.
Two consequences follow:
- A stack can mix fonts within one line. If your first font lacks an accented letter or a symbol, that single character may come from a later font.
- A font that is not installed is skipped silently. CSS gives no error, so a typo in a family name looks the same as a missing font.
Designing a good stack
A stack has three parts.
- Your preferred font. This is the typeface you chose for the design.
- Similar fonts that are widely available. Choose faces with similar proportions and, above all, similar x-height and width. A close match keeps line breaks and page height nearly unchanged.
- A generic keyword. This matches the classification of your preferred font.
For example, a body stack built around a transitional serif might look like this:
.article {
font-family: "Source Serif 4", Georgia, "Times New Roman", serif;
font-size: 1.125rem;
line-height: 1.6;
}Georgia is a good second choice because it has a large x-height and is installed on many systems. Times New Roman is smaller on the page at the same size, so it is a weaker match and comes later.
Web fonts and the stack
If you load a font from a file with @font-face, the font name you declare becomes usable in font-family, and the stack still decides what shows while it downloads or if it fails.
@font-face {
font-family: "Roboto Slab";
src: url("/fonts/roboto-slab.woff2") format("woff2");
font-weight: 100 900;
font-display: swap;
}
.slab-headline {
font-family: "Roboto Slab", Rockwell, serif;
}The font-display: swap line tells the browser to show the fallback immediately and swap in the web font when it arrives. Without it, some browsers hide the text for a short time. The swap can cause a visible jump if the fallback has different proportions, which is another reason to choose fallbacks with a similar x-height and width.
Here the order has changed from the earlier slab example. Because the page now ships Roboto Slab itself, that font comes first, since it is the one you can guarantee.
Common errors
- Forgetting the generic family. Without it, the browser uses its default font, which is usually a Times-like serif, even when you intended a sans serif design.
- Quoting a generic keyword.
"sans-serif"is treated as a font name and does not trigger the generic fallback. - Leaving out quotes on multi-word names. Unquoted names with spaces often work, but names containing digits or special characters can break, so quote them.
- Choosing the wrong generic. A slab or display stack that ends in
serifwhen the named fonts are heavy and geometric produces a poor failure. Match the generic to what the named fonts look like. - Assuming a font is present. Georgia and Arial are common, but no font is guaranteed. Other systems substitute their own.
- Using
cursivefor body text. Script faces are hard to read in long passages, and the generic fallback is unpredictable. - Typos.
Helvetica Nueueis skipped without any warning.
Putting it together
A page often uses several families at once. Keeping the stacks in CSS custom properties gives each role one name and one place to edit.
:root {
--font-body: "Source Serif 4", Georgia, "Times New Roman", serif;
--font-heading: "Helvetica Neue", Arial, sans-serif;
--font-slab: Rockwell, "Roboto Slab", serif;
--font-script: "Brush Script MT", cursive;
--font-display: Impact, "Arial Black", sans-serif;
}
body {
font-family: var(--font-body);
}
h1, h2, h3 {
font-family: var(--font-heading);
}
.pull-quote {
font-family: var(--font-slab);
}
.signature {
font-family: var(--font-script);
}
.hero-title {
font-family: var(--font-display);
font-size: 4rem;
line-height: 1;
}Each variable ends in a generic keyword that matches its family, and the choice of keyword follows the table earlier in this chapter. Changing a typeface later means editing one line. This setup also reflects the pairing advice from earlier chapters: a serif body is contrasted with a sans serif heading, and the slab, script, and display faces appear only in small, deliberate roles.
Summary of decisions
Use this checklist when you write a stack:
- Name the font you want first.
- Add one or two common fonts with similar proportions.
- End with the generic keyword that best describes how the stack should look if everything fails.
- Test with your preferred font disabled, or in a browser where it is not installed, and check line lengths and headings.
Exercise
A designer wants a heading in the slab serif Rockwell, with Roboto Slab as a backup. Write the font-family declaration, and say which generic keyword belongs at the end and why.
Show answer
font-family: Rockwell, "Roboto Slab", serif; The generic keyword is serif, because CSS has no generic family for slab serif, and a slab is a serif face, so serif is the nearest match. "Roboto Slab" is quoted because the name contains a space, and serif is not quoted because it is a keyword.
Exercise
A stylesheet contains font-family: "Impact", "Arial Black", "sans-serif"; for a display headline. What is wrong, and what happens if neither Impact nor Arial Black is installed?
Show answer
The generic keyword sans-serif is in quotation marks, so the browser treats it as the name of a font called "sans-serif" instead of the generic family. No such font exists, so it is skipped, and the browser falls back to its default font, which is usually a serif. The fix is to remove the quotes: font-family: Impact, "Arial Black", sans-serif;.
Exercise
Why is cursive a risky last item in a stack for a script face, and what two practices reduce the risk?
Show answer
Browsers and operating systems map cursive to very different fonts, and some have no cursive font at all, so the fallback may look nothing like the intended script. To reduce the risk, use script faces only for short, large text where any fallback is acceptable, and list one or more named backup fonts before cursive that you have tested.