completefrontend
Open lab My progress
THE WEB PERFORMANCE FIELD GUIDE

How CLS works

Score a layout shift by hand from its rectangles, group shifts into session windows, and see which movement Chrome skips or never counts toward CLS.

By the end, you can
  • 01
    Score one layout shift by handWork out the impact fraction and the distance fraction from the moved elements' rectangles and the screen's size.
  • 02
    Turn many shifts into one CLSGroup shifts into session windows with the 1 second gap and the 5 second limit, and keep the largest window.
  • 03
    Tell which movement countsKnow which shifts hadRecentInput skips, and which movement never becomes a layout shift at all.

Two jumps, one number

Asha opens the Leaf and Kettle's mint tea page on her phone. She starts to read about the tea. Half a second later, the words jump down the screen. A yellow banner has appeared at the top: "Free delivery this week". She finds her place again. Then the reviews arrive, and the Add to cart button drops lower still.

Ravi builds the shop's site. The page reports its numbers to him after each visit. For Asha's visit, its CLS was 0.1112. CLS, Cumulative Layout Shift, is Chrome's score for how much a page jumps. You met it in lesson 3.1: 0.1 or less is good. So this page "needs improvement".

Ravi has three questions. How did two jumps become one number? Would a third jump add to it? And which jumps don't count?

Here is Asha's screen at three moments, as Chrome drew it:

Real frames of Asha's screen on the mint tea page. 35 ms: Asha reads: nothing has moved yet. 515 ms: the banner: 3 boxes moved 112 px down, score 0.0924. 1214 ms: the reviews: Add to cart moved 144 px, score 0.0188.35 ms: Asha readsAsha's screen at 35 ms: Asha readsnothing has moved yet515 ms: the bannerAsha's screen at 515 ms: the banner3 boxes moved 112 px downscore 0.09241214 ms: the reviewsAsha's screen at 1214 ms: the reviewsAdd to cart moved 144 pxscore 0.0188
Real frames from the course's recording in Chrome. Times count from the moment the page started loading. Each jump got its own score.

Each jump is a layout shift. Something already on the screen moved to a new place between one frame and the next. A frame is one picture the browser draws on the screen. Chrome noticed both jumps and gave each one a score:

Asha's two jumpsPop out in the code editor (opens in a new tab)JavaScript
// Chrome's two layout shifts on Asha's visit: [when, in ms; score].const shifts = [[511.1, 0.0924], [1210.3, 0.0188]]; console.log(`The banner's jump scored ${shifts[0][1]}.`);console.log(`The reviews' jump scored ${shifts[1][1]}.`);console.log(`They came ${(shifts[1][0] - shifts[0][0]).toFixed(1)} ms apart.`);console.log(`CLS: ${(shifts[0][1] + shifts[1][1]).toFixed(4)}`);
  • Line 1 is a comment. Times are in milliseconds (ms), thousandths of a second. They count from the moment the page started to load.
  • Line 2 holds Chrome's two layout shifts: when each one happened, and its score.
  • Lines 4 and 5 print The banner's jump scored 0.0924. and The reviews' jump scored 0.0188.
  • Line 6 prints They came 699.2 ms apart.
  • Line 7 adds the two scores. It prints CLS: 0.1112.

This time, CLS was the two scores added up. Each jump alone was under 0.1. Together, they went over. But CLS doesn't always add up every jump. This lesson takes the rules apart, one by one, with real recordings.

Real-life analogyRating a bumpy bus ride

Asha takes the bus to work. A jolt that throws the whole bus forward is worse than a wobble that tips one cup. Jolts come in rough stretches of road. When she rates the ride, she doesn't add up an hour of bumps. She remembers the roughest stretch.

In real life: One jolt of the bus
In JavaScript: One layout shift: things moved between two frames
In real life: How many people were thrown, and how far
In JavaScript: The shift's score: how much of the screen moved, and how far
In real life: A rough stretch of road
In JavaScript: A burst of shifts close together, called a session window
In real life: The ride's rating: its roughest stretch
In JavaScript: CLS: the burst with the largest total
The main idea

Chrome scores every frame in which something on the screen moved: how much of the screen it covered, times how far it went. It skips moves right after a click, tap or key press. It groups the rest into bursts. CLS is the largest burst's total.

The next four sections follow those rules in order:

  • How is one shift scored?
  • How do many shifts become one number?
  • Which shifts does a click excuse?
  • Which movement never counts at all?

The course recorded every page in this lesson in Chrome 157, on a screen the size of Asha's phone: 412 px wide and 823 px tall.

Scoring one layout shift

Start with the banner's jump. What did Chrome see?

Before it draws each frame, Chrome checks where every element starts. For English text, that's the element's top-left corner. If the corner moved 3 px or more since the last frame, the element has shifted. The Layout Instability spec, the standard that defines layout shifts, calls it unstable. The last section shows why smaller moves don't count.

Two changes don't move a corner:

  • A new element. The banner didn't exist before. It has no old corner, so it never shifts. Only what it pushes does. Lesson 11.3 found the same thing.
  • An element that grows. When the reviews arrived, the reviews box got 144 px taller. Its top-left corner stayed put. Only Add to cart, below it, moved.

In the banner's frame, three boxes moved, with everything inside them. The first was the tea's part of the page, #tea: the heading, photo and text. Then came the reviews box, #reviews, and Add to cart, #add. Each moved 112 px down, the banner's height. Lesson 11.3 called this list the shift's sources. Each source has two rectangles: where it was, and where it is now.

Real-life analogyTwo dents in the carpet

You drag a sofa across the living room. It leaves a dent where it stood, and makes a new dent where it stands now. How much did you disturb the room? You'd look at two things: how much carpet the two dents cover, and how far you dragged it. A big sofa nudged a little covers lots of carpet but hardly moved. A footstool dragged across the room covers little but went far.

In real life: The carpet
In JavaScript: The screen, which the spec calls the viewport
In real life: The old dent and the new dent, together
In JavaScript: Each moved element's rectangle before and after
In real life: How much carpet the dents cover
In JavaScript: The impact fraction: that area, out of the screen's area
In real life: How far you dragged it, out of the room's longest wall
In JavaScript: The distance fraction: the farthest move, out of the screen's longer side

The spec scores a frame with the same two numbers:

  • The impact fraction. Take each moved element's rectangle before the move, and after it. Join them all. Count any overlap once. Keep only the part inside the screen. Divide that area by the screen's area.
  • The distance fraction. For each moved element, take how far its corner went: across or down, whichever is larger. Take the largest of those. Divide it by the screen's longer side. On Asha's phone, that's the height, 823 px.
  • The shift's score, its value, is the first times the second.
Asha's 412 by 823 px screen, twice. The banner's shift: the moved elements covered 230,280 px², 0.679 of the screen, and moved 112 px, 0.136 of its height, for a score of 0.0924. The reviews' shift: the moved elements covered 36,480 px², 0.108 of the screen, and moved 144 px, 0.175 of its height, for a score of 0.0188.The banner's shiftcovered: 380 × 558+ 380 × 48= 230,280 px²÷ 339,076 = 0.679moved 112 px÷ 823 = 0.136score 0.0924The reviews' shiftold: 380 × 48new: 380 × 48= 36,480 px²÷ 339,076 = 0.108moved 144 px÷ 823 = 0.175score 0.0188
Shaded: where the moved elements were, or are now. Each arrow is the farthest move. The score is the shaded share of the screen times the move's share of its height.

Look at the left screen. All three moved boxes are 380 px wide, so their rectangles stack into strips. Together they cover one strip 558 px tall, from the tea's old top to the reviews box's new bottom. Below it is a gap of 16 px that no rectangle covers. Then comes a strip 48 px tall: Add to cart's new place. Here is the arithmetic:

Two shifts, scored by handPop out in the code editor (opens in a new tab)JavaScript
// Asha's screen, in CSS px.const screen = { width: 412, height: 823 }; function score(covered, farthest) {  const impact = covered / (screen.width * screen.height);  const distance = farthest / Math.max(screen.width, screen.height);  return `impact ${impact.toFixed(3)} × distance ${distance.toFixed(3)} = ${(impact * distance).toFixed(4)}`;}console.log("Banner:", score(380 * 558 + 380 * 48, 112));console.log("Reviews:", score(2 * 380 * 48, 144));
  • Line 2 is Asha's screen.
  • Lines 4 to 8 are the spec's rule as a function. Line 5 divides the covered area by the screen's area. Line 6 divides the farthest move by the longer side. Math.max picks it.
  • Line 9 is the banner's shift: the two strips, and a move of 112 px. It prints Banner: impact 0.679 × distance 0.136 = 0.0924.
  • Line 10 is the reviews' shift. Add to cart's old place and new place are 144 px apart, so they don't overlap. Both count in full. It prints Reviews: impact 0.108 × distance 0.175 = 0.0188.

Two more details from the spec:

  • One entry per frame. Everything that moved in the same frame shares one score. That's why the banner's three sources made one shift, not three.
  • The distance can't go over 1. A move longer than the screen counts as one whole screen.

Chrome's own scores match these exactly. Now compare the two jumps. The banner moved most of the screen a short way. Add to cart went further, but it's small. The big block scored almost five times as much.

Why it matters: what moves counts as much as how far. Keeping space for the banner would save far more than fixing the button.

Try it yourself. The slider moves Add to cart down, alone, from where the banner left it:

Playground: move Add to cart
What the playground works outJavaScript
// Add to cart, 380 × 48 px, moves `moved` px down, alone, on Asha's screen.const covered = moved < 48 ? 380 * (48 + moved) : 2 * 380 * 48; // old and new places, any overlap onceconst impact = covered / (412 * 823);const distance = moved / 823;const score = moved < 3 ? 0 : impact * distance; // under 3 px, Chrome writes no entry
Asha's screenscore 0.0188
covered36,480 px²
impact0.108
distance0.175
score0.0188
Try it yourself
Score 0.0188

Add to cart moved 144 px. Its old and new places don't touch, so both count in full. They cover 36,480 px², 0.108 of the screen. The move is 0.175 of 823 px.

Asha's 412 × 823 px screen, with Add to cart where the banner left it. The dashed box is its old place. 144 px is what the reviews did.

Only Chromium browsers, like Chrome and Edge, write layout-shift entries. Firefox and Safari don't, so their visitors have no CLS. Chrome added the sources' rectangles a few versions after the entries:

Session windows: the worst burst

Back to Ravi's second question: would a third jump add to Asha's CLS? It depends on when the jump comes.

Lesson 3.1 gave the rule. CLS groups shifts into bursts called session windows, and keeps the worst burst. To watch the rule work, Ravi built a test page. A box sits under an empty space. On a timetable, the space grows, and the box jumps down: 11 times in 9 seconds.

Real-life analogyRushes at the café counter

Customers come into the Leaf and Kettle in rushes. If the next one walks in less than a minute after the last, Meera counts them in the same rush. A quiet minute ends the rush. And no rush runs past five minutes: after that, she starts a new one, even if people keep coming. At closing time, she writes down the busiest rush, not the day's total.

In real life: A customer walking in
In JavaScript: A layout shift, with its score
In real life: Less than a minute since the last customer
In JavaScript: Less than 1 second since the last shift: same window
In real life: No rush runs past five minutes
In JavaScript: A window ends before 5 seconds after its first shift
In real life: The busiest rush of the day
In JavaScript: CLS: the window with the largest total

Here are Chrome's 11 shifts, grouped by those two rules:

Ravi's shifts, grouped into windowsPop out in the code editor (opens in a new tab)JavaScript
// Ravi's test page: [when, in ms; score]. The last eight jumps were the same size.const small = [3400.6, 4201.3, 5001, 5800.8, 6600.9, 7400.4, 8201.1, 9000.5];const shifts = [[502.1, 0.01307], [1200.9, 0.00599], [2600.8, 0.0094], ...small.map((time) => [time, 0.00286])]; const windows = [];for (const [time, score] of shifts) {  const open = windows.at(-1);  const joins = open && time - open.last < 1000 && time - open.first < 5000;  if (joins) Object.assign(open, { last: time, total: open.total + score });  else windows.push({ first: time, last: time, total: score });}windows.forEach((w, i) => console.log(`Window ${i + 1}: ${w.total.toFixed(4)}, from ${w.first} to ${w.last} ms`));
  • Line 1 is a comment. Each shift is a time in ms and a score.
  • Lines 2 and 3 hold Chrome's 11 shifts. The last eight were the same size, so line 2 only lists their times.
  • Line 5 starts with no windows.
  • Line 7 takes the open window: the last one. Line 8 checks the two rules. The shift must come less than 1000 ms after the window's last shift, and less than 5000 ms after its first.
  • If both hold, line 9 adds the shift to the open window. If not, line 10 starts a new window with it.
  • Line 12 prints one line per window. The first is Window 1: 0.0191, from 502.1 to 1200.9 ms.
  • The next two are Window 2: 0.0266, from 2600.8 to 7400.4 ms and Window 3: 0.0057, from 8201.1 to 9000.5 ms.
Ravi's test page: 11 layout shifts on a timeline, each bar as tall as its score. window 1: 0.0191, 2 shifts from 502.1 to 1200.9 ms; window 2: 0.0266 = CLS, 7 shifts from 2600.8 to 7400.4 ms; window 3: 0.0057, 2 shifts from 8201.1 to 9000.5 ms. A gap of more than 1 second ends window 1; window 2 ends before 5 seconds after its first shift.1.4 s gap5 s limitwindow 1: 0.0191window 2: 0.0266 = CLSwindow 3: 0.00570 s5 s9 s
Drawn to scale: each bar is one shift, as tall as its score. A gap of 1 second or more ends a window, and so does the 5 second limit. CLS is the largest window, not the sum.

Each rule split the shifts once:

  • The gap. The third shift came 1399.9 ms after the second. That's more than 1 second, so window 1 closed.
  • The limit. The shift at 8201.1 ms came only 800.7 ms after the one before. But it came 5600.3 ms after window 2's first shift. A window can't reach 5 seconds, so this shift started window 3.

CLS is the largest window: 0.0266. All 11 shifts added up would make 0.0513, almost twice as much. The course also ran the web-vitals library, version 6.2.3, on the page. Its final CLS was 0.0266 too, made of window 2's seven shifts.

Why it matters: a page that stays open for an hour isn't punished for an hour of small jumps. Only its worst burst counts.

CLS didn't always work this way. Until Chrome 91, in 2021, it added up every shift on the page. Chrome's team then changed it to the largest window, with the 1 second gap and the 5 second limit. They kept the largest window, not an average, for a reason. With an average, fixing one small shift could make the score worse.

web-vitals checks the two rules on lines 38 and 39 of its LayoutShiftManager.ts. The course watched its CLS change on Ravi's page:

When web-vitals' CLS went upPop out in the code editor (opens in a new tab)JavaScript
// web-vitals' CLS on Ravi's page, each time it went up: [when, in ms; CLS].const reports = [[502.4, 0.0131], [1201.2, 0.0191], [5801.1, 0.0208], [6601.1, 0.0237], [7400.7, 0.0266]];for (const [time, cls] of reports) console.log(`At ${time} ms, CLS became ${cls}.`);
  • Line 2 holds each report: when it came, and CLS at that moment.
  • Line 3 prints them, from At 502.4 ms, CLS became 0.0131. to At 7400.7 ms, CLS became 0.0266.

Look between 1201.2 and 5801.1 ms. Window 2 started at 2600.8 ms, but CLS didn't move. Window 2 was still smaller than window 1. CLS only changed at window 2's fifth shift. That's when it passed window 1's 0.0191.

Why it matters: a new burst only shows in CLS once it's bigger than the worst one so far.

So when does CLS stop? It doesn't, while the page is open. A jump ten minutes in can still start the worst window. So web-vitals reports CLS each time the page is hidden: when Asha switches to another tab, or closes this one. Chrome also keeps pages she just left in its back/forward cache, ready for the Back button. If the page comes back from there, CLS starts again at 0. To her, it's a new visit.

Try fixing jumps on Ravi's page. Each button is one of Chrome's shifts:

Playground: fix one jump
Session windowssum 0.0513
Window 1: 2 shifts, 502.1 to 1200.9 ms0.0191
Window 2: 7 shifts, 2600.8 to 7400.4 ms0.0266 = CLS
Window 3: 2 shifts, 8201.1 to 9000.5 ms0.0057
Try it yourself
CLS 0.0266

All 11 jumps happen. They fall into 3 windows. CLS is the largest, 0.0266. Added up, they'd be 0.0513. Press a jump to fix it.

Chrome's 11 recorded shifts on Ravi's test page, grouped the way web-vitals 6.2.3 groups them. A pressed jump is one you fixed: it never happens.

Shifts right after a click

Meera, who runs the shop, wants the reviews hidden until someone asks. So the page gets a Show reviews button. When Asha clicks it, a box opens at once with "Loading reviews". 300 ms later, the reviews fill it in. 900 ms after the click, customers' photos arrive too. Each step pushes Add to cart further down.

Real-life analogyWaving for the waiter

You wave, and the waiter comes over and moves your plates to make room for dessert. You expected that. You asked. But say the dessert only comes ten minutes later, and the waiter pushes your book aside while you read. You did ask, long ago. It still takes you by surprise.

In real life: Your wave
In JavaScript: A click, tap or key press
In real life: Plates moved right after you wave
In JavaScript: A shift less than 500 ms after the input: hadRecentInput is true
In real life: Your book pushed aside ten minutes later
In JavaScript: A shift after the 500 ms: it counts, even though your click started it

Lesson 11.3 showed that a shift right after a click carries a flag, hadRecentInput, and that CLS skips it. Here is the spec's rule in full:

  • Some events are input: pressing a mouse button or a finger down, pressing a key, and a change event, like picking from a list. Resizing the window counts too.
  • Each layout-shift entry keeps lastInputTime: the time of the last such input.
  • hadRecentInput is true if that input came less than 500 ms before the shift.

The course clicked Show reviews with a mouse. Chrome wrote three shifts:

Three shifts after one clickPop out in the code editor (opens in a new tab)JavaScript
// Asha clicked Show reviews at 1501.5 ms. Chrome's shifts after it: [when, in ms; score].const shifts = [[1503.6, 0.00994], [1805.1, 0.0115], [2404.6, 0.01255]]; for (const [time, score] of shifts) {  const after = time - 1501.5;  const recent = after < 500; // hadRecentInput  console.log(`${after.toFixed(1)} ms after the click: ${score}, ${recent ? "skipped" : "counts"}`);}
  • Line 1 is a comment. Chrome's lastInputTime on all three shifts was 1501.5 ms: the moment of the click.
  • Line 2 holds Chrome's three shifts.
  • Line 5 works out how long after the click each shift came. Line 6 applies the rule: less than 500 ms.
  • Line 7 prints 2.1 ms after the click: 0.00994, skipped, 303.6 ms after the click: 0.0115, skipped and 903.1 ms after the click: 0.01255, counts.
Show reviews, clicked at 1501.5 ms. Three layout shifts after it: +2.1 ms: skipped, score 0.00994; +303.6 ms: skipped, score 0.01150; +903.1 ms: counts, score 0.01255. The shaded band is the 500 ms after the click, when hadRecentInput is true.click at 1501.5 ms+2.1 ms: skipped+303.6 ms: skipped+903.1 ms: countsthe 500 ms after the clickhadRecentInput is true
Drawn to scale. The shaded band is the 500 ms after Asha's click. Shifts inside it have hadRecentInput set, and CLS skips them. The photos came after it, so they count.

Chrome's own flags matched: true, true, then false. The page's CLS, from web-vitals, was 0.01255: the photos' shift alone.

Why it matters: the photos' jump counts, even though Asha's click caused it. web.dev's advice fits here. If a click starts something slow, make space for the answer right away, and show that it's loading. Then nothing moves when the answer arrives.

Not every input counts. Read each row across: an input, then what the spec and Chrome's code say about it.

Which inputs turn on hadRecentInput
The inputIn the specIn Chrome's code
A mouse click, or a mouse button pressed downYes. The spec lists mousedown and pointerdown.Yes. Chrome counts the press and the release.
A tap on a touch screenYes. A tap starts with pointerdown.Yes. Chrome counts the tap.
A key pressYes. The spec lists keydown.Yes. Chrome counts it.
A change event, like picking from a listYes. The spec lists change events.Yes. Chrome counts it.
Resizing the windowYes. It changes the viewport's size.Yes. Chrome counts it.
Moving the mouseNo. mousemove and pointermove aren't on the list.No.
Scrolling or swipingNo. A scroll or flick gesture isn't on the list.No. Chrome only notes that a scroll happened.

Try it on this page. Each button moves the box 48 px. One moves it at once, and the other a second later:

Playground: a shift right after your click
The playground, shortenedJavaScript
new PerformanceObserver((list) => {  for (const shift of list.getEntries()) {    const after = shift.startTime - lastClick;    show(shift.value, shift.hadRecentInput, after);  }}).observe({ type: "layout-shift" }); moveNow.onclick = () => spacer.classList.toggle("tall");moveLater.onclick = () => setTimeout(() => spacer.classList.toggle("tall"), 1000);
The boxlive
Add to cart
shiftsnone yet
Try it yourself
Your browser

Press Move now, then Move in 1 second. Each moves the box 48 px. Watch hadRecentInput.

This runs in your browser, with the Layout Instability API. The scores depend on the size of your window. Keep the box on screen: a move nobody can see isn't a shift.

Movement that never counts

Some movement never becomes a layout shift at all. Chrome doesn't skip it later. It never writes an entry in the first place. Ravi built a second test page to find these. It has five boxes, and it moves each box once, at its own time. Here are four of the moves:

Four moves on Ravi's second pageJavaScript
// Four of the boxes on Ravi's second test page, each moved once.
box("two").style.top = "82px"; // was 80px: 2 px down
box("three").style.top = "83px"; // was 80px: 3 px down
box("slide").animate([{ transform: "translateX(0)" }, { transform: "translateX(240px)" }], { duration: 400, fill: "forwards" });
box("left").animate([{ left: "16px" }, { left: "256px" }], { duration: 400, fill: "forwards" });
  • Line 2 moves a box 2 px down. Line 3 moves another box 3 px down.
  • Line 4 slides a box 240 px to the right in 400 ms with transform, a CSS property that changes where an element is drawn.
  • Line 5 slides a box the same 240 px by changing left: its place in the layout.

The fifth box sits 1000 px down the page, below the screen. It moves 100 px. Here is what Chrome wrote for each move. Read each row across:

What Chrome wrote down for each move
The moveLayout shiftsWhy
A box moved 2 px downNo entry.Chrome ignores moves smaller than 3 px.
A box moved 3 px downOne entry, scored 0.00016.3 px is Chrome's smallest move that counts.
A box slid 240 px with transformNo entry.The spec ignores a change of transform.
A box slid 240 px with left24 entries, 0.0089 in all.left moves the box's place in the layout, every frame.
A box below the screen moved 100 pxNo entry.Only what's inside the screen counts.
The page scrolled 720 pxNo entry for the scroll.Moving with the scroll isn't moving in the page.
Real-life analogyA motion alarm that knows the wind

At night, a motion alarm watches the café. It doesn't ring when a leaf quivers in the wind, or when a shadow slides across the wall. It ignores anything outside its camera's view. When the camera itself turns, everything in the picture moves, and it still stays quiet. It rings when a chair really moves.

In real life: A leaf that quivers in the wind
In JavaScript: A move of less than 3 px
In real life: A shadow sliding across the wall
In JavaScript: A transform: the picture moves, not the thing
In real life: Something outside the camera's view
In JavaScript: A move below the screen
In real life: The camera turning to look at another table
In JavaScript: Scrolling: the whole view moves
In real life: A chair that really moves
In JavaScript: A layout shift

Here is the rule behind each quiet move:

  • Tiny moves. The spec lets each browser pick how many pixels a move needs before it counts. Chrome picked 3. Its source calls it kMovementThreshold. So 2 px wrote nothing, and 3 px wrote a tiny shift.
  • Transforms. The spec checks each corner twice: as drawn, and as if the element had no transform. A move only counts if both corners moved. A transform slide only moves the drawn corner, so it never counts. The same slide with left wrote 24 entries, one per frame, about 10 px each.
  • Off the screen. Only the part of an element inside the screen counts. Nobody could see the box 1000 px down, so its move wrote nothing.

Why it matters: if something must slide, slide it with transform. Lesson 30.3 shows how.

Scrolling moves everything, and nothing

Ben reads every review on a long page. He turns his mouse wheel six times, 120 px each. Everything on his screen moves up 720 px. Chrome wrote no entry for that. The spec only counts an element that moved both on the screen and inside the page. Scrolling moves things on the screen, but nothing inside the page. It's like the view from a train window: the trees slide past, but you moved, not them.

But scrolling doesn't excuse a real jump. Ben's page opens a block of photos once he has scrolled 240 px. It opened in the middle of his screen, during his second turn of the wheel. The reviews below it dropped 172 px. Chrome wrote one entry, scored 0.0775, with hadRecentInput false. Its lastInputTime was 0. The wheel and the mouse never counted as input.

That entry also shows the off-screen rule at work. One review was pushed right off the bottom of the screen. In the entry, its new rectangle was empty: 0 by 0. Only the part of it that had been on the screen counted.

Why it matters: content that loads while the visitor scrolls, like more items in a feed, counts in full. Lesson 30.4 looks at finding those shifts.

Pick a move to see every entry Chrome wrote for it:

Playground: what Chrome wrote down
Four of the movesJavaScript
// Four of the boxes on Ravi's second test page, each moved once.box("two").style.top = "82px"; // was 80px: 2 px downbox("three").style.top = "83px"; // was 80px: 3 px downbox("slide").animate([{ transform: "translateX(0)" }, { transform: "translateX(240px)" }], { duration: 400, fill: "forwards" });box("left").animate([{ left: "16px" }, { left: "256px" }], { duration: 400, fill: "forwards" });
2 pxrecorded
entries0
score in all0
hadRecentInputno entry
Try it yourself
No layout shift

The box moved 2 px down at 601.3 ms. Chrome wrote no entry: 2 px is under its 3 px.

Recorded in Chrome 157 on Ravi's second test page and Ben's page of reviews. Each choice shows every layout-shift entry Chrome wrote for that movement.

Does it count toward CLS?

Each change below happens on a shop's page. Sort it: does it count toward CLS, is it skipped as recent input, or is it never a layout shift?

Does it count toward CLS?
  • A banner pushes the text down while Asha reads
  • Photos arrive 900 ms after Asha clicks Show reviews
  • A photo block opens in the middle of the screen while Ben scrolls
  • A menu opens 50 ms after a tap and pushes the page down
  • Typing in a search box filters the list below it at once
  • A carousel slides its pictures with transform: translateX()
  • A price moves 2 px when its text changes
  • A footer far below the screen moves down
  • The whole page moves up as Ben scrolls
Try it yourself
0 of 9 correct

Sort each change by what Chrome does with it.

Choose a category for every card. You can change an answer at any time; Reset clears them all.

Common mix-ups

  • "CLS adds up every shift on the page." No. It keeps the largest session window. On Ravi's page, that was 0.0266 of the 0.0513 in all.
  • "A shift's score is how far things moved." No. It's how much of the screen the moved things covered, old and new places together, times how far.
  • "An element that grows has shifted." No. Its top-left corner stays put. Only what it pushes shifts.
  • "Anything a click causes is skipped." No. Only shifts less than 500 ms after it. Asha's photos came 903.1 ms after her click, and they counted.
  • "Scrolling excuses a shift, like a click does." No. Scrolling isn't input for this rule. A shift during a scroll counts.
  • "Any animation hurts CLS." No. A transform slide wrote no entry. The same slide with left wrote 24.

Practice exercises

Answer first, then use the hints.

Exercise 1 · Warm-upOnly the photo moves

Say only the mint tea photo moved, 20 px down. What does the line print?

Starter codePop out in the code editor (opens in a new tab)JavaScript
// Asha's screen, in CSS px.
const screen = { width: 412, height: 823 };

function score(covered, farthest) {
  const impact = covered / (screen.width * screen.height);
  const distance = farthest / Math.max(screen.width, screen.height);
  return `impact ${impact.toFixed(3)} × distance ${distance.toFixed(3)} = ${(impact * distance).toFixed(4)}`;
}
console.log("Photo:", score(380 * (214 + 20), 20));

Answer, then press Check. Spacing and letter case don’t matter.

    Exercise 2 · Warm-upAn earlier third jump

    The third jump on Ravi's page now comes at 2100 ms. What does the first line print?

    Starter codePop out in the code editor (opens in a new tab)JavaScript
    // Ravi's test page: [when, in ms; score]. The last eight jumps were the same size.
    const small = [3400.6, 4201.3, 5001, 5800.8, 6600.9, 7400.4, 8201.1, 9000.5];
    const shifts = [[502.1, 0.01307], [1200.9, 0.00599], [2100, 0.0094], ...small.map((time) => [time, 0.00286])];
    
    const windows = [];
    for (const [time, score] of shifts) {
      const open = windows.at(-1);
      const joins = open && time - open.last < 1000 && time - open.first < 5000;
      if (joins) Object.assign(open, { last: time, total: open.total + score });
      else windows.push({ first: time, last: time, total: score });
    }
    windows.forEach((w, i) => console.log(`Window ${i + 1}: ${w.total.toFixed(4)}, from ${w.first} to ${w.last} ms`));

    Answer, then press Check. Spacing and letter case don’t matter.

      Exercise 3 · PracticeFaster photos

      The photos now arrive 400 ms after Asha's click. What does the third line print?

      Starter codePop out in the code editor (opens in a new tab)JavaScript
      // Asha clicked Show reviews at 1501.5 ms. Chrome's shifts after it: [when, in ms; score].
      const shifts = [[1503.6, 0.00994], [1805.1, 0.0115], [1901.5, 0.01255]];
      
      for (const [time, score] of shifts) {
        const after = time - 1501.5;
        const recent = after < 500; // hadRecentInput
        console.log(`${after.toFixed(1)} ms after the click: ${score}, ${recent ? "skipped" : "counts"}`);
      }

      Answer, then press Check. Spacing and letter case don’t matter.

        Exercise 4 · PracticeFix the start of window 2

        In the fix-one-jump playground, fix the jump at 2600.8 ms. What is CLS now, to four decimal places?

        Answer, then press Check. Spacing and letter case don’t matter.

          Exercise 5 · ChallengeThe banner on a bigger screen

          Load the course's copy of Asha's page on a screen bigger than her phone, and record it in DevTools. Is the banner's shift score higher or lower than 0.0924?

          Answer, then press Check. Spacing and letter case don’t matter.

            Exercise 6 · ChallengeWhat web-vitals waits for

            web-vitals' onCLS won't report CLS until another metric has been reported. Which metric?

            Answer, then press Check. Spacing and letter case don’t matter.

              Check your understanding

              For each question, ask which rule it's about: the score, the window, recent input, or movement that never counts.

              The how CLS works quiz · 7 questionsScore: first tries count
              1. Question 1 of 7What does the Reviews line of the score example print?

                Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
                // Asha's screen, in CSS px.
                const screen = { width: 412, height: 823 };
                
                function score(covered, farthest) {
                  const impact = covered / (screen.width * screen.height);
                  const distance = farthest / Math.max(screen.width, screen.height);
                  return `impact ${impact.toFixed(3)} × distance ${distance.toFixed(3)} = ${(impact * distance).toFixed(4)}`;
                }
                console.log("Banner:", score(380 * 558 + 380 * 48, 112));
                console.log("Reviews:", score(2 * 380 * 48, 144));

                Choose an answer to see the explanation.

              2. Question 2 of 7Asha's two shifts came less than a second apart. Why is her CLS the two scores added up?

                Choose an answer to see the explanation.

              3. Question 3 of 7On Ravi's recorded test page, one shift came less than a second after the one before it, yet it started a new window. Why?

                Choose an answer to see the explanation.

              4. Question 4 of 7What does the third line of the click example print?

                Read the code, then predictPop out in the code editor (opens in a new tab)JavaScript
                // Asha clicked Show reviews at 1501.5 ms. Chrome's shifts after it: [when, in ms; score].
                const shifts = [[1503.6, 0.00994], [1805.1, 0.0115], [2404.6, 0.01255]];
                
                for (const [time, score] of shifts) {
                  const after = time - 1501.5;
                  const recent = after < 500; // hadRecentInput
                  console.log(`${after.toFixed(1)} ms after the click: ${score}, ${recent ? "skipped" : "counts"}`);
                }

                Choose an answer to see the explanation.

              5. Question 5 of 7The banner on Asha's page was new. Is it one of its shift's sources?

                Choose an answer to see the explanation.

              6. Question 6 of 7On the recorded test page, which of these movements made no layout-shift entry?

                Choose an answer to see the explanation.

              7. Question 7 of 7Ben scrolled with his mouse wheel, and photos opened in the middle of his screen. Did that shift count toward CLS?

                Choose an answer to see the explanation.

              Key takeaways

              • A layout shift is a frame in which a visible element's top-left corner moved 3 px or more.
              • Its score is the impact fraction times the distance fraction: old and new places together, out of the screen, times the farthest move, out of the longer side.
              • Shifts less than 500 ms after a click, tap or key press have hadRecentInput, and CLS skips them. Scrolling isn't input.
              • The rest fall into session windows: less than 1 second apart, less than 5 seconds long. CLS is the largest window's total.
              • New elements, growing elements, transforms, tiny moves, moves below the screen and scrolling never make a shift of their own.

              Remember the one-liner.
              CLS is the worst burst of unexpected jumps, and each jump is scored by how much moved times how far.

              Sources

              The facts on this page come from these specs, docs and source files:

              Coming next: What causes layout shifts. Images without sizes, ads, late content, fonts and sliding with left: where jumps like Asha's come from.

              CompleteFrontend Clear concepts. Working examples.