Skip to content

Lesson 13 of 14 · 20m

CLS: Why the Page Moves and How to Pin It

Impact × distance per unexpected shift — the five real causes, reserved space, font metric matching, and finding the moving element with attribution.

CLS is the only Core Web Vital that punishes you for things that happen after the page looks done. A user reaching for a button and having it yanked out from under their thumb by a late-loading ad or a font swap is a specific, measurable betrayal — and Chrome counts every pixel of it. The score is impact fraction × distance fraction summed over unexpected shifts, bucketed into session windows (shifts separated by less than 1 s, capped at 5 s), and the worst window is your number.

The wrong mental model is "CLS is about jank." Jank is INP's territory — motion that's slow. CLS is about movement the user didn't ask for. A smooth, well-animated accordion expanding because you clicked it doesn't count: any shift within 500 ms of user input is excluded by design. What counts is everything else moving on its own.

The five real causes

  1. Media without dimensions. An <img> with no width/height starts life at 0×0 and snaps to size when the first bytes decode. Still the #1 offender after all these years.
  2. Injected content above the fold. Cookie banners, promo bars, "your cart was restored" toasts, email-capture popups — anything that inserts itself above existing content instead of displacing reserved space.
  3. Ads and embeds. An ad slot that collapses when the fill fails, or grows when it succeeds, shifts everything below it. Third-party <iframe>s whose height is decided by the embed's own JavaScript are worse.
  4. Web font swap. The fallback renders, then the real font arrives with different metrics — different line-height, different wrapping, reflow. FOIT (invisible text) doesn't shift layout; the FOUT/swap that follows it does.
  5. Dynamically resized elements. A carousel that recalculates its height after measuring its tallest slide; a lazy component that mounts taller than its placeholder.

Fixes, in order of leverage

Reserve the space before the bytes arrive.

<img src="hero-1200.avif" width="1200" height="900" alt="...">
<!-- or, when aspect ratio is the only thing you know: -->
<div style="aspect-ratio: 4/3; background: #eee"></div>

width/height on media and aspect-ratio (or explicit min-height) on dynamic slots convert "unknown size" into "reserved box." For ad slots: min-height the container, and when the fill fails, leave the empty space rather than collapsing — the layout stays put.

Fix the font swap. font-display: optional kills font-swap CLS entirely by refusing a late swap; you pay with some users seeing the fallback. The surgical option is metric matching — size-adjust, ascent-override, descent-override, line-gap-override on a local fallback @font-face tuned so Arial and Inter occupy the same line box (tools like Fontaine / @capsizecss compute the numbers). The font lesson in the rendering path shows the code.

Never insert above the fold post-render. Banners and toasts belong in reserved space, position: fixed/sticky overlays, or below the content. The A/B test that rewrites the hero after hydration is a CLS machine by construction.

Help the browser help you. content-visibility: auto on below-the-fold sections skips their layout work entirely, but always pair it with contain-intrinsic-size — otherwise the skipped sections are 0-height and your scrollbar lies.

Find the offender in the field

The web-vitals attribution build hands you the element, not just the score:

import { onCLS } from "web-vitals/attribution";
onCLS((m) => {
  const s = m.attribution.largestShiftTarget; // selector of the element that moved most
  const t = m.attribution.largestShiftTime;   // when it happened — correlates with what loaded
  sendToRum({ cls: m.value, target: s, at: t, url: routeTemplate });
});

largestShiftTarget turns "CLS is 0.31 on the PDP" into "the reviews widget mounts 3.1 s in and pushes the buy button down 180 px" — which is a fix, not a mystery.

Field notes

  • A hero that fades in is not a CLS problem (opacity doesn't move layout); a hero that swaps aspect ratios when the real image loads is.
  • transform animations are layout-free and CLS-free; animating height/top/margin is not.
  • Check CLS by session window, not per-load: a page can have zero shifts at load and still fail from what happens at second 8.
  • 500 ms is the grace window after input. An autocomplete that repaints results 600 ms after the last keystroke counts as unexpected shift — batch your updates tighter or hold the geometry stable.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.