Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
2 min readPerformance

Reduce layout shift when fonts swap

Match a local fallback to your web font to reduce layout shift during font swaps.

With font-display: swap, the browser can paint text in a fallback font and redraw it when the web font arrives. If the fonts wrap text at different points or use different line heights, content below the paragraph can jump.

Load both faces

Start with an untuned fallback. Pick a local font you expect readers to have and check it on the operating systems you support.

@font-face {
  font-family: 'Web';
  src: url('/fonts/web.woff2');
  font-display: swap;
}

@font-face {
  font-family: 'Fallback';
  src: local('Arial');
}

body {
  font-family: 'Web', 'Fallback', sans-serif;
}

Point the URL at your web font. The browser uses the local face while the web font loads, or the generic font if it cannot use either face.

Measure a paragraph

Open the page with a representative paragraph and run this in the console. Both named faces need to load for the measurement.

const sample = document.querySelector('p').textContent.trim()
if (!sample) throw new Error('Choose a paragraph with text')
const px = 1000
const ctx = document.createElement('canvas').getContext('2d')

async function measure(family) {
  const font = `${px}px "${family}"`
  const faces = await document.fonts.load(font, sample)
  if (!faces.length) throw new Error(`No face loaded for ${family}`)
  ctx.font = font
  return ctx.measureText(sample)
}

const web = await measure('Web')
const fallback = await measure('Fallback')
const scale = web.width / fallback.width
const percent = (metric) => `${(100 * metric / (px * scale)).toFixed(2)}%`

console.log(`size-adjust: ${(100 * scale).toFixed(2)}%;
ascent-override: ${percent(web.fontBoundingBoxAscent)};
descent-override: ${percent(web.fontBoundingBoxDescent)};`)

Paste the output inside the Fallback rule. size-adjust scales the fallback to match the sample’s measured width. The CSS Fonts spec applies the metric percentages to the adjusted font size, so the calculation divides by that scale.

The canvas metrics give you starting values for ascent and descent. If the web font has a line gap, you can also set line-gap-override from its metrics using the same calculation. A percentage that matches the total width of one paragraph can still leave individual words at different widths.

November 2026 update. Safari supports size-adjust starting with version 17. MDN’s compatibility data lists the metric overrides in Safari Technology Preview. Check width and line height on the Safari release your readers use.

Check the swap

In Chrome DevTools, disable the cache and throttle the network. Record a reload in the Performance panel before and after adding the descriptors. Compare the layout shifts around the font request, then repeat at a few viewport widths. A heading or paragraph can still wrap differently at a width you didn’t measure.