Skip to content

Stopping layout shift from web fonts: size-adjust and metric-matched fallbacks

How to stop text jumping when a web font loads: compute size-adjust and the vertical overrides, and avoid three mistakes that leave a shift behind.

· 7 min read

  • Performance
  • Core Web Vitals
  • CSS

A web font with font-display: swap shows text straight away in a system font, then swaps in the real one when it arrives. The swap moves everything on the page, because the two fonts take up different amounts of space. Google counts that movement towards Cumulative Layout Shift (CLS), and on a page that is mostly text it is often the main cause of a poor score.

Four @font-face descriptors fix this. They declare a fallback face that is really the system font in disguise, scaled and padded to take up the same space as the web font. This article shows how to compute the values, what they do and do not fix, and three mistakes we found while checking our own site.

Every measurement below comes from a test page in headless Chromium 153, driven by Playwright at a 900 by 2000 pixel viewport. The font file was served 1.5 seconds late so the swap always happened after first paint, and a PerformanceObserver for layout-shift entries recorded the result. Arial is not installed on the Linux machine we used, so Liberation Sans stood in for it: same vertical metrics, same average character width, checked in the browser first.

What the browser measures

Two dimensions of a line of text matter for layout: how wide the characters are, which decides where lines wrap, and how tall the line box is, which the font’s ascent, descent and line gap decide when line-height is normal.

For our body face, Mona Sans, against Arial:

Metric (per em) Mona Sans Arial
Ascent 1.090 0.905
Descent 0.320 0.212
Line gap 0 0.033
normal line height 1.410 1.150
Weighted average character width 0.464 0.446

Arial is 4% narrower and its natural line is 18% shorter. On a 360 px column with fifteen short paragraphs, the swap turned 102 lines into 108, moved the footer by 163 px, and scored a CLS of 0.022. A heading that reflows from one line to two makes it worse.

The four descriptors

size-adjust scales the fallback’s glyphs. ascent-override, descent-override and line-gap-override replace its vertical metrics. Each is a percentage of the em. The overrides are percentages of the size-adjusted em, so compute size-adjust first.

@font-face {
  font-family: "Mona Sans Fallback";
  src: local("Arial"), local("ArialMT");
  size-adjust: 104.08%;
  ascent-override: 104.72%;
  descent-override: 30.74%;
  line-gap-override: 0%;
}

body {
  font-family: "Mona Sans", "Mona Sans Fallback", sans-serif;
}

The arithmetic is short. The one input that needs care is the average width. The font file’s own average, xAvgCharWidth in the OS/2 table, is taken over every glyph and is no use here: Mona Sans’s file reports 0.518 em. Capsize’s @capsizecss/metrics package publishes a frequency-weighted average of Latin character widths instead, and we confirmed the Mona Sans figure in Chromium with measureText.

const mona = { ascent: 1090, descent: -320, lineGap: 0, upm: 1000, xWidthAvg: 464 };
const arial = { ascent: 1854, descent: -434, lineGap: 67, upm: 2048, xWidthAvg: 913 };

const sizeAdjust = (mona.xWidthAvg / mona.upm) / (arial.xWidthAvg / arial.upm); // 1.0408
const em = mona.upm * sizeAdjust;
const pct = (n) => `${(n * 100).toFixed(2)}%`;

console.log({
  "size-adjust": pct(sizeAdjust),                    // 104.08%
  "ascent-override": pct(mona.ascent / em),          // 104.72%
  "descent-override": pct(-mona.descent / em),       // 30.74%
  "line-gap-override": pct(mona.lineGap / em),       // 0.00%
});

Or let Capsize do it, which also writes the local() sources with both the full and PostScript names:

import { createFontStack } from "@capsizecss/core";
import monaSans from "@capsizecss/metrics/monaSans";
import arial from "@capsizecss/metrics/arial";

const { fontFamily, fontFaces } = createFontStack([monaSans, arial]);

With those values on the same 360 px test page, the fallback and the web font both laid out at 108 lines, the footer did not move, and the CLS was 0.0001.

Which descriptors you actually need

This depends on your line-height, and Safari makes it matter.

With an explicit unitless line-height, say 1.6, the line box is 1.6 times the font size whatever font is in it, and the vertical overrides make almost no difference. On a 680 px column with line-height: 1.6, the unadjusted fallback scored 0.0006, size-adjust alone 0.0001, and the full set 0. Width decides the wrapping, so width is what matters.

With line-height: normal, the line box comes from the font’s own metrics, and the overrides do the work. On the same column the unadjusted fallback moved the footer 90 px (CLS 0.009). size-adjust alone still moved it 70 px (0.007). Only the full set held it still.

At the time of writing, MDN’s compatibility data lists ascent-override, descent-override and line-gap-override as available only in Safari Technology Preview, not in a shipping Safari. size-adjust has shipped everywhere since Safari 17. So the practical rule is: set an explicit line-height on text, get size-adjust right, and treat the three overrides as a bonus for Chromium and Firefox.

The similarly named font-size-adjust property is a different tool: it scales the font in use to a target x-height, and does not match widths or line boxes.

Mistake one: metrics from the wrong weight

Tools that read metrics straight from a font file measure the instance the file opens at. The variable Mona Sans file we ship from Fontsource opens at weight 200. The computed width was 455 per em instead of the 464 of the regular weight our body text uses, and the generated size-adjust was 102.06% instead of 104.08%.

Two per cent does not sound like much. On the 360 px column, the fallback with 102.06% still went from 102 to 108 lines, scoring 0.019, barely better than no adjustment. At other widths the error happened not to cross a wrap boundary, which is why it is easy to ship without noticing.

Astro’s fonts API, which we use, generates the fallback face from the first font file it collects. We found the 102.06% figure in our own built HTML while writing this. Make sure the metrics come from the weight your text uses: a static regular file, or Capsize’s published metrics. Those default to the regular weight and also come per weight, such as @capsizecss/metrics/monaSans/700.

Mistake two: one fallback face for every weight

It is tempting to declare the fallback with font-weight: 100 900 to mirror a variable web font. That tells the browser the face genuinely covers bold, so it will not synthesise it. On our test page, a heading at weight 700 rendered in regular Arial glyphs, visibly not bold and 4.8% narrower than Mona Sans Bold. The shift comes back on every heading.

Our site has this too: Astro copies the variable font’s 100 900 range onto the generated fallback face, so every heading falls back to unbolded Arial at the wrong width. The fix is a hand-written bold face from the real bold system font, with its own size-adjust. Arial Bold is 7.7% wider than Arial, while Mona Sans Bold is only 5.2% wider than Mona Sans, so the number is different:

@font-face {
  font-family: "Mona Sans Fallback";
  src: local("Arial Bold"), local("Arial-BoldMT");
  font-weight: 700;
  size-adjust: 101.67%;
}

That value comes from Capsize’s weight 700 metrics for both fonts. With the pair in place, a 17 px paragraph on one line measured 2,508 px in the fallback and 2,507 px in Mona Sans Bold. With the single face it measured 2,387 px.

Mistake three: assuming the fallback exists

local() fails quietly. On Android there is no Arial, so a face written for Arial is skipped and the text lays out in Roboto with no adjustment. Each platform font you expect to land on needs its own face and numbers; for Roboto, Mona Sans needs a size-adjust of about 104.3%.

Do not put those faces under one family name. In Chromium, when two @font-face rules share a family and both local() fonts exist, the last rule wins. A Windows machine with both Arial and Segoe UI would get the wrong numbers. Give each one its own family, as Capsize does, so the faces above become "Mona Sans Fallback: Arial" with a "Mona Sans Fallback: Roboto" beside them, and list them in font-family in order. A family whose only source is missing is skipped and the next is tried.

The other option: never swap

font-display: optional avoids the problem by never swapping. Chromium gives the font a short block period at first render; if it is available by then it is used, otherwise the fallback stays for the life of the page. In our tests that only worked with <link rel="preload" as="font">: without it the page rendered in the fallback every time, even from localhost. With it, the web font won when the response took a few tens of milliseconds and lost when it took longer. Layout shift was zero either way. That suits a decorative font. For a brand face the client expects to see on the first visit, metric-matched fallbacks keep the font and lose the shift.

Checking your own site

  1. Read the fallback @font-face in the built HTML. Check size-adjust against the weight your body text uses, and that there is a bold face.
  2. Throttle the font request in DevTools, or serve it late, and watch the layout-shift entries from a PerformanceObserver.
  3. Test a heading and a narrow column, not just body text on a desktop.
  4. Keep an explicit line-height, so Safari’s missing overrides cost you nothing.

Fonts are one of the usual causes of a poor CLS score, as blocking JavaScript is for INP, which we covered in improving INP with scheduler.yield(). If your site’s Core Web Vitals are in the red, we can find where the movement comes from and remove it. See our web development service.

Have something you'd like built?

Tell us what you're building. We'll reply within two working days with questions, an approach and next steps.

Start a project
Esc

↑ ↓ to move · Enter to open