Emmanuel Akinfulubi

HTML and CSSHTML and CSS

Font stacks with fallback fonts in CSS

24 A font stack that always lands somewhere

easy

Write two font stacks, one for prose and one for code, each ending in a generic family so there is always something to fall back to. Use no downloaded fonts.

Stuck? Three hints, smallest first
  1. 1. The browser walks the list left to right and stops at the first family it has.
  2. 2. The last entry must be a generic family: serif, sans-serif or monospace.
  3. 3. A family name with a space in it needs quotes: ’Times New Roman’.

index.html

<p class="prose">Set in whichever of these the reader already has.</p>
<p class="ui">Interface text, using the system font.</p>
<pre class="code">border-collapse: collapse;</pre>

style.css

.prose {
  font-family: Georgia, 'Times New Roman', Times, serif;
  font-size: 1rem;
}

/* system-ui is the operating system's own interface font: no download, and it
   already looks native on every platform. */
.ui {
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
}

.code {
  font-family: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  font-size: 0.85rem;
  padding: 8px 10px;
  border-radius: 6px;
  background: #f2f3f5;
  color: #17171a;
  overflow-x: auto;
}
Rendered result. Shown, not machine-checked: there is no browser in the build.
7 checks that prove this answer

Assertions, run in the browser. The markup and styles above are handed to them as html and css, so press Run and each check prints ok. The build fails if a single one fails.

Checks for exercise 24

// Every stack has to end in a generic family, or a reader with none of the
// named fonts falls back to the browser default and your metrics change.
const generic = ['serif', 'sans-serif', 'monospace', 'cursive', 'fantasy', 'system-ui'];
const stacks = [...css.matchAll(/font-family:\s*([^;]+);/g)]
  .map((m) => m[1].split(',').map((f) => f.trim()));
const lastOf = stacks.map((s) => s[s.length - 1]);

test('three stacks are declared', stacks.length, 3);
test('each ends in a generic family', lastOf.every((f) => generic.includes(f)), true);
test('the fallbacks are serif, sans-serif and monospace', lastOf, ['serif', 'sans-serif', 'monospace']);
test('multi-word names are quoted', /'Times New Roman'/.test(css), true);
test('every stack offers more than one option', stacks.every((s) => s.length >= 3), true);
test('no font is downloaded', /@font-face/.test(css), false);
test('and none is imported', /@import/.test(css), false);

Output


      

The stack is a preference list, not a request: the browser takes the first family it can find and never asks for the rest, so the last entry being generic is what guarantees an answer. Naming system-ui first is the cheapest way to look native on every platform, and it costs no download at all — which matters more than it sounds, because a web font is a blocking request that can leave text invisible while it loads. This course ships no fonts for exactly that reason.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.