Sub topic 10
Choosing font families and fallback stacks
This chapter covers the font-family property: how the browser picks a font from a list, how to build that list, and which mistakes break it. It assumes you know basic HTML, CSS selectors and the cascade, and how to link a stylesheet. Font size, weight, and style come in the next chapter. Loading your own font files with @font-face comes after that.
What font-family does
font-family takes a comma-separated list of candidates, called a font stack. The browser works through the list in order and uses the first font that is available. Fonts can be missing because the visitor's device doesn't have them, or because a web font hasn't loaded.
body {
font-family: "Helvetica Neue", Arial, sans-serif;
}Here the browser tries Helvetica Neue first, then Arial. If neither exists, it uses the system's default sans-serif font. The last entry should always be a generic family, because it is the one entry guaranteed to resolve to something.
font-family is an inherited property. Set it once on body or html, and almost every element picks it up. Override it only where a part of the page needs a different voice, such as code samples.
Two kinds of names
A stack mixes two kinds of entries.
- Specific family names name an actual font, such as
"Georgia","Segoe UI", or"Inter". They only work if that font is installed or loaded. - Generic family keywords ask the browser to choose a font of a general style. The browser always has an answer for these.
| Keyword | Meaning | Typical use |
|---|---|---|
| `serif` | Letters with small strokes at the ends | Long-form reading, editorial design |
| `sans-serif` | Letters without those strokes | Interfaces, body text |
| `monospace` | Every character has the same width | Code, tabular output |
| `system-ui` | The operating system's interface font | App-like interfaces |
| `ui-serif`, `ui-sans-serif`, `ui-monospace`, `ui-rounded` | The platform's own serif, sans, mono, or rounded UI font | System font stacks |
| `cursive`, `fantasy` | Decorative styles | Rarely a good choice |
| `emoji`, `math`, `fangsong` | Special-purpose styles | Specialized content |
The ui-* keywords are not supported in every browser. Because of that, place them early in a stack and follow them with older fallbacks.
How the browser resolves a stack
The browser does not pick one font for a whole element. For each character, it takes the first font in the stack that contains a glyph for that character. A glyph is the drawn shape of a character. If your first font has no glyph for an arrow, a Chinese character, or an emoji, that one character is drawn with a later font in the stack, or with a system fallback if no entry has it.
This has a practical consequence. The look of your page depends on the whole stack, not just the first name. A visitor without your first font gets the second, and the text may be wider or narrower. Line breaks can move because of this. Choose fallbacks that have similar proportions to your preferred font.
Quoting rules
Quote family names that contain spaces, digits, or unusual characters. Never quote generic keywords.
.good {
font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
}
.broken {
font-family: "Segoe UI", Arial, "sans-serif";
}In .broken, "sans-serif" in quotes is treated as a request for a font literally named "sans-serif". No such font exists, so the stack has no working generic fallback. The browser then uses its own default font, which is often a serif.
Unquoted names with spaces are valid CSS when every word is a valid identifier. For example, Times New Roman works unquoted. Names containing digits as a separate word, such as Source Sans 3, are invalid unquoted. Quoting every specific name avoids having to remember which case applies.
Building a stack
Build a stack from the most specific entry to the most general one.
- Your preferred font, if you load one.
- System fonts with a similar look, for visitors who lack the preferred font.
- A generic family that matches the style.
The system font stack
If you don't want to load any font files, you can use the fonts the visitor's device already has. This is fast because there is nothing to download, and text looks native on each platform.
:root {
--font-sans: system-ui, -apple-system, "Segoe UI", Roboto,
"Helvetica Neue", Arial, sans-serif;
--font-serif: "Iowan Old Style", "Palatino Linotype", Palatino,
Georgia, serif;
--font-mono: ui-monospace, "SF Mono", "Cascadia Code", Menlo,
Consolas, monospace;
}
body {
font-family: var(--font-sans);
}
code,
pre,
kbd {
font-family: var(--font-mono);
}Each stack ends in a generic keyword. The stacks live in custom properties, which are the same mechanism you used for colors earlier in this tutorial. A change to --font-sans then applies everywhere the variable is used.
The trade-off is that your text looks different on each platform. If a consistent brand look matters more than load time, load a web font and use the system stack as its fallback.
Putting a custom font first
When you load a custom font, put it first and keep the system stack behind it. The font file may be slow, blocked, or fail to load, and the fallbacks are what visitors see during that time.
:root {
--font-body: "Inter", system-ui, "Segoe UI", Arial, sans-serif;
}
body {
font-family: var(--font-body);
}The @font-face rule that defines "Inter" is covered in a later chapter. Until the font loads, the text shows in system-ui. If you list "Inter" without defining it or installing it, the browser skips it and uses the next entry.
Pairing fonts by role
A page usually needs one to three families, each with a clear role.
- Body text needs legibility at small sizes over long stretches. Most sans-serif and serif fonts with open letterforms work.
- Headings can use a different family for contrast, but too many styles looks messy. Often one family used at different weights is enough.
- Code should be monospace so that columns and indentation line up.
body {
font-family: var(--font-sans);
}
h1,
h2,
h3 {
font-family: var(--font-serif);
}
code {
font-family: var(--font-mono);
}Prefer fewer families. Each custom font is an extra download, and each pairing is a design decision that has to look right at all sizes.
Common errors
Monospace text is smaller than expected. Browsers apply a smaller default size to monospace when it is the only generic family given. If you set font-family: monospace on code, the text can look smaller than surrounding text. Listing a specific font first, such as ui-monospace, monospace, avoids that special-casing in most browsers. Check the result in your target browsers.
Form controls ignore your font. Elements such as button, input, select, and textarea don't inherit font-family by default. Browsers give them a system font. Make them follow the page:
button,
input,
select,
textarea {
font: inherit;
}font: inherit copies all font properties from the parent, including family and size, so it covers more than font-family: inherit alone.
Quoting a generic keyword. As shown earlier, "sans-serif" is a font name, not a keyword.
No generic fallback. A stack with only specific names has no safety net. On a device without any of them, the browser uses its default font, and your design choice is lost.
Missing commas or a stray semicolon. font-family: Arial sans-serif; is invalid because the two names are not separated by a comma. The browser drops the whole declaration and uses an inherited or default value. If a font change has no effect, check the Styles panel in your browser's developer tools for a declaration that is struck through.
Assuming a font is installed. Fonts like Calibri or Helvetica exist on some platforms only. Treat each specific name as optional and test with the first font unavailable.
Checking which font was used
Open your browser's developer tools, select an element, and look at the Computed or Fonts panel. It lists the font actually rendered, which can differ from the first name in the stack. Check this whenever a page looks different on another device.
Exercise
A stylesheet sets body { font-family: "Source Sans 3", "sans-serif"; }. A visitor's device doesn't have Source Sans 3 and no web font is loaded. Why might the page show a serif font, and how do you fix the declaration?
Show answer
Quoting "sans-serif" turns the keyword into a font name. No font has that name, so the browser skips it and uses its default font, which is often a serif. Remove the quotes: font-family: "Source Sans 3", sans-serif;. The name "Source Sans 3" correctly stays quoted because it contains spaces and a digit.
Exercise
Your buttons and text inputs show a different font from the rest of the page. What rule makes them match, and why is it needed?
Show answer
Add button, input, select, textarea { font: inherit; }. Form controls get a system font from the browser's default stylesheet instead of inheriting from body, so they need to be told to inherit.