How to Avoid Layout Shift

You go to tap a button, the page jumps, and you tap an advert instead. Layout shift is a small technical problem which causes a big annoyance, and it is usually straightforward to fix.

How to Avoid Layout Shift — Troiana insight cover

In short

To avoid layout shift, reserve space for everything that loads late. Give images and videos width and height attributes or a CSS aspect-ratio, set fixed slots for ads and embeds, don't insert content above existing content, load web fonts carefully and animate with transform. Google's target is a CLS of 0.1 or less, for at least 75% of page visits, measured from real users.

What layout shift is

Layout shift happens when something visible moves unexpectedly because something else loaded or changed size. Google measures it through Cumulative Layout Shift (CLS), one of the Core Web Vitals.

CLS scoreRating
0.1 or lessGood
0.1 to 0.25Needs improvement
Over 0.25Poor

Shifts within 500 milliseconds of a user interaction, such as opening an accordion they clicked, don't count. Users expect those.

Good: 0.1 or lessNeeds improvement: to 0.25
Over 0.25 rates as poor.
Google's bands for CLS. Shifts just after a user interaction don't count, because the person expected something to move.

Measure it first

Field data shows what real users get, and it's what counts for Core Web Vitals. Look at Search Console's report and PageSpeed Insights. See how to measure real user performance.

Lab tools, such as Lighthouse, only capture shifts during the initial load, so they often miss those that happen when scrolling. Don't assume that a good score means that there's no problem.

To find the cause, open Chrome DevTools, go to the Performance panel, then record the page loading and look at the layout shifts it marks. Scroll the page while recording. It highlights which elements moved.

You can also log shifts from real users using Google's web-vitals library, which can report the element most responsible.

Fix 1: Images and videos

This is the most common cause. Without dimensions, the browser doesn't know how much space to save until the image arrives.

<img src="team.jpg" width="1200" height="800" alt="Our team" />
img {
  max-width: 100%;
  height: auto;
}

The browser uses the width and height to work out the ratio, even in a responsive layout. For things like video embeds, use the CSS aspect-ratio:

No space reserved
The sentence you were reading.Buy now
When the image loads, all of this moves down.
Space reserved
The sentence you were reading.Buy now
Width and height hold the space, so nothing moves.
A drawn example. The width and height attributes give the browser the ratio before the file arrives.
.video {
  aspect-ratio: 16 / 9;
  width: 100%;
}

Fix 2: Ads and embeds

Third-party content often loads late and at unknown sizes.

  • Reserve a slot of the most likely size, using min-height.
  • Don't collapse empty slots in ways that move content.
  • Put ads further down the page, where possible, as shifts near the top tend to affect more of the screen.
  • Use placeholders for embeds, like maps and social posts, or click-to-load them. It's often much faster, too.
.ad-slot {
  min-height: 250px;
}

Fix 3: Banners and injected content

Cookie notices, promotional bars and “sign up” prompts often push the whole page down.

  • Overlay the banner, with position: fixed, rather than pushing content. See how to design a cookie banner people don't hate.
  • Or include it in the server-rendered HTML, so that the space is there from the beginning.
  • Avoid inserting content above what's already on screen, unless the user asked for it.

If new items arrive in a feed, then show a “New items” button rather than moving everything down.

Fix 4: Web fonts

When a custom font replaces a fallback, text may reflow if the sizes differ.

  • Preload your key font files.
  • Choose a similar fallback font.
  • Use size-adjust descriptors, such as size-adjust and ascent-override, to match the fallback to your web font. Next.js's font tool, and tools like Fontaine, can do this for you.
  • Use font-display: optional if you'd rather skip the swap altogether on slow connections.
  • Host fonts yourself, for faster and more predictable loading.

See also how to set up a type scale with CSS variables.

Fix 5: Dynamic content in your app

In JavaScript apps, shifts often happen when data arrives and fills a space.

  • Use skeletons or placeholders of the right size, so the final content doesn't push others around.
  • Set a fixed height for known layouts, such as table rows or cards.
  • Avoid loaders that are much smaller than the content that replaces them.
  • Render key content on the server when possible. See Server Components explained.
  • Avoid client-side swaps of personalised content above the fold, such as A/B tests that change a heading after load. See feature flags.

Fix 6: Animations

Animating top, left, width, height or margins moves other elements, can cause layout shifts and may be slow. Use transform and opacity instead:

/* Causes layout work */
.panel { transition: height 200ms; }

/* Doesn't */
.panel { transition: transform 200ms; }

Remember to respect people's preference for reduced motion, as well. See motion design patterns worth adopting now.

Fix 7: Things that often get forgotten

  • Back/forward cache: if a page is restored from this cache, it doesn't shift. Avoid things that prevent the page from being eligible, such as no-store on pages which could safely use the cache, and unload handlers.
  • Scrollbars appearing: scrollbar-gutter: stable reserves space for them.
  • Sticky headers that change height while scrolling.
  • Carousels that load slides of varying heights.
  • Lazy-loaded images with no dimensions. Lazy loading itself is fine if space has been reserved.
  • Web components and widgets that render late, such as review stars or chat buttons.

A quick audit routine

  1. Check Search Console for groups of URLs that are flagged as poor.
  2. Pick the template with the most traffic.
  3. Load it on a throttled mobile connection, then scroll to the bottom.
  4. Record it using DevTools, and note what moves.
  5. Fix the biggest shift first.
  6. Deploy, then wait for field data to update. Google's data covers a rolling 28-day period, so results take time to appear.

Fix at the template level. Most sites have just a few templates, so one change can improve thousands of pages.

Does it affect rankings?

Core Web Vitals are part of Google's page experience signals, but relevance matters far more. A perfect CLS won't rescue weak content. Fix it mainly because it's annoying, and it can cost you clicks and sales. See performance is a feature, not a phase.

Keep it fixed

Add performance checks to your release process, because new ads, widgets and A/B tests will bring the problem back. Add width and height rules to your component library, and check real-user data after adding any third-party script. AI-generated code often leaves out image dimensions, so check for this in your reviews.

If your site's Core Web Vitals need attention, book a call.

Common questions

What is a good CLS score?

Google considers 0.1 or less to be good, between 0.1 and 0.25 to need improvement, and above 0.25 to be poor. The target is for at least 75% of page visits to meet the good threshold, using data from real users.

What causes layout shift?

Common causes are images and videos without dimensions, ads and embeds with no reserved space, banners that push content down, web fonts swapping in, and content inserted after the page loads. Animating layout properties such as height can also cause it.

How do I find what's causing layout shift?

Use Chrome DevTools' Performance panel to record the page while loading and scrolling. It marks shifts and affected elements. Check field data in Search Console and PageSpeed Insights too, because lab tests often miss shifts that happen later.

Does layout shift affect SEO?

It's part of Core Web Vitals, which feed into Google's page experience signals, but relevance and content quality matter far more. It's worth fixing, mainly because it frustrates users and can cause misclicks, and these can reduce conversions.

Have something worth building right?