Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 37 ยท Images, Fonts and Media

Web Fonts and Performance

Load custom fonts fast with WOFF2, subsetting, font-display, preload and variable fonts, and avoid invisible text and layout shifts with matched fallback metrics.

Custom fonts make a site look distinctive, but they are files like any other and they have two special costs. First, the browser discovers a font late: only after it has parsed the CSS and found text that uses it. Second, while the font loads the browser must decide what to show, and that choice can leave text invisible or make the layout jump.

This lesson shows how to make fonts small, early and stable.

The cost of a font

Text rendering waits on fonts in two ways:

  • FOIT (flash of invisible text): the browser hides text until the font arrives.
  • FOUT (flash of unstyled text): the browser shows a fallback font first, then swaps.

FOIT hurts LCP and readability; the swap in FOUT can cause layout shift when the two fonts have different widths. The goal is a fast swap between two fonts that look alike.

Use WOFF2

WOFF2 is the best format for the web: it uses Brotli compression and is typically 30 percent smaller than WOFF, and every modern browser supports it. You rarely need anything else.

CSS
@font-face {
  font-family: "Inter";
  src: url("/fonts/inter-latin.woff2") format("woff2");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

font-display

font-display controls what happens while the font loads.

Value Behaviour Use it for
auto Browser default (usually like block) Rarely
block Short invisible period, then fallback, swap when ready Icon fonts
swap Fallback at once, swap whenever the font arrives Body text, most cases
fallback Very short invisible period, swap only if the font arrives quickly Text where swaps would be jarring
optional Very short block; the font is used only if it is already cached or arrives almost instantly Best for performance and stability

optional gives the best Core Web Vitals: no late swap and no shift. The font appears from the second page view onward, once it is cached.

Preload the important font

Because fonts are found late, preload the one or two files used above the fold:

HTML
<link rel="preload" href="/fonts/inter-latin.woff2" as="font" type="font/woff2" crossorigin>

crossorigin is required even for same-origin fonts, because fonts are fetched in CORS mode; without it the preload is wasted and the font downloads twice. Do not preload every weight; two files at most.

Subsetting

A full font may contain thousands of glyphs for many languages. A page in English needs only a few hundred. Subsetting keeps only the characters you use, often cutting a file by 80 percent or more. Tools such as pyftsubset (fonttools) or glyphhanger do it.

Split by language ranges with unicode-range, so the browser downloads only the files whose characters appear on the page:

CSS
@font-face {
  font-family: "Inter";
  src: url("/fonts/inter-latin.woff2") format("woff2");
  unicode-range: U+0000-00FF, U+0131, U+2000-206F;
}
@font-face {
  font-family: "Inter";
  src: url("/fonts/inter-cyrillic.woff2") format("woff2");
  unicode-range: U+0400-045F;
}

Variable fonts

A variable font packs many weights and widths into one file, controlled by axes such as wght. Instead of four files (regular, medium, bold, black), you ship one. It is smaller overall when you need three or more styles, and it unlocks smooth weight changes.

CSS
@font-face {
  font-family: "InterVar";
  src: url("/fonts/inter-var.woff2") format("woff2");
  font-weight: 100 900;
  font-display: swap;
}
h1 { font-weight: 750; }

Fewer fonts

Each additional family, weight or style is another request. Limit yourself to one or two families and the weights you truly use. Often a system font stack is all you need and is free:

CSS
body { font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
Check which fonts the browser has loaded

document.fonts is the Font Loading API. You can create new FontFace(...), call .load(), and add it to document.fonts for fine control, or use document.fonts.load("1em Inter") to start a load early.

Matching the fallback to avoid shift

When the real font swaps in, different glyph widths and line heights re-flow text. You can adjust a local fallback to match using the size-adjust, ascent-override, descent-override and line-gap-override descriptors:

CSS
@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", sans-serif; }

Frameworks such as Next.js and tools like Fontaine calculate these numbers for you. The result: the swap barely moves anything.

Measure how much a fallback differs

The result depends on the fonts installed on your device (some systems map both names to the same font, giving 100%). Fonts with very different widths cause large shifts when they swap, which is why measuring and adjusting a fallback is worthwhile.

Self-host or third-party?

Hosting fonts on your own origin avoids an extra connection and gives you full control of caching and headers. A third-party font service requires another DNS, TCP and TLS setup (latency); if you must use one, preconnect to it.

flowchart LR
  A["Subset to needed glyphs"] --> B["Convert to WOFF2"]
  B --> C["Self-host + long cache"]
  C --> D["Preload the key file"]
  D --> E["font-display + matched fallback"]

A complete font loading recipe.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which font-display value gives the most stable layout by skipping late swaps?

    optional uses the web font only if it is available almost immediately, so there is no late swap.
  2. 2. Why must a font preload include crossorigin?

    The preload request mode must match the font request, otherwise the preload is not reused.
  3. 3. What is subsetting?

    A subset keeps only the characters you need, reducing file size a lot.
  4. 4. What is the best default font file format?

    WOFF2 has the best compression and is supported by all modern browsers.

Summary

  • Serve WOFF2, subset it, and preload only the one or two critical files with crossorigin.
  • Use font-display: swap or optional, and match fallback metrics to avoid layout shift.
  • Variable fonts replace many files when you need several weights; system fonts cost nothing.
  • Self-host when you can, or preconnect to the font host.

Next: Video and Audio Performance