Why text flashes or jumps while a font loads
Text flashes or jumps because the browser has to decide what to show before the font file arrives. It has two choices: hide the text, or show it in a stand-in font and change it later.1
You can pick which one happens, with a single line of CSS.
The two things people notice
- Invisible text. The space is there, but the words aren't. During the block period, an invisible fallback font is drawn if the web font hasn't loaded.2
- A jump. Text appears in one font, then changes to another. If the two fonts take up different amounts of space, the layout shifts.1
What browsers do by default
Without any setting, browsers differ:
- Chromium-based browsers and Firefox block text rendering for up to 3 seconds if the web font hasn't loaded.1
- Safari blocks text rendering indefinitely.1
So a slow font can mean a blank page of words. That's why it's worth setting this yourself.
The font-display setting
It goes inside the @font-face rule, one per font.3 It controls two stretches of time.2 First comes the block period, with invisible text. Then comes the swap period, when a late font can still replace the stand-in.
| Value | What MDN says it does |
|---|---|
auto |
The browser decides |
block |
Short block period, infinite swap period |
swap |
Extremely small block period, infinite swap period |
fallback |
Extremely small block period, short swap period |
optional |
Extremely small block period, no swap period |
Those descriptions come from MDN's reference page.2
Which one to choose
web.dev says the right choice depends on your priorities.1
swap: show text at once, use the web font when it arrives. The downside is the jarring shift if the font arrives late.1optional: put speed first. The downside is that the web font won't be used if it arrives late.1block: always want the web font. Text shows later. It can still cause a shift, because the invisible text reserved space for the fallback font.1fallback: a middle road. The block period is extremely small and the swap period is short.2
MDN's own walkthrough uses font-display: swap.3
Cutting the jump itself
- Choose a fallback with a similar size and width. The less the space changes, the less the page moves.
- Make sure the file arrives early. web.dev suggests delivering the font early enough that it doesn't cause a layout shift.1
- Use fewer files. Fewer downloads means fewer chances for a late one. See [page:how-many-fonts-are-too-many].
- Use a system font for body text. Nothing is fetched, so nothing swaps. The caveats about long paragraphs are on that page. See [page:system-fonts-and-font-stacks].
Keep a fallback in the list either way, so there's always something to show if the web font can't load.3
A simple way to test
- Open your page in a private window.
- Slow the connection with your browser's developer tools.
- Reload and watch the text.
If you see blank space, try swap. If you see a big jump, try a closer fallback or optional.
Cited material
- Best practices for fonts, web.dev (Google)
- font-display - CSS, MDN Web Docs (Mozilla)
- Web fonts - Learn web development, MDN Web Docs (Mozilla)
