How to Optimize Web Font Loading
Keep a web font from blocking text or shifting layout with font-display, preload, subsetting and a size-adjusted fallback.
On this page
Introduction
A web font is a render-blocking resource if the browser waits for it before painting text, and a layout-shifting one if the fallback font has different metrics. Both are avoidable. The goals are to show text immediately with a fallback, swap to the web font when it arrives without visibly shifting the layout, and download only the characters the page needs. This article covers font-display, preloading the critical font, subsetting, self-hosting versus a third-party CDN, and the metric overrides that keep the swap from moving content.
Why Fonts Block and Shift
The browser must download the font file, parse it, and then re-layout any text using it. By default it may hide text until the font is ready or show the fallback and swap later, and the choice is controlled by font-display. If it hides text, the content is invisible during the download, which is bad for the largest contentful paint. If it swaps, the different metrics of the fallback and the web font can move the text, which causes layout shift.
A third-party font host adds a connection and a DNS lookup to an origin the page does not control, which lengthens the download. Self-hosting removes that, and it also removes the third-party from the critical path and the privacy surface.
font-display
font-display controls what happens while the font loads. swap shows the fallback immediately and swaps when the font arrives, which never hides text but can shift layout. optional gives the font a very short block period and, if it does not arrive in time, uses the fallback for that page load without swapping, so it never shifts but the first visit may not use the font. block hides text briefly then swaps.
For body text, swap or optional are the common choices: swap if the font is important and the fallback metrics are close, optional if avoiding shift matters more than using the font on the first visit. Avoid block for content text, because hiding text delays the meaningful paint.
@font-face {
font-family: 'Inter';
src: url('/fonts/inter-subset.woff2') format('woff2');
font-weight: 400;
font-style: normal;
font-display: swap; /* show fallback, swap when ready */
/* Match the fallback metrics so the swap does not shift layout. */
ascent-override: 90%;
descent-override: 22%;
line-gap-override: 0%;
size-adjust: 100%;
}
Preloading the Critical Font
A font referenced only from CSS is discovered late, after the CSS is parsed. Preloading the font file in the HTML head makes the browser start the download earlier, which shortens the swap delay. Preload only the font used above the fold and in the initial render; preloading every weight and style competes for bandwidth with the resources that matter more.
<!-- Start the critical font download early, before CSS is parsed. -->
<link rel="preload" href="/fonts/inter-subset.woff2" as="font"
type="font/woff2" crossorigin>
<!-- Only the weight used above the fold; the rest load on demand. -->
<style>
body { font-family: 'Inter', system-ui, sans-serif; }
</style>
Subsetting
A full font file contains glyphs for many languages and styles you may never use. Subsetting keeps only the characters the page needs, which can shrink a font by a large factor. For a site that uses one language, subset to that language’s common range; for a site with dynamic content, keep a broader range or use unicode-range so the browser downloads only the subset it needs per character used.
unicode-range lets you split a font into several files by character range, and the browser downloads only the ranges that the page actually renders. This keeps the critical subset small while still supporting other scripts when they appear. Generate a woff2 as the primary format, since it is smaller than the older formats and supported widely.
Avoiding Layout Shift
The shift on swap comes from the fallback and the web font having different metrics. Size-adjusted fallbacks fix this by overriding the fallback’s metrics to match the web font, so the two occupy the same space and the swap moves nothing. Set ascent-override, descent-override, line-gap-override and size-adjust on the fallback, or use a tool that computes them from the two font files.
The result is a fallback that looks slightly different but occupies the same layout, so swapping to the web font changes glyph shapes without moving content. This preserves the cumulative layout shift score while still showing text immediately. Combine it with font-display: swap for the best of both: immediate text and no shift.
Self-Hosting Versus a CDN
Self-hosting the font puts it on the same origin as the page, removing a DNS lookup and a connection to a third party, and it means the font is served from the same CDN as the rest of the assets. It also removes a third party from the privacy surface, since a font CDN can see the requests. The trade is that you must update the font yourself.
A font CDN can be convenient and can serve a cached copy from a shared cache, but the shared-cache benefit is smaller than it once was, because browsers cache per origin and cross-origin font caching is restricted. Weigh the convenience against the extra connection and the privacy exposure, and prefer self-hosting for a public site where the font is not changing constantly.
Conclusion
Keep web fonts from blocking or shifting: use font-display swap or optional so text is never hidden, preload the critical font so it arrives sooner, subset to the characters the page needs with unicode-range for other scripts, and override the fallback metrics so the swap does not move content. Self-host to remove a third-party connection and the privacy surface, and preload only the font used above the fold. Then the page shows text immediately and the font arrives without a visible jump.
Sources: MDN: font-display, web.dev: font best practices, MDN: unicode-range.
FAQ
- What is the best font-display value for body text?
- swap or optional. swap shows the fallback immediately and swaps when the font loads, never hiding text. optional avoids any shift by not swapping if the font is late. Avoid block for content text, because hiding text delays the meaningful paint.
- How do I stop a font swap from shifting the layout?
- Use a size-adjusted fallback: override the fallback ascent, descent, line gap and size to match the web font, so the two occupy the same space. The swap then changes glyph shapes without moving content, preserving the layout shift score.
Report a problem with this articleOpens a pre-filled issue on GitHub.