Skip to content
Sandeep Kumar
Updated 5 min read

How to fix Cumulative Layout Shift in Next.js

CLS is caused by content that arrives without reserved space: images, fonts, ads and client-only widgets. How to find each shift and reserve space for it.

Sandeep Kumar, Full-stack EngineerSandeep KumarFull-stack Engineer
On this page
  1. Find the shifts first
  2. Images without dimensions
  3. Fonts that re-flow the page
  4. The client-only shift, which is self-inflicted
  5. Everything else that arrives late
  6. CLS counts the whole visit
  7. One instruction, several costumes

Cumulative Layout Shift is the Core Web Vital that most often looks fine on your machine and fails in the field. Your fonts are cached, your images are warm, your connection is fast, so nothing moves. On a cold visit over a mid-range phone connection, everything moves.

The cause is always the same. Content arrived and the browser had not reserved space for it. Every fix below is a different costume for reserving that space in advance.

Find the shifts first

CLS is a sum, and usually one or two shifts dominate while the rest are noise. Harden everything and you will spend a day for a rounding error.

In DevTools, record a page load in the Performance panel and look at the Experience track. Each shift is a marker, and selecting one names the element that moved and what it contributed.

For real sessions, the Layout Instability API reports the same thing from the field:

js
new PerformanceObserver((list) => {
  for (const entry of list.getEntries()) {
    if (entry.hadRecentInput) continue;      // user-initiated shifts don't count
    console.log(entry.value, entry.sources.map((s) => s.node));
  }
}).observe({ type: "layout-shift", buffered: true });

The sources array is the useful part. It points at the nodes that moved, which in my experience is frequently not the element anyone suspected.

Images without dimensions

An image with no dimensions occupies zero height until it loads, then suddenly occupies several hundred pixels and shoves everything below it down the page.

With a static import, next/image reads the intrinsic size at build time and reserves the space for you:

tsx
import Image from "next/image";
import cover from "../public/cover.webp";

<Image src={cover} alt="…" className="w-full" />   // dimensions known, space reserved

With a remote source nothing is known at build time, so you supply it:

tsx
<Image src={url} alt="…" width={1200} height={630} className="h-auto w-full" />

Those numbers are an aspect ratio, not a display size. CSS still controls how large it renders. Their only job is to let the browser compute the right height before a single byte arrives.

For images of unknown ratio, reserve the box yourself and crop into it:

tsx
<div className="relative aspect-[16/9] w-full">
  <Image src={url} alt="…" fill className="object-cover" />
</div>

fill needs a positioned parent with its own dimensions. Without one you are back to reserving nothing, which is a mistake that looks like it works until it is on a phone.

Fonts that re-flow the page

A web font loading after first paint re-flows every line of text it touches. How bad it is depends on how far the fallback's metrics are from the real font: if the fallback is narrower, lines re-wrap and everything below them moves.

next/font does the hard part. It self-hosts, preloads, and generates a size-adjusted fallback whose metrics are matched to the real font, so the swap is close to invisible.

app/layout.tsxts
import { Inter } from "next/font/google";

const inter = Inter({ subsets: ["latin"], display: "swap", variable: "--font-inter" });

Two things to check afterwards. Load only the weights and subsets you use, since each one is a download competing with first paint. And confirm no @import from a font CDN survived in your CSS, because that quietly reintroduces the exact round trip next/font removed. I hit that one myself, and wrote it up in how I cut First Contentful Paint.

The client-only shift, which is self-inflicted

This is the Next.js-specific one, and it is in almost every codebase I have looked at.

A component that renders nothing on the server and real content after mount produces a shift on every single load:

tsx
// ❌ renders null on the server, content after hydration — a shift on every load
"use client";
export function ThemeToggle() {
  const [mounted, setMounted] = useState(false);
  useEffect(() => setMounted(true), []);
  if (!mounted) return null;
  return <button>…</button>;
}

The motivation is avoiding a hydration mismatch, which is reasonable, because the server genuinely cannot know the user's theme or locale or viewport. The fix is not to render nothing. It is to render something of the same size.

tsx
// ✅ occupies its final size from the first paint
if (!mounted) return <span className="block size-11" aria-hidden />;

Better still, avoid the mismatch. For a theme toggle, render both icons and let CSS choose, so server and client markup are identical and nothing waits for mount:

tsx
<svg className="dark:hidden" />
<svg className="hidden dark:block" />

The same applies to next/dynamic with ssr: false. Give it a loading placeholder sized like the real component, not null.

Everything else that arrives late

Ads and embeds want their slot reserved at its known size. A collapsed slot that expands when the ad fills is pure CLS and it is the most common failure on content sites by a distance.

Banners and notices inserted at the top of the document push the whole page down. Overlay them, or render them in the initial HTML so they are part of the first layout rather than an edit to it.

Icon fonts shift inline text when they swap in late. Inline SVG does not, which is most of the argument for inline SVG.

And if you hand-roll @font-face rather than using next/font, the metric matching is now your job, size-adjust included.

CLS counts the whole visit

Not just load. A shift triggered by something loading five seconds in still counts, which is why lazy content below the fold matters even though nobody has scrolled to it yet.

Test on a throttled connection with an empty cache, because a warm cache hides nearly every shift on this page. Then read Search Console's 75th percentile and treat that as the verdict over any lab score.

One instruction, several costumes

Images get dimensions or an aspect-ratio box. Fonts get a metric-matched fallback. Client-only components get a placeholder the size of the real thing. Ads and banners get their slot in the initial layout.

All of it is the same instruction: reserve the space before the content arrives.

Do it consistently and CLS stops being a score you chase after each report and becomes a property the layout has by construction, which is a much cheaper place to be.

Frequently asked

What is a good Cumulative Layout Shift score?

0.1 or less at the 75th percentile of real users. Between 0.1 and 0.25 needs improvement, and above 0.25 is poor. Unlike the loading metrics, CLS accumulates over the whole lifetime of the page visit, so a shift caused by something loading late still counts against you minutes after load.

How do I find what is causing layout shift?

Chrome DevTools' Performance panel marks each shift on the Experience track, and selecting one shows the element that moved and its score contribution. For field data, the Layout Instability API can report shift sources from real sessions. Always identify the specific element first: CLS is a sum of individual shifts, and usually one or two dominate.

Does next/image prevent layout shift?

It does when the dimensions are known. A static import gives Next the intrinsic width and height at build time, and it reserves the space before the image loads. A remote src requires explicit width and height props, or the fill prop with a positioned parent that has its own dimensions. Without either, the space is not reserved and the image will shift content when it arrives.

Why does my page shift when fonts load?

Because the fallback font and the web font have different metrics, so text reflows when the real font arrives. next/font generates a size-adjusted fallback matched to the web font's metrics, which makes the swap close to invisible. Without that adjustment, swap is visibly jarring and shift is unavoidable.

Found this useful?Share on XShare on LinkedIn

Related reading