Cumulative Layout Shift

Cumulative Layout Shift (CLS) measures how much content moves around on the page after being rendered. The lower the CLS is the better. A value of 0 means the layout is completely stable.

Layout shifts hurt user experience, as they can cause accidental clicks on the wrong element or make users lose track of where they were when reading an article.

CLS is one of the Core Web Vitals, a set of three key web performance metrics defined by Google. From 2021 it will contribute to the page experience signal that Google uses for search rankings.

Example of layout shift

This filmstrip shows an example of page content shifting around after first being rendered. When the banner loads, the heading and article below it shift downward to make space for the banner.

For this page the CLS value is 0.13.

Comulative Layout Shift filmstrip showing different rendering stages

If you look closely there are actually two layout shifts on this page. The first one occurs after 2.5s when the webfont loads, causing the title and description to re-render. After this, the description takes up a little less space, and the content below it shifts upward.

Cumulative Layout Shift in Lighthouse

You can find the CLS metric as one of the 6 key metrics at the top of each Lighthouse report.

The filmstrip below can help identify what's causing the layout shift.

Performance metrics at the top of the Lighthouse report

There's also a more detailed "Avoid large layout shifts" audit, which breaks down different LayoutShift events as reported by Chrome.

CLS audit in the Lighthouse Performance section

How does Cumulative Layout Shift affect Lighthouse scores?

CLS currently determines 5% over the overall Lighthouse Performance score.

The CLS metric should be as low as possible, ideally below 0.1. A CLS above 0.25 is considered poor.

Breakdown of the Lighthouse Performance score

How do reduce Cumulative Layout Shift

Layout shifts during the initial page load usually happen when the initial page HTML has been returned by the server, but additional resources like JavaScript and images are still loading. When these resources do load the layout shift occurs.

A common example would be ads, which are often loaded from a different server owned by the ad network, and therefore might take an extra second or two to load.

You can prevent layout shifts if you know the size of the element that's being loaded. In that case, you can provide an empty placeholder with the appropriate height, and then fill in the contents later.

Layout Instability API

You can use the Layout Instability to detect layout shifts inside your application, and calculate the Cumulative Layout Shift on the fly.

Here's how to use a PerformanceObserver to retrieve and monitor layout-shift entries:

var cumulativeLayoutShift = 0;

const observer = new PerformanceObserver((list) => {
  list.getEntries().forEach((entry) => {
    console.log({ entry });
    // Don't count if the layout shift is a result of user interaction
    if (!entry.hadRecentInput) {
      cumulativeLayoutShift += entry.value;
    }
  });
});

// `buffered: true` to report layout shifts that have already happened on the page
observer.observe({ type: "layout-shift", buffered: true });

A layout-shift entry looks like this – each event is attributed to a set of DOM nodes.

Entry in the Layout Instability API

Layout Shifts in Chrome DevTools

Chrome DevTools can highlight layout shifts as part of its Animation tooling. First, click the three dots in the top right corner, then select More tools, and finally click Animations.

DevTools More tools menu item

You can then enable highlighting for Layout Shift Regions.

DevTools animations pane

Now, when content moves on the page, Chrome will show a blue highlight rectangle for the affected DOM nodes.

Layout Shifts highlighted on the inspected page

Cumulative Layout Shift in DebugBear

You can find the Cumulative Layout Shift metric in the page Overview tab, just below the filmstrip.

Cumulative Layout Shift in DebugBear


Still need help? Contact matt@debugbear.com for support.
DebugBear is a website monitoring tool built for front-end developers. Track performance metrics and Lighthouse scores in CI and production. Learn more.
© 2020 DebugBear Ltd