Sub topic 9
font-display, preconnect, and avoiding layout shifts
This chapter assumes you can write a <link> element, a font-family rule, and a font stack with fallbacks, and that you can open the Network and Performance panels in your browser's developer tools. It covers what the browser does while a web font downloads, how to control that behavior, and how to stop the page from jumping when the font arrives.
What happens while a font loads
A browser does not download a web font when it sees your <link> tag. It first downloads the HTML and the stylesheet. It then builds the page, finds text that uses a font from an @font-face rule, and only then requests the font file. That means the font request starts late, after the CSS has arrived.
While the file is in flight, the browser must decide what to do with the text. It has two choices:
- Hide the text until the font arrives. This is called a flash of invisible text, or FOIT.
- Show the text in a fallback font, then swap to the web font when it arrives. This is called a flash of unstyled text, or FOUT.
Most browsers default to a short period of invisible text, often around three seconds, before they fall back. On a slow connection, readers stare at blank space where your headline should be. The font-display descriptor lets you pick the behavior yourself.
The font-display values
font-display is a descriptor inside @font-face. It defines a timeline with three periods: a block period (text is invisible), a swap period (the web font may replace the fallback), and a failure period (the fallback stays for good).
| Value | Block period | Swap period | Typical use |
|---|---|---|---|
| `auto` | Browser decides | Browser decides | Rarely what you want |
| `block` | Short, about 3 seconds | Infinite | Icon fonts, where fallback text would be wrong |
| `swap` | Extremely short, near zero | Infinite | Body text and headings where the brand font matters |
| `fallback` | Extremely short, near zero | Short, about 3 seconds | Body text where a late swap would be disruptive |
| `optional` | Extremely short, near zero | None | Fonts that are nice to have, not essential |
The exact durations are up to the browser, but the shape is the same everywhere.
swapguarantees readable text immediately. The cost is a visible swap and possible layout movement when the font arrives, even if that is ten seconds later.fallbacklimits the swap to a short window. If the font misses it, the page keeps the fallback font for that visit, and the font is cached for the next page.optionallets the browser decide, based on connection speed, whether to use the font at all. It gives the least layout movement and the weakest guarantee that your font appears on the first visit.
Where font-display comes from in each service
With Google Fonts, you set it through the display query parameter in the CSS URL. The service writes the matching font-display value into each @font-face rule it returns.
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;700&display=swap" rel="stylesheet">Here display=swap produces font-display: swap in every generated rule. If you leave the parameter out, Google Fonts returns auto. You can use swap, fallback, optional, or block as the parameter value.
With a self-hosted font, you write the descriptor yourself:
@font-face {
font-family: "Inter";
src: url("/fonts/inter-regular.woff2") format("woff2");
font-weight: 400;
font-style: normal;
font-display: swap;
}Adobe Fonts is different. You do not write the @font-face rules, because the project stylesheet at use.typekit.net generates them. Adobe Fonts web projects expose a font-display setting in the project settings, and the generated CSS uses it. Check the current options in your project's settings page, because the interface can change. Whatever the setting, confirm the result by opening the stylesheet URL in a browser tab and reading the generated @font-face rules.
Preconnect: start the connection earlier
Before the browser can download anything from a new host, it must look up the domain name, open a TCP connection, and negotiate TLS. On a mobile network this can take several hundred milliseconds. A preconnect hint tells the browser to do that work immediately, in parallel with other tasks, instead of waiting until it needs the file.
Google Fonts involves two hosts, and each plays a different role:
fonts.googleapis.comserves the CSS file that holds the@font-facerules.fonts.gstatic.comserves the font files themselves.
That is why the snippet has two preconnect hints. The second one needs the crossorigin attribute. Browsers fetch font files in CORS mode, and a connection opened without crossorigin is not reused for them. Leaving the attribute off is a common mistake: the hint appears to be present, but the browser opens a second connection anyway and you gain nothing.
For Adobe Fonts, the equivalent hint targets the stylesheet host and the font host:
<link rel="preconnect" href="https://use.typekit.net" crossorigin>
<link rel="preconnect" href="https://p.typekit.net" crossorigin>
<link rel="stylesheet" href="https://use.typekit.net/your-project-id.css">Verify the hosts in your own Network panel. Adobe has changed its delivery hosts over time, so confirm the domains you see requests going to and preconnect to those.
Rules of thumb for preconnect
- Place the hints as early as possible in
<head>, before the stylesheet link. - Use preconnect only for hosts you will definitely use on this page. Each open connection costs memory and battery, and browsers close idle connections after a short time.
- Limit yourself to a few. Two to four preconnects is plenty.
- Do not add preconnect for a host you already self-host from. Your own origin is already connected.
Preload: skip the discovery delay
Preconnect only removes connection setup. The browser still waits to discover the font file. If you self-host, you can use preload to request the file right away, without waiting for the CSS to be parsed:
<link rel="preload" href="/fonts/inter-regular.woff2" as="font" type="font/woff2" crossorigin>The crossorigin attribute is required here as well, even though the file is on your own domain. Without it, the preloaded response is not matched to the later font request, and the file downloads twice. Preload only the one or two fonts used above the fold. Preloading every weight competes with your CSS and images for bandwidth.
You cannot reliably preload Google Fonts files, because the file URLs are generated and may change. That is one practical reason teams move to self-hosting once performance becomes a priority.
Layout shift: why the swap moves things
Cumulative Layout Shift (CLS) is a Core Web Vitals metric that measures how much visible content moves unexpectedly while the page loads. Font swaps cause shifts because two fonts at the same font-size rarely take the same space. Letters have different widths, so lines wrap at different points, paragraphs grow or shrink, and everything below them moves.
A typical case: the fallback is Arial, the web font is wider, and a heading that fit on one line now wraps onto two. The content below jumps down by one line height.
You have four tools for reducing this.
- Use
optionalorfallbackso the swap happens rarely or only early. - Choose a fallback font whose widths are close to the web font.
- Adjust the fallback's metrics so it matches the web font more closely.
- Load fewer font files, so there are fewer swaps.
Matching the fallback with size-adjust
The @font-face rule can define a local font under a new name and adjust its metrics. The descriptors are size-adjust, ascent-override, descent-override, and line-gap-override. The goal is for the fallback to occupy nearly the same space as the web font, so the swap is barely noticeable.
@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;
}The percentages above are illustrative. Real values depend on the exact font files, so do not copy them. Compute them from the font's metrics, using a tool such as the Fontaine or Capsize libraries, or the fallback generation built into some frameworks. Then test by throttling the network in developer tools and watching whether the text moves.
Note that src: local("Arial") only works where Arial is installed. On Android, for example, the equivalent might be a different family. That is acceptable: if the local font is missing, the stack falls through to the next entry.
Other causes of shift
Fonts are not the only source of layout shift, so rule out the others before blaming the font:
- Images without
widthandheightattributes. - Ads or embeds that insert themselves after load.
- Late-loading stylesheets that change line heights.
Reserve space for each of these in your markup.
Measuring the result
Do not guess. Use these checks:
- In the Network panel, set throttling to a slow profile and reload. Watch when the font file starts and finishes relative to the HTML.
- In the Network panel, check the connection column (enable it if hidden). A preconnect that works shows the font request without a fresh connection setup.
- In the Performance panel, record a load and look at the Layout Shifts track. Each shift lists the elements that moved.
- Run Lighthouse and read the CLS value. Run it several times, because results vary.
A practical default
For most sites using Google Fonts, this combination works well:
- Two preconnect hints, the second with
crossorigin. display=swapin the CSS URL.- Only the weights you use.
- A well-matched fallback stack.
If layout shift remains a problem after you measure it, move to self-hosting with preload for the primary font and a metric-adjusted fallback. The self-hosting trade-offs, including privacy, are covered in a later chapter.
Exercise
A page loads Inter from Google Fonts with two preconnect hints. The Network panel shows that the font files still open a new connection to fonts.gstatic.com, so the preconnect had no effect. The hint for fonts.gstatic.com is <link rel="preconnect" href="https://fonts.gstatic.com">. What is wrong, and how do you fix it?
Show answer
The hint is missing the crossorigin attribute. Browsers fetch font files in CORS mode, so a connection opened without crossorigin is not reused for them. Change the hint to <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>.
Exercise
Your body text uses font-display: swap. Testers on slow connections report that the page text shifts down by one line when the web font arrives. Name two changes that reduce the shift without removing the web font.
Show answer
Any two of these: switch to font-display: optional or fallback so the late swap is skipped or limited; define a fallback @font-face using local() with size-adjust and the ascent, descent, and line-gap overrides so its metrics match the web font; pick a system fallback with widths closer to the web font; load fewer weights so fewer swaps happen.