# Fonts & Web Font Loading

> How font-family fallback chains work, how @font-face loads custom fonts, and how font-display stops the blank-text flash.

Subject: CSS | Lesson 10 of 44 | 7 min read
Interactive lesson: https://codewithanimation.in/css/fonts-and-web-fonts
Previous: [Overflow, Outline & Display](https://codewithanimation.in/css/overflow-and-display.md) | Next: [Font Sizing & Spacing](https://codewithanimation.in/css/font-sizing-and-spacing.md)

## What a font stack is

`font-family` takes a comma-separated list, and the browser uses the first family it can find. If the first is missing, it tries the next, and so on. The last entry should always be a generic family like `sans-serif`, `serif`, or `monospace`, because those are guaranteed to exist on every device.

A real stack looks like `font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;`. Quotes are only needed when a name has spaces. The browser does not guess: it walks the list top to bottom and stops at the first hit.

Custom fonts that are not on the user's system are loaded with `@font-face`. You name the family, give a `src` (the file URL, with `format()` hints), and optionally pin `font-weight` and `font-style` so the right face is picked for bold or italic text.

## Why the fallback chain matters

If you write `font-family: "MyBrandFont";` with no fallback and that font fails to load, the browser drops back to its default, which is usually a serif. Your whole interface suddenly looks wrong, and on slow connections the text may be invisible for seconds.

A good chain degrades gracefully: the brand font, then a similar widely-available font, then a generic family. The page always reads well, even before the web font arrives.

Loading custom fonts also costs bandwidth and time. One variable font file can replace six static weights, and `font-display` lets you show readable text instantly instead of hiding it while the font downloads.

## How to load a web font

Declare the face with `@font-face`, list the modern `woff2` format first, and use `local()` so a user who already has the font installed does not download it again:

`@font-face { font-family: "Brand"; src: local("Brand"), url(brand.woff2) format("woff2"); font-weight: 400; }`

Then set `font-display` to choose the load behavior. `swap` shows fallback text almost instantly and swaps in the web font when ready, which is best for body text. `block` hides text briefly (the FOIT flash), which suits icon fonts where a fallback glyph would look wrong.

For a variable font, one file holds the whole `wght` axis. Drive it with `font-variation-settings: 'wght' 450;` (or plain `font-weight: 450`) to pick any weight, not just 400 and 700.

## Common mistake: Forgetting the generic fallback

Writing `font-family: "BrandSans";` with no trailing generic family is the #1 font mistake. If the web font fails (bad URL, blocked CDN, offline dev), the browser silently falls back to its default serif, and an entire app can look broken with no error in the console.

Always end the list with a generic family. The cost is zero and it is the only thing that guarantees readable text in every failure case.

## Tip: Match font-display to the font's job

Use `font-display: swap` for body text so readers never stare at blank space. Use `optional` for decorative fonts on flaky networks: it shows the fallback and only swaps if the font is already cached, so returning visitors get the brand font but first-time visitors never see a jarring flash.

Reserve `block` for icon fonts and logo glyphs where a fallback character would be visibly wrong.

## Try it: Try it

Watch a font-family chain resolve, see the four `font-display` strategies as timelines, and sweep a variable font weight axis live.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/fonts-and-web-fonts

## Check yourself

### Question 1

Which font-display value shows fallback text almost immediately instead of hiding it while the web font loads?

- A. swap
- B. block
- C. optional

Correct answer: A

swap gives a tiny block period (about 100ms) then renders fallback text immediately and swaps the web font in when it arrives. This is the FOUT pattern and is best for readable body text.

- Why not B: block hides the text for a short period (the FOIT flash), which is the opposite of showing fallback text right away.
- Why not C: optional may show fallback, but on slow networks it often never swaps in the web font at all, so it is not the best choice when you want a reliable swap.

### Question 2

What does local("Brand") do inside an @font-face src?

- A. It checks whether the user already has a font named "Brand" installed, and uses that copy before downloading.
- B. It downloads the font from a local CDN mirror closest to the user.
- C. It sets the display name shown in the browser's font picker.

Correct answer: A

local() lets you reuse a font the user already has, skipping the network download entirely. List it before url() so an installed copy wins, then fall back to the file.

- Why not B: local() has nothing to do with CDNs. It only looks for an already-installed system font by that name.
- Why not C: The name comes from the @font-face font-family descriptor, not from local(). local() only probes for an installed copy.

### Question 3

Why should every font-family stack end with a generic family like sans-serif?

- A. So the text still renders sanely if every named font is unavailable.
- B. It is required syntax or the rule is invalid.
- C. It makes the web font file download faster.

Correct answer: A

A generic family (sans-serif, serif, monospace) is guaranteed to exist on every device. Ending the stack with one means a missing or failed font still produces readable text instead of the browser default serif.

- Why not B: It is not required syntax. The rule is valid without it, but then the browser falls back to its own default, which is usually a serif.
- Why not C: The fallback list never affects download speed. It only changes which installed font renders when a named one is missing.

## Remember this

- End every `font-family` stack with a generic family (`sans-serif`, `serif`, `monospace`) as a safety net.
- Prefer `font-display: swap` for body text so users never stare at blank space while a font loads.
- List `woff2` first in `@font-face` `src`, and use `local()` to reuse an installed copy before downloading.
- One variable font file replaces a folder of static weights; drive any value with `font-variation-settings: 'wght' 450`.
