Skip to content
Tutorials Center
My learning

Topics

Lesson 8 of 37 ยท Metrics and Core Web Vitals

Cumulative Layout Shift (CLS)

Learn how Cumulative Layout Shift is calculated, what causes content to jump, and how to prevent shifts with image dimensions, aspect ratios, reserved space and careful loading.

You are about to tap a link, an ad loads above it, the text jumps down and you tap the wrong thing. That is a layout shift. Cumulative Layout Shift (CLS) adds up the shifts that happen unexpectedly. A good CLS is 0.1 or less at p75.

CLS is the one vital you can often fix with a few lines of HTML and CSS, because most shifts come from missing size information.

How a shift is scored

Each shift gets a score: impact fraction ร— distance fraction.

  • Impact fraction: the share of the viewport covered by elements that moved (before and after positions combined).
  • Distance fraction: how far they moved, as a share of the largest viewport dimension.

If an element filling half the screen moves down by a quarter of the screen height, the score is roughly 0.5 ร— 0.25 = 0.125 (for the combined area, slightly more). Moves that are a direct result of a click, tap or key press within 500 ms are ignored. CLS reports the largest burst of shifts in a session window (shifts less than 1 second apart, capped at 5 seconds), not the sum of the whole visit.

Common causes

Cause Why it shifts
Images without width/height Browser reserves no space until the image arrives
Ads, embeds and iframes Inserted late, size unknown
Web fonts swapping Fallback and final fonts have different metrics
Banners injected above content Content below is pushed down
Late-inserted DOM from JavaScript Content appears where there was none
Animations using top, left, height Move layout instead of compositing

Fix 1: always give media dimensions

Modern browsers use the width and height attributes to compute an aspect ratio before the image loads and reserve exactly that space, even when CSS makes the image fluid.

HTML
<img src="/img/team.jpg" width="800" height="450" alt="The team on stage">
CSS
img { max-width: 100%; height: auto; }

You can also use aspect-ratio in CSS for any box, such as a video embed:

CSS
.embed { aspect-ratio: 16 / 9; width: 100%; }
Reserved space before an image loads

The point of the demo is the pattern: with the ratio declared up front (images get it from their width and height attributes), the box is the right size before any byte of the image is downloaded.

Fix 2: reserve space for late content

If you cannot know the exact size of an ad or widget, give its container a min-height matching the most common size. A bit of empty space is better than a jump.

Fix 3: insert content below, not above

If you must show a banner or cookie notice, place it where it does not push visible content, for example as an overlay fixed to the bottom, or reserve its space in the layout from the start.

Fix 4: animate with transforms

Animating top, margin or height forces layout each frame and moves neighbours. Animating transform and opacity changes only how the element is composited and never causes a shift.

CSS
.panel { transition: transform 0.2s; }
.panel.open { transform: translateY(0); }

Fix 5: stabilise fonts

A different fallback font changes line breaks when the real font loads. Choose a similar fallback, set font-display and adjust metrics (see web fonts).

Measuring CLS yourself

JS Compute CLS the way the browser does

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The output shows windows of 0.1 and 0.12. CLS takes the worst window, 0.12, which is "needs improvement".

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which pair of attributes prevents most image-related layout shift?

    Width and height let the browser reserve the correct aspect ratio before the image loads.
  2. 2. Which CSS animation is safest for layout stability?

    Transform is composited and does not change layout of neighbouring elements.
  3. 3. Shifts within 500 ms of a user's click are

    Shifts that follow user input are expected, so they are flagged hadRecentInput and ignored.
  4. 4. What is a good CLS value?

    0.1 or less at p75 is good; over 0.25 is poor.

Summary

  • A shift score is impact fraction times distance fraction; CLS reports the worst burst.
  • Set width, height or aspect-ratio on media; reserve space for ads and embeds.
  • Avoid inserting content above existing content; animate with transform.
  • Pick fallback fonts that match your web font.

Next: TTFB, FCP and Other Metrics