Font Loading Strategies — Performance

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…

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):

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:

The two effects a visitor can see have names:

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 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>

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

⚠️ A font downloaded twice, or a rule that's ignored

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.

What you will learn