Paj Digital Labs
All articles
Web Design & Engineering

Web Fonts Without the Flash: Loading, Subsetting and Fallback Metrics

Custom fonts make a site feel designed, and they are a common cause of layout shift and slow text rendering. How to load them so the page reads instantly and doesn't jump when the font arrives.

Paj Digital Labs 2 min read
Web design workspace with desktop and laptop displays
Editorial illustration · Web Design & Engineering

Typography is most of what gives an editorial site its character. It is also a frequent reason the first paint shows invisible text, or the whole article shifts a few pixels when the real font finally arrives. Both are fixable, and the fixes are small.

Load fewer files

Every weight and style is a separate download. A site using regular, italic, medium, semibold and bold in two families can easily request ten font files. Audit what the design actually uses. Most editorial layouts need:

  • Body: regular, italic, bold
  • Headings: one or two weights
  • Monospace: regular

If a family offers a variable font, one file can cover the whole weight range and is often smaller than three static weights combined.

Subset aggressively

A full font file includes glyphs for many languages you may not publish in. Subsetting to the character ranges you need — Latin and Latin Extended for most English sites — can cut file size by more than half. Font services do this automatically with unicode-range; for self-hosted fonts, a subsetting tool does it at build time.

css
@font-face {
  font-family: class="text-code-string">"Plex Sans";
  src: url(class="text-code-string">"/fonts/plex-sans-latin.woff2") format(class="text-code-string">"woff2");
  font-weight: 400 700;
  font-display: swap;
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+2000-206F;
}

Serve WOFF2 only. Every current browser supports it, and older formats just add weight.

Choose a font-display strategy

| Value | Behaviour | Good for | |---|---|---| | swap | Show fallback immediately, swap when loaded | Body text — readable right away | | optional | Use the font only if it arrives very fast | Sites that prioritise stability over branding | | block | Hide text briefly while loading | Icon fonts only, and even then avoid |

For a content site, swap is usually right: readers can start reading before the font arrives.

Match the fallback metrics

The cost of swap is the jump when the web font replaces the fallback, because the two fonts have different widths and line heights. Modern CSS lets you adjust the fallback to match:

css
@font-face {
  font-family: class="text-code-string">"Baskerville Fallback";
  src: local(class="text-code-string">"Georgia");
  size-adjust: 104%;
  ascent-override: 92%;
  descent-override: 24%;
  line-gap-override: 0%;
}

h1 { font-family: class="text-code-string">"Libre Baskerville", class="text-code-string">"Baskerville Fallback", serif; }

With well-tuned overrides, the swap is nearly invisible and layout shift from fonts drops close to zero. Several tools calculate these values for popular fonts automatically.

Preload only the critical file

Preload the one or two files used above the fold — typically the body regular weight and the heading weight. Preloading everything competes with more important resources.

html
<link rel=class="text-code-string">"preload" href=class="text-code-string">"/fonts/plex-sans-latin.woff2" as=class="text-code-string">"font" type=class="text-code-string">"font/woff2" crossorigin />

The crossorigin attribute is required even for same-origin fonts; without it the preload is wasted and the font downloads twice.

Self-host or use a service?

Font services are convenient and handle subsetting. Self-hosting removes a separate connection to another domain, gives you full cache control, and avoids sending visitor requests to a third party. For performance-sensitive sites I generally self-host.

Check the result

Throttle the network in dev tools and reload. Text should be visible immediately, and the swap should cause little or no movement. Then check field data for layout shift on article pages. Fonts handled well should not show up there at all.

Paj Digital Labs

The engineering journal of Paj Digital Solutions — hosting, server infrastructure and web engineering, written by the people who run the servers.

Related reading