Skip to content

Latest commit

 

History

History
95 lines (74 loc) · 5.05 KB

cls.md

File metadata and controls

95 lines (74 loc) · 5.05 KB

CLS

Definition

The Cumulative Layout Shift (CLS) metric measures the largest burst of layout shift scores for every unexpected layout shift that occurs during the entire lifecycle of a page.

Components

CLS is essentially composed of:

Value Range

CLS Mobile/Desktop
Good < 0.1
Needs Improvement 0.1-0.25
Poor > 0.25

Most common issues

  • Position of DOM elements is changed
  • Dimension of DOM elements is changed
  • DOM elements are inserted or removed
  • CSS animations that trigger layout are running on the page
  • CSS position of a DOM element
  • Images, ads or social embeds are loaded without proper space being reserved
  • Async content is rendering without proper space being reserved
  • Use of custom fonts with no proper fallback
  • Loading async content that causes the scrollbar to appear lazily
  • Cookie consent banners injected at the top of the page pushing all content down

Most common optimizations

For step-by-step fixes with code examples and anti-patterns, see the use-case runbooks: layout stability, images and fonts.

How to measure

Manually

new PerformanceObserver((entryList) => {
  for (const entry of entryList.getEntries()) {
    console.log('Layout shift:', entry);
  }
}).observe({ type: 'layout-shift', buffered: true });

Using web-vitals.js

import { onCLS } from 'web-vitals/attribution';

// Measure and log CLS in all situations where it needs to be reported,
// including the largest shifted element.
onCLS(({ value, attribution }) => {
  console.log('CLS:', value, attribution.largestShiftTarget, attribution);
});

How to debug

Follow the steps in one of:

References