Sub topic 9
font-display, preconnect, and avoiding layout shifts
This chapter assumes you can add a link tag, write a font-family rule, and open the Network panel in your browser's developer tools. It covers what the browser does while a web font downloads, and how to keep that wait from hurting the page.
What happens while a font loads
A browser finds out which web fonts a page needs only after it has downloaded the HTML and the CSS and matched rules to elements. Only then does it request the font file. Until the file arrives, the browser must decide what to do with the text.
Browsers use up to three phases, controlled by the font-display descriptor:
- Block period: the text is invisible. Browsers reserve space for it, but the reader sees nothing.
- Swap period: a fallback font is shown. If the web font arrives, the browser swaps it in.
- Failure period: the browser gives up and keeps the fallback.
The default value, auto, usually behaves like block. That can mean up to about three seconds of invisible text on a slow connection. This is often called a flash of invisible text (FOIT). The opposite, showing a fallback and then swapping, is a flash of unstyled text (FOUT).
Choosing a font-display value
| Value | Behavior | Typical use |
|---|---|---|
| `swap` | Almost no block period, swaps whenever the font arrives | Body text and headings |
| `fallback` | Very short block period, short swap window, then keeps the fallback | Body text when late swaps are distracting |
| `optional` | Very short block period, no swap if the font is not ready | Fonts that are nice to have |
| `block` | Short block period, then swaps | Icon fonts, where a fallback would show wrong characters |
| `auto` | Browser decides, usually like `block` | Rarely a deliberate choice |
For most text, swap is the right default. Readers can start reading at once.
Setting it on each service
Google Fonts takes the value from the display query parameter. The snippet below requests Inter at weights 400 and 700 with swap.
<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">Google then writes font-display: swap into the @font-face rules it returns. If you leave display out, the default is auto. For Adobe Fonts, the project stylesheet sets its own loading behavior, and you can't change it from your HTML.
If you self-host, 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;
}What preconnect does
Google Fonts uses two hosts. fonts.googleapis.com serves the CSS. fonts.gstatic.com serves the font files. Each new host costs a DNS lookup, a TCP connection, and a TLS handshake before any data moves.
rel="preconnect" tells the browser to do that setup early, while it is still parsing the page. The crossorigin attribute on the fonts.gstatic.com hint is required. Font files are fetched in CORS mode, so a connection opened without crossorigin is not reused for them, and the hint is wasted.
The same idea applies to Adobe Fonts. Add a preconnect to https://use.typekit.net before the project stylesheet link. Adobe serves its font files from https://use.typekit.net as well, so one hint is enough, with crossorigin added.
<link rel="preconnect" href="https://use.typekit.net" crossorigin>
<link rel="stylesheet" href="https://use.typekit.net/your-project-id.css">Place preconnect hints as early as possible in the head, before the stylesheet links they support.
Layout shift from font swapping
With swap, the fallback and the web font usually have different glyph widths and line heights. When the swap happens, lines can wrap differently and content below moves. Search engines and browsers measure this as Cumulative Layout Shift (CLS).
You can reduce the shift in three ways:
- Load fewer fonts. Every extra weight is another swap. Request only what the page uses.
- Choose a close fallback. A fallback with similar proportions moves less. Test by loading the page with the web font blocked.
- Adjust the fallback's metrics. The
size-adjust,ascent-override, anddescent-overridedescriptors let you define a local fallback that matches the web font's size.
@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. Compute values for your own font pair with a tool such as Fontaine or Capsize, or by measuring in the browser.
Checking your work
Open the Network panel, filter by Font, and throttle to a slow connection. Look at when each font request starts and how long it takes. Then reload and watch for text that is invisible, or that jumps when the font arrives. In the Performance panel, layout shift events are marked in the Experience track.
Exercise
A page loads Inter from Google Fonts with preconnect hints, but the hint for https://fonts.gstatic.com has no crossorigin attribute. The font still loads. Why is the hint still a problem?
Show answer
Font files are requested in CORS mode. A connection opened without crossorigin does not match that request, so the browser opens a second connection when the font is fetched. The early setup is wasted and the font loads no faster.
Exercise
You use font-display: optional for a body font and test on a slow connection. The page shows only the fallback font on the first visit. Is this a bug?
Show answer
No. With optional, the browser gives the font a very short window. If the file is not ready, it keeps the fallback for that page view and does not swap, which avoids layout shift. The font is cached, so later visits usually show the web font.