CLS: reserve the space

React chapter 144 · Web Perf · Core Web Vitals · layout shift, skeletons, aspect-ratio · source: (Core Web Vitals) web.dev/articles/cls

The problem

A promo banner arrives a second after the article and is inserted above the buttons with no space reserved. Everything below jumps down just as the user aims for “Confirm”. That jump is a layout shift; CLS adds them up.

The pattern that fixes it

Reserve the final size before the content arrives: width/height on images (the browser derives aspect-ratio), a skeleton with the same box, or min-height on the slot. Content then fills a hole instead of pushing its neighbours.

In plain words

Things on the page should not move by themselves. If something is going to appear later, leave an empty box of the right size for it now.

Reserving a seat at the cinema with your jacket. When your friend arrives, nobody in the row has to shuffle along.

Terms

layout shift
A visible element changes position between two frames without the user causing it.
CLS
Cumulative Layout Shift. Good ≤ 0.1, poor > 0.25. Unitless.
shift score
impact fraction × distance fraction: how much of the viewport moved × how far it moved.

Going deeper

How the score is computed

Impact fraction: the union of the element’s old and new boxes, as a share of the viewport. Distance fraction: how far it moved, divided by the viewport’s larger side. A block covering half the screen moving 10% of the height scores about 0.5 × 0.1 = 0.05. Since 2021, CLS is the worst “session window” — shifts less than 1 s apart, capped at 5 s — not the sum over the page’s whole life.

Shifts the user asked for do not count

A shift within 500 ms of a click, tap or key press has hadRecentInput: true and is excluded. Opening an accordion is fine. A banner that lands 1.2 s after load is not. That is why this lab loads the banner on a timer, not in the click handler.

Usual suspects in React apps

Images without dimensions; ads and embeds; a cookie bar inserted at the top; a Suspense fallback smaller than the content that replaces it (chapter 147 — make the skeleton the real size); web fonts swapping metrics (use size-adjust or font-display: optional); client-only content that SSR rendered empty. Animate with transform — it does not move layout, so it never counts.

Keep learning

Overview

linear-gradient(120deg, #7a4bb5, #3b6fd8) Spring sale · 20% off skeleton · same box Delivery Thursday. Free returns for 30 days. Card ending 4242 will be charged $249. Cancel order Confirm purchase Real life: hero images without dimensions, ads, cookie bars, “you may also like” rails. Reload the article Your order <p>Delivery Thursday…</p> <button>Confirm purchase</button> The buttons jumped while you were aiming. Tiny score — the card is probably off-screen. Scroll it into view and reload. Hover over “Confirm purchase” and wait… The banner had no box before it loaded. When it arrived, the paragraph and both buttons were pushed down. A user mid-tap hits “Cancel” or worse — and CLS records it. Put your pointer on Confirm purchase and press Reload. Watch it slide out from under you. Make the window shorter: the same pixels are a bigger share of the viewport, so the score grows. frame 1 · viewport text + buttons frame 2 · banner lands pushed down 54px impact region old ∪ new box reserve the 44px up front → nothing moves → score 0 Same article. Give the banner its box before it exists. What a layout shift is, and how it is scored Frame 1: the text and buttons are painted. The user starts reaching for “Confirm”. Frame 2: the banner is inserted above them. They move down without anyone asking. That is a layout shift. If the banner’s box was already there, frame 2 changes only pixels inside it. Nothing moves, score 0. How the slot is reserved Nothing (the bug) Skeleton, same size width + height → aspect-ratio , hot: true, note: <PromoBanner /> The banner filled a hole. Nothing below it moved. Still shifting — this mode reserves nothing. Waiting for the banner… Reserve the final size, not “some” size. A 40px skeleton for a 160px banner still shifts by 120px. Flip between Nothing and aspect-ratio with your pointer on Confirm. In Chrome DevTools → Performance, tick Layout shifts in the Rendering drawer to see shifted regions flash.

More courses

  • Jobs, linked to lessons — Browse jobs where every requirement links to the chapter that teaches it. See how ready you are, how long the gap takes to close, and which listings look stale.
  • Match a job post to courses — Paste a job post. Each skill is matched to a track here and put in learning order, foundations first.
  • Databases in depth — How a query becomes work. Indexes, isolation, locks, and the plans that explain why a page is slow — not an ORM tour.
  • System Design — Core concepts first (SQL vs NoSQL, CAP, indexing, replication, sharding), then designs grouped by what carries them: the database, Redis, Kafka, or many services.
  • NestJS: build a real API — One ticket-booking API, built in order: a plain Node server first, then NestJS modules, validation, a real database, and the features real clients lean on.
  • Microservices — Microservice patterns one diagram at a time: how to split a monolith, how services talk, and how data stays consistent without distributed transactions.
  • Kafka — A beginner-to-senior path through event logs, reliable delivery, production operations, and disaster recovery.
  • AI engineering for software engineers — Eight free reads, in order, for software engineers moving into AI: when to build an agent, how to build one, how to test it, and how the model works underneath.
  • Frontend system design — How a UI is sliced when it is a product, not a page: ownership of state, cache, routes, and what happens when the network lies.
  • TypeScript in depth — The type system as a design tool. Inference, narrowing, generics, and the errors that mean the model is wrong — not a catalog of utility types.
  • CSS in depth — See the box, then the page. Visual chapters from the box model through Flexbox surprises, Grid, selectors, and units, then backgrounds, shapes, and visual effects, each with a playground.
  • Accessibility — Foundations first (WCAG in plain words, semantic HTML, lists, alt text), then hands-on HTML labs, a full pass of WAI-ARIA 1.3, and a final fix-the-page challenge.
  • Docker — What an image is, why layers cache, and how containers find each other on a network.
  • Redis — Fifteen Redis patterns, one picture each: cache, session, lock, counter, rate limiter, queue, and when Redis is the wrong tool.
  • Kubernetes — Kubernetes from zero to operators: why it exists, Pods, Deployments and Services, then configuration, networking, storage, scheduling, scaling and security.
  • AWS — AWS networking in plain English. A VPC is a gated housing society: streets are subnets, the Internet Gateway is the main gate, and a route table is the signboard.
  • Networking: the NetRoute game — NetRoute is a packet logistics game. You wire up computers, switches, routers, firewalls and gateways, set each device, and watch glowing packets travel. If a packet gets dropped, the game shows you which header field was wrong.