Sub topic 10
Choosing font families and fallback stacks
This chapter assumes you know basic HTML structure, CSS selectors and the cascade, and how to link or embed a stylesheet. You do not need to know how to load custom fonts yet. That comes in a later chapter.
How the browser picks a font
The font-family property takes a comma-separated list called a font stack. The browser checks each entry from left to right and uses the first font that is installed on the user's device or loaded by your page. If none match, it falls back to its default font.
A font stack has two kinds of entries:
- A specific family name, such as
"Georgia"or"Helvetica Neue". It works only if the font is available. - A generic family, such as
seriforsans-serif. It is a keyword that the browser always resolves to some installed font of that style.
Always end a stack with a generic family. It is your guarantee that text renders in a font of roughly the right style.
body {
font-family: "Helvetica Neue", Arial, sans-serif;
}Here the browser tries Helvetica Neue, then Arial, then whatever sans-serif font the system defines.
Generic families
| Keyword | Look | Typical use |
|---|---|---|
| `serif` | Strokes with small finishing marks | Long reading, editorial text |
| `sans-serif` | Plain strokes without finishing marks | Interfaces, body text |
| `monospace` | Every character has the same width | Code, tabular output |
| `system-ui` | The operating system's interface font | App-like interfaces |
| `cursive`, `fantasy` | Decorative styles that vary widely | Rarely a good choice |
The actual font behind each keyword differs by operating system. A sans-serif page looks slightly different on Windows, macOS, and Android. That is expected, and you should check your layout with more than one.
Quoting and naming rules
Family names that contain spaces or characters other than letters, digits, and hyphens should be quoted. Generic keywords must not be quoted. Quoting "serif" makes the browser look for a font literally named serif, not the generic family.
.note {
font-family: "Times New Roman", Times, serif;
}
.code {
font-family: "Courier New", monospace;
}Building a good stack
A useful stack is ordered from your preferred font to the safest option. Each fallback should have similar proportions to the one before it. If the fallback is much wider or narrower, the text reflows when the preferred font appears, and the page shifts.
:root {
--font-body: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
--font-heading: Georgia, "Times New Roman", serif;
--font-code: ui-monospace, "Cascadia Code", Menlo, Consolas, monospace;
}
body {
font-family: var(--font-body);
}
h1,
h2,
h3 {
font-family: var(--font-heading);
}
code,
pre {
font-family: var(--font-code);
}Defining stacks as custom properties, as in the earlier chapter on reusable colors, keeps them in one place. Changing a typeface for the whole site then means editing one line.
Common errors
- No generic fallback.
font-family: "Inter";alone leaves the result to the browser default, which is often a serif font. - Unquoted names with spaces. Some unquoted multi-word names work, but quoting them is safer and clearer.
- Quoting generic keywords.
"sans-serif"is treated as a font name, not as the generic family. - Assuming a font is installed. A font on your computer may be missing on a visitor's device. Test by removing it from the stack and checking the fallback.
- Mismatched fallbacks. A narrow preferred font followed by a wide fallback causes layout shifts when the first font loads.
Exercise
Exercise
A stylesheet sets font-family: "Open Sans"; on the body, and some visitors see text in a serif font. Why does this happen, and how do you fix it?
Show answer
"Open Sans" is not installed on those visitors' devices, and the stack has no fallback, so the browser uses its default font, which is often a serif. Add fallbacks that end with a generic family, for example font-family: "Open Sans", Arial, sans-serif;.
Exercise
Which of these is correct for a monospace stack, and why? (a) font-family: "Consolas", "monospace"; (b) font-family: Consolas, monospace;
Show answer
Option (b). The generic keyword monospace must not be quoted. In option (a), the browser looks for a font literally named "monospace" instead of using the generic family.