A web font is a file your visitor's browser has to download before it can draw your text in your chosen typeface. The catch is when that download starts. It starts later than most people expect.
The order of events
The browser can't ask for a font until it knows the page needs it. To know that, it first has to read the page and its CSS (the style rules).1
In web.dev's description, the steps run like this:
- The browser asks for the HTML page, then starts reading it.1
- It spots the CSS and other files, and asks for those too.1
- Once it has all the CSS, it combines it with the page structure into what's called the render tree.1
- Font requests go out only after the render tree shows which font variants the text needs.1
- The browser lays out the page. If the font hasn't arrived, it may not draw any text yet. Once the font is there, the text appears.1
That gap is the root of the "blank text" problem. The browser can draw the page layout, but the words wait for the font.1
Declaring a font isn't the same as using it
Many people assume a font downloads as soon as the browser reads the @font-face rule. It doesn't. That rule only gives the font a name and says where the file lives.2
The file is downloaded only if styling on the page refers to it. If your rule is for a heading font and the page has no heading, the browser can skip the download.2
Two sides of the same fact:
| If you... | Then the browser... |
|---|---|
| Declare a font but nothing uses it | Doesn't download it2 |
| Use the font in a style on the page | Downloads it, after it has worked out the font is needed1 |
So the stylesheet matters as much as the font file. Changing how your CSS is delivered can change when fonts arrive.2
Why it matters for a small site
A full font file can include every style and every character, and a full font can easily result in a multi-megabyte download.1
If you use a font service, the service writes the @font-face code for you and serves the files. You add a line or two to your site.3 That's handy, but the loading order above still applies. The service's files still have to be asked for, and fonts from another site need their own connection set up first.2
One more detail trips people up. Font files must be sent over a CORS connection, unlike stylesheets.2
What you can change
You have three levers, and each gets its own page here.
- When the browser shows text while it waits. That's the
font-displaysetting. See font-display explained. - How big the files are. Format and number of weights both count, since a full font can be a multi-megabyte download.1 See font file formats and how many fonts are too many.
- Where the files come from. See self-hosting or hosted fonts.
Preloading is another lever. It asks for a font early, but web.dev says it offers no guarantees that the font is ready when the text is drawn.1 To see the real order on your own site, follow checking font loading in browser tools.
Cited material
- Optimize WebFont loading and rendering, web.dev (Google)
- Best practices for fonts, web.dev (Google)
- Web fonts, MDN Web Docs (Mozilla)
