Skip to content

Troubleshooting guide

How to fix CLS (Cumulative Layout Shift)

Layout shifts happen when the browser does not know how much space something needs. Tell it in advance and most CLS disappears.

3 min read11 sectionsUpdated

On this page

Key points

  • Give every image and video dimensions or an aspect ratio.
  • Reserve space for ads, embeds and dynamic content.
  • Never insert content above what the user is reading, unless they asked for it.
  • Match fallback fonts to web fonts to avoid text reflow.

Images and video

html
<!-- width and height let the browser compute the aspect ratio before the image loads -->
<img src="/team.webp" width="1200" height="800" alt="Our team" style="width:100%;height:auto">

<!-- for responsive containers -->
<div style="aspect-ratio: 16 / 9"><iframe src="https://www.youtube.com/embed/..." title="Demo" style="width:100%;height:100%"></iframe></div>

Ads, embeds and widgets

Ad slots and third-party widgets resize when they load. Reserve the most common size up front:

css
.ad-slot { min-height: 250px; }            /* the usual creative height */
.newsletter-embed { min-height: 420px; }   /* measured once from the real widget */

Banners, notices and late content

Cookie banners, promos and "you may also like" blocks inserted at the top push everything down. Options:

  • Overlay them with position: fixed, so they cover rather than push.
  • Reserve their space in the server-rendered HTML, even before the content arrives.
  • Insert below the viewport, or only after a user action.

Web fonts

When a web font replaces a fallback font with different metrics, every line of text can reflow. Preload the main font file and adjust the fallback to match:

css
@font-face {
  font-family: "Brand Fallback";
  src: local("Arial");
  size-adjust: 104%;
  ascent-override: 92%;
  descent-override: 24%;
}
body { font-family: "Brand", "Brand Fallback", sans-serif; }

Animations

Animating top, left, width, height or margins moves layout and can count as shifts. Animate transform and opacity instead, which run on the compositor and do not shift other elements.

Single-page apps and skeletons

Client-side navigation brings its own shifts: data arrives after the route renders and pushes the layout around. Render a skeleton with the final dimensions while data loads, keep list items a fixed height where you can, and avoid swapping a spinner for content of a different size. For infinite scroll, append below the viewport and keep a placeholder for the next batch, so the content under the reader's eyes never moves.

Sticky headers that change height on scroll are another common source. Animate them with transform, or keep the height constant and change only the background.

Verify

Use the layout-shift debugging snippet from our CLS definition while you load and scroll the page, then confirm with field data: CLS at the 75th percentile per page and device from real user monitoring.

Frequently asked questions

My CLS is fine in Lighthouse but poor in the field. Why?

Lighthouse measures only the initial load without scrolling. Real visitors scroll, accept cookie banners and see late ads, which cause shifts the lab never sees.

Do single-page app route changes count?

Shifts right after a click are excluded for 500 ms. Shifts that happen later, as data arrives, do count.

  • How to reduce First Contentful Paint (FCP): Reduce First Contentful Paint: lower TTFB, remove render-blocking CSS and scripts, load fonts sensibly and measure improvements with real-user data.
  • How to reduce TTFB (server response time): Practical ways to reduce time to first byte: edge caching, fewer redirects, faster queries, warm runtimes and closer servers, in order of impact.
  • What is log optimization?: Log optimization explained: finding the noisiest log sources, levels and sampling, structured fields, dropping duplicates, retention tiers.
  • ERR_ADDRESS_UNREACHABLE: how to fix it: Fix ERR_ADDRESS_UNREACHABLE: no route to the server, wrong IP in DNS, IPv6 problems, private addresses and firewalls, for visitors and server owners.
  • ERR_INTERNET_DISCONNECTED: how to fix it: Fix ERR_INTERNET_DISCONNECTED in Chrome: reconnect Wi-Fi, check the router, disable proxies and VPNs, reset network settings, and when apps say offline.
  • What is alert flapping?: Alert flapping explained: why alerts fire and resolve over and over, and the fixes that work: sustained conditions, hysteresis, cooldowns and grouping.

Watch your own site and services with VitalRix

Uptime checks, APM, logs, real-user monitoring, alerts and status pages in one place. Every product has a free tier and no credit card is needed.