On slow 3G, the blog page in the Lab is ready to draw at 500 ms. The heading? Not visible yet. It only shows up at 2,900 ms. For 2.4 seconds the visitor stares at a page with no heading, all because of one font file that hasn't arrived.
A custom font is a font file the browser has to download first, such as Inter, rather than a font already on the device. Until that file arrives, the browser has to decide: hide the text for now, or show it in a backup font (the fallback) and swap it later. Picture a class presentation where the projector isn't on yet. You can stand silently until the slides appear (the class stares at nothing), or start explaining on the whiteboard and move to the slides once the projector is ready. font-display is the rule that picks between those two.
The example page
The blog page in the exercise has a single heading, <h1>Hello World</h1>, set in Inter. Here is what happens (simulated numbers, the same as in the Lab):
- At 500 ms the HTML and CSS are ready, and the browser is ready to draw the page (the first paint).
- Only then does the browser learn that the heading needs Inter, so
inter.woff2starts downloading at 500 ms. - On 4G the download takes 300 ms: the font arrives at 800 ms. On slow 3G it takes 2,400 ms: the font arrives at 2,900 ms.
From 500 ms until the font arrives, the heading has to be drawn with something. That's where font-display comes in.
FOIT and FOUT
As soon as the browser needs a font that hasn't arrived, the font's clock starts. There are two periods:
- Block period: the text is hidden (drawn transparent, its space still kept). If the font arrives during this period, the text shows in that font straight away.
- Swap period: once the block period is over, the text shows in the fallback font. If the font arrives during this period, the browser swaps to the custom font.
- If the swap period also runs out before the font arrives, the fallback stays until the page is closed.
The two effects a visitor can see have names:
- FOIT (flash of invisible text): the text can't be seen during the block period. The page looks empty, and LCP is delayed too when its biggest element is text.
- FOUT (flash of unstyled text): the text shows in the fallback right away, then changes to the custom font. It's readable quickly, but there's a moment when the letters change.
The four font-display values
| Value | Block period | Swap period | On slow 3G (font arrives at 2,900 ms) | Good for |
|---|---|---|---|---|
block |
about 3 seconds | unlimited | invisible from 500 to 2,900 ms, then Inter | icon fonts, which are wrong when shown in a fallback |
swap |
almost 0 | unlimited | fallback at 500 ms, Inter at 2,900 ms | body text and headings that must be readable fast |
fallback |
about 100 ms | about 3 seconds | invisible until 600 ms, fallback, Inter at 2,900 ms | fonts that may swap as long as it's quick |
optional |
about 100 ms | 0 | invisible until 600 ms, then fallback until the page closes | decorative fonts, pages that must never change font |
The font clock starts at 500 ms, so the block period of block ends at 3,500 ms. The font arrives at 2,900 ms, before the block period ends, so the text stays hidden until 2,900 ms and then shows in Inter straight away. So with block, the heading can only be read at 2,900 ms: that's the 2.4 seconds without a heading from the start of this lesson.
- The 3 seconds and 100 ms are what the CSS spec recommends. Each browser may differ a little.
auto(the default when you write nothing) leaves it to the browser, and most browsers treat it likeblock.- With
optional, the font file is usually still downloaded and kept in the cache. The next page on the same site uses Inter right away.
The code in the exercise
<head>
<title>Blog Post</title>
<link rel="preload" href="/fonts/inter.woff2" as="font" type="font/woff2" crossorigin>
<style>
@font-face {
font-family: "Inter";
src: url("/fonts/inter.woff2") format("woff2");
font-display: swap; /* text shows in the fallback right away */
}
body {
font-family: "Inter", system-ui, sans-serif;
}
</style>
</head>
@font-faceregisters the custom font: its name (font-family: "Inter") and its file (src). It only registers it; no element uses it yet.format("woff2"): WOFF2 is the smallest compressed font format, supported by every modern browser.font-display: swaphas to sit inside that same@font-faceblock. Outside@font-face(onbody, say), the browser ignores it.font-family: "Inter", system-ui, sans-serifonbodyuses the font, with a list of backups.system-uiis the system's own font: San Francisco on Mac and iPhone, Segoe UI on Windows, Roboto on Android.sans-serifis the last backup.rel="preload"tells the browser to download the file now, before it realises it needs it.as="font"is the kind of file,type="font/woff2"its format.crossoriginis required for fonts, even when the file is on your own domain. Fonts are always fetched in CORS mode, and a preload withoutcrossorigindoesn't match the real request, so the font downloads twice.- The
hrefin the preload and theurl()insrcmust be exactly the same file.
Preload: the font arrives before the first paint
With a preload, the browser starts downloading the font at 100 ms, while it reads the <head>, instead of at 500 ms.
| 4G (300 ms) | Slow 3G (2,400 ms) | |
|---|---|---|
| no preload | Inter shows at 800 ms | Inter shows at 2,900 ms |
| with preload | the font arrives at 400 ms, the heading is in Inter from the first paint at 500 ms | Inter shows at 2,500 ms |
On 4G with a preload, the font is there before the first paint, so there's no FOIT or FOUT at all, whatever the font-display value. On 3G, the preload brings Inter 400 ms earlier, and swap still keeps the text readable at 500 ms. Preload only the fonts used on the first screen, one or two files. Preloading too many makes fonts compete with the CSS and the main image.
🎯 No jumping: a fallback with matching size
FOUT has a side effect: system-ui letters are wider or narrower than Inter's, so when the font swaps, lines of text can rewrap and the content below shifts. That shift counts toward CLS. The cure is a fallback font sized to match Inter:
@font-face {
font-family: "Inter Fallback";
src: local("Arial");
size-adjust: 107%;
ascent-override: 90%;
descent-override: 22%;
line-gap-override: 0%;
}
body {
font-family: "Inter", "Inter Fallback", system-ui, sans-serif;
}
local("Arial") uses the Arial already on the device. size-adjust scales its letters to Inter's width, and the three *-override lines match the line height. The numbers above are a rough example for Inter over Arial. Tools such as Fontaine or next/font work them out for you.
📌 Make the font file small
- Self-host: put the font file on your own domain. Since 2020 browsers keep a separate cache per site, so a font from Google Fonts is no longer "already cached from another site". Self-hosting also saves a connection to another domain and can be preloaded.
- Subset with
unicode-range: a full font file holds the letters of hundreds of languages. Split it by groups of characters, and the browser only downloads what the page uses. A page in English only needs the Latin subset, for exampleunicode-range: U+0000-00FF;. - Variable fonts: one file holds every weight. Write
font-weight: 100 900;in@font-face, then use any number, such asfont-weight: 650. One variable file is usually smaller than five static files.
⚠️ A font downloaded twice, or a rule that's ignored
- Preloading without
crossorigin. There's no error, but the font downloads twice and the preload is wasted. - Putting
font-displayonbody. It's a descriptor that belongs to@font-face. Anywhere else it's ignored, and the text keeps theautobehaviour. font-family: "Inter"with no backup. While waiting, the browser uses its own default font (often Times New Roman), so the jump is big. Always end withsystem-ui, sans-serif.- A preload path that differs from
src./fonts/inter.woff2and/fonts/Inter.woff2count as different files: the preload is thrown away and the font downloads again.
TL;DR: A custom font doesn't exist until it's downloaded. font-display picks what shows while you wait: block hides the text for up to about 3 seconds (FOIT), swap uses the fallback right away and swaps it later (FOUT), fallback hides it for 100 ms and only swaps within 3 seconds, optional hides it for 100 ms and then never swaps. For text that must be readable fast, use font-display: swap inside @font-face, font-family: "Inter", system-ui, sans-serif on body, and <link rel="preload" as="font" type="font/woff2" crossorigin> with the same path as src. In this example, block on slow 3G hides the heading until 2,900 ms, swap shows it at 500 ms.
⚡ FOIT and FOUT
Meaning: Until a custom font arrives, the browser has to choose. FOIT: the text is hidden for now, and the page looks empty. FOUT: the text shows in a backup font (the fallback) right away, then swaps once the font arrives.
⚡ Pick a font-display
Guess first: your blog's body text must be readable as fast as possible, even before the font has finished downloading. Which value?
swap. The fallback shows right away, then swaps to the custom font. block hides the text for up to about 3 seconds, and optional never swaps after 100 ms.
Write:
@font-face {
font-family: "Inter";
src: url("/fonts/inter.woff2") format("woff2");
font-display: swap;
}
Meaning: font-display has to sit inside @font-face. On body, the browser ignores it.
⚡ Preload and a backup
Write:
<link rel="preload" href="/fonts/inter.woff2" as="font" type="font/woff2" crossorigin>
Meaning: A preload starts the font downloading earlier, and crossorigin is required so it isn't downloaded twice. On body, end the font list with backups: font-family: "Inter", system-ui, sans-serif.