← Back to HTML & CSS, page 4

html-css-0078 · Medium mid

How do you load a web font without hurting speed or causing layout jumps?

Declare fonts with @font-face using woff2, pick a font-display value such as swap or optional, preload the key font with crossorigin, subset or use variable fonts, and tune a fallback with size-adjust to limit layout shift.

Full answer

A custom font is a file the browser must download. Until it arrives, the browser has to decide what to show. That choice is controlled by font-display inside @font-face.

  • swap: show text at once in a fallback font, then switch when the font loads. Text is readable early, but the swap can move the layout.
  • block: hide the text for a short period, then swap. This avoids the flash of fallback text but delays reading.
  • fallback: a very short block period and a short swap window. If the font arrives late, the fallback stays.
  • optional: a very short block period, and the browser may not use the font at all on a slow connection. This gives the least movement.

Steps for a good setup:

  1. Use woff2, which is smaller than older formats.
  2. Load only the weights and styles that you use. A variable font can replace several files.
  3. Use unicode-range or a subset so users download only the characters they need.
  4. Preload the most important font with <link rel="preload" as="font" type="font/woff2" crossorigin href="...">. The crossorigin attribute is needed even for fonts on your own domain, or the preloaded file is fetched twice.
  5. Pick a fallback font with similar size. The size-adjust, ascent-override and similar descriptors in @font-face can tune a local fallback to match, which reduces the layout shift when the swap happens.

The heading is drawn first in the adjusted Arial. When brand.woff2 has loaded, it switches to the brand font, and the adjusted fallback keeps the size change small. The font file in this example is a placeholder, so the page shows the fallback.

Test on a throttled network in DevTools and check the layout shift score, not only the look on a fast connection.

html
<style>
  @font-face {
    font-family: "Brand";
    src: url("/fonts/brand.woff2") format("woff2");
    font-weight: 400;
    font-style: normal;
    font-display: swap;
  }
  @font-face {
    font-family: "Brand Fallback";
    src: local("Arial");
    size-adjust: 105%;
    ascent-override: 92%;
  }
  body { font-family: "Brand", "Brand Fallback", sans-serif; }
</style>
<link rel="preload" as="font" type="font/woff2" crossorigin href="/fonts/brand.woff2">
<h1 id="h">Learn on Schoolabe</h1>

The font file is a placeholder, so the page shows the fallback font. The check read the @font-face rule, the preload link and the font family in use. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • font-display decides what shows while the font loads.
  • swap shows fallback text first; optional may skip the font on slow networks.
  • Preload important fonts with crossorigin.
  • Use woff2, subsets and variable fonts to cut size.
  • size-adjust and ascent-override tune a fallback to reduce layout shift.

Common mistakes

  • Preloading a font without crossorigin, so the browser downloads it twice.
  • Shipping five weights of a font when the design uses two.
  • Using font-display: block for body text, which leaves a blank page on slow networks.

Follow-ups

  • Why does a font preload need the crossorigin attribute?
  • When would you choose font-display: optional?

Tips

  • Name the four common font-display values and what the user sees for each.
  • Mention fallback metric overrides as the reason that swap does not need to cause a big jump.