How do you load web fonts without layout shift?
IntermediatePerformance interview questionBy UIRound, updated 23 November 2026
Short answer
Web fonts cause layout shift when text first renders in a fallback font and then reflows as the web font, with different widths and line heights, swaps in. To prevent it: choose a font-display value deliberately, preload the one or two critical WOFF2 files with crossorigin, make the fallback match the web font's metrics with size-adjust and the override descriptors, and subset fonts so they arrive sooner.
Why fonts shift the page
The browser only discovers a font when it builds the render tree and finds text that uses it, which is late: after the HTML and CSS have arrived. Until the font file loads, the browser either hides the text or draws it in a fallback. When the web font arrives, every line is re-laid out. If the fallback is narrower, paragraphs wrap differently, headings change height and everything below moves. That movement counts toward CLS, and the late font can also delay LCP when the largest element is text.
So there are two separate jobs: get the font earlier, and make the swap harmless when it happens.
font-display: what happens while the font loads
font-display in @font-face controls two periods: a block period, where text is drawn invisibly, and a swap period, where fallback text is shown and the web font replaces it if it arrives.
| Value | Block period | Swap period | Result |
|---|---|---|---|
auto |
Browser default, usually like block |
Browser default | Usually invisible text, then a swap |
block |
Short (browsers use about 3 seconds) | Unlimited | Invisible text, then a swap |
swap |
Extremely small | Unlimited | Fallback immediately, swap whenever the font arrives |
fallback |
Extremely small | Short (about 3 seconds) | Swap only if the font is quick; otherwise keep the fallback for this page |
optional |
Extremely small | None | Use the font only if it is already available almost at once, for example from cache |
swap is the common choice (Google Fonts' embed code and next/font use it by default) and is right for brand fonts, but it guarantees a swap on a cold load, so it needs a matched fallback. optional gives zero font shift by design: on a slow first visit the user sees the fallback, and the font is cached for the next page. It suits body text where the fallback is acceptable. Avoid block for body text: invisible text for seconds is worse than a shift.
Preload the critical font, with crossorigin
<link rel="preload" href="/fonts/inter-latin-var.woff2" as="font" type="font/woff2" crossorigin />Preloading tells the browser about the file from the HTML, so it downloads in parallel with the CSS rather than after layout. The crossorigin attribute is required even for same-origin fonts, because fonts are always fetched in CORS mode; without it the preload is fetched in a different mode, does not match, and the font downloads twice. Preload only the one or two files used above the fold, since every preload competes with the LCP image. The details are in preload vs prefetch vs preconnect, and choosing resource hints for a landing page lets you practise the choice.
Match the fallback: size-adjust and metric overrides
The real fix for a swap shift is a fallback that takes up the same space. You declare a second @font-face that points at a local system font and adjusts its metrics:
@font-face {
font-family: "Inter";
src: url("/fonts/inter-latin-var.woff2") format("woff2");
font-weight: 100 900;
font-display: swap;
}
/* Arial scaled to occupy the same space as Inter. Values are computed from both fonts' metrics. */
@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;
}size-adjustscales the fallback's glyphs so average character widths match, which keeps line breaks the same.ascent-override,descent-overrideandline-gap-overrideset the vertical metrics, so line boxes have the same height.
Do not guess the numbers. Tools such as Capsize and Fontaine compute them from the font files, and frameworks do it at build time. size-adjust works in all current browsers. The three override descriptors work in Chromium and Firefox but not in Safari, so on Safari you get width matching without the vertical correction, which still removes most of the reflow. The swap now changes the letter shapes but not the layout. Fixing all the shift sources together is fixing layout shifts from ads, web fonts, images and late banners.
Make fonts smaller: subsetting and unicode-range
A full font with every script can be hundreds of kilobytes. Subsetting removes glyphs you do not use (with pyftsubset from fonttools, or glyphhanger), and a variable font replaces several weight files with one. Serve WOFF2 only; every current browser supports it.
unicode-range lets one family be split by script. The browser downloads only the files whose range covers characters actually on the page:
@font-face {
font-family: "Noto Sans";
src: url("/fonts/noto-latin.woff2") format("woff2");
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+2000-206F;
}
@font-face {
font-family: "Noto Sans";
src: url("/fonts/noto-devanagari.woff2") format("woff2");
unicode-range: U+0900-097F;
}An English page then never downloads the Devanagari file. That matters for multilingual products like a multilingual Indian news app.
Self-hosting versus Google Fonts
Google Fonts handles subsetting and unicode-range for you, but the page must fetch CSS from fonts.googleapis.com and files from fonts.gstatic.com, which costs extra connections before the font can start. The old argument that users already have the font cached from other sites no longer holds: browsers partition the HTTP cache by top-level site, so nothing is shared across sites. Self-hosting on your own origin removes the extra connections, lets you preload, and avoids sending visitor IP addresses to a third party, which matters for privacy reviews.
next/font in Next.js is a good example of all of the above done automatically. It downloads Google Fonts at build time and serves them from your own domain, so the browser sends no requests to Google. It defaults to display: 'swap', preloads the font files on the routes that use them, and with adjustFontFallback (on by default) generates a fallback @font-face with size-adjust and the override descriptors.
Follow-up questions interviewers ask
Is FOUT or FOIT worse?
A flash of invisible text (FOIT) hides content, which is worse for users and can delay LCP. A flash of unstyled text (FOUT) shows content immediately; with a metric-matched fallback it causes no layout shift, so it is usually the better choice.
Does preloading fix layout shift on its own?
No. It makes the font arrive earlier, so the swap often happens before first paint, but on slow connections it still arrives late. You still need optional or a matched fallback.
When would you load fonts with JavaScript?
When you want all faces to swap in together with one class change, or to give up after a timeout, using the CSS Font Loading API (FontFace and document.fonts). You can build that in a FontFace loader with a timeout and a single class swap.
How many font files should a page load?
As few as possible. One variable font file for the main family, maybe one for headings, and subsets per script. Every extra weight or style is another file that can arrive late.
Sources
Practise it in code
Interviewers rarely stop at the definition. These questions open in an editor with tests.
- Fix layout shifts from ads, web fonts, images and late bannersHard40 min
- Load web fonts with the FontFace API: a timeout, a fallback and a single class swapMedium35 min
- Choose resource hints for a landing page: preload, preconnect, prefetch, modulepreload and
fetchpriorityMedium30 min - Design a multilingual Indian news app: Indic fonts, transliteration input, Urdu right-to-left, per-language SEO and low bandwidthHard55 min
- Design a news article page like the New York Times: ads without layout shift, a metered paywall, speed, SEO and sharingHard55 min