Sub topic 5
Writing font-family stacks with fallbacks
This chapter assumes you know basic HTML, the link element, CSS selectors, and the font-family property. You should also be able to open your browser's developer tools. By the end, you will be able to write a font stack that still looks reasonable when your web font is slow or missing.
Why a single font name is not enough
A web font is a file the browser must download. Until it arrives, or if it never arrives, the browser needs another font to draw the text. The font-family property accepts a comma-separated list for this reason. The browser walks the list from left to right and uses the first font it can use.
This list is called a font stack. A good stack has three parts:
- The web font you chose, such as Inter.
- System fonts that resemble it and are already on the user's device.
- A generic family keyword as the last resort.
A working stack
body {
font-family: "Inter", system-ui, -apple-system, "Segoe UI", Arial, sans-serif;
}Here is how each entry works:
"Inter"is the web font loaded from Google Fonts, Adobe Fonts, or your own server.system-uiasks for the operating system's default interface font.-apple-systemis an older keyword that Safari and some Apple browsers still recognize."Segoe UI"is the default on Windows.Arialis installed on almost every desktop system.sans-serifis a generic family. Every browser must provide one, so the stack always resolves to something.
How the browser chooses
The browser does not stop at the first name it recognizes. It checks whether each font is available and whether it can draw the characters on the page. If the web font is still downloading, the browser either hides the text briefly or shows the next font, depending on font-display. The next chapters cover that setting. For now, remember that the fallback fonts are what users see during loading.
Matching the generic family to the typeface
Pick the generic keyword that matches your chosen font's category from the first chapter. If the fallback has the wrong shape, the page looks wrong while loading.
| Typeface type | Generic keyword | Example stack ending |
|---|---|---|
| Sans-serif | `sans-serif` | `Arial, sans-serif` |
| Serif | `serif` | `Georgia, "Times New Roman", serif` |
| Monospace | `monospace` | `"Courier New", monospace` |
A serif heading font that falls back to a sans-serif looks like a different design for a moment. When the real font arrives, the text changes shape.
Stacks for Adobe Fonts
Adobe Fonts gives you a CSS family name for each font in your web project. Use that name first and add fallbacks after it.
h1, h2 {
font-family: "proxima-nova", Helvetica, Arial, sans-serif;
font-weight: 700;
}The stack works the same way as with Google Fonts. Only the first name comes from a different service.
Common mistakes
- No generic family at the end. If every named font is missing, the browser uses its default, which is often a serif.
- Misspelled names. The browser ignores an unknown name without warning and moves on.
- Fallbacks that differ a lot in size. Fonts at the same
font-sizecan look larger or smaller. The text may jump in size when the web font loads. - Setting a stack on every element. Set it once on
bodyand let it inherit. Override it only where you need a different font, such as headings or code.
Try it
Create a page with a heading and a paragraph. Set this stack on body, then block the Google Fonts request in the Network tab of your developer tools and reload the page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Fallback test</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">
<style>
body {
font-family: "Inter", system-ui, -apple-system, "Segoe UI", Arial, sans-serif;
}
</style>
</head>
<body>
<h1>Fallback test</h1>
<p>The quick brown fox jumps over the lazy dog.</p>
</body>
</html>With the request blocked, the text should render in your system font instead of Inter, and the page should stay readable.
Exercise
A site uses a serif web font called "Lora" for article text. Write a font-family declaration with sensible fallbacks, and explain why sans-serif would be a poor last entry.
Show answer
font-family: "Lora", Georgia, "Times New Roman", serif; The last entry should be the generic keyword that matches the font's category. If the stack ended in sans-serif, readers would see a sans-serif face while Lora loads, and the text would change shape and spacing when Lora arrived.
Exercise
Why should the generic keyword sans-serif not be written in quotes?
Show answer
Quoted, it is treated as the name of a specific font called "sans-serif", which almost no system has. Unquoted, it is the generic family keyword that every browser maps to a real font.