Ad code & integration

How to prevent layout shift (CLS) from AdSense ads by reserving slot space

Last updated Published By the editorial team6 min5 sources

Key takeaways

  • web.dev lists ads, embeds and iframes without dimensions, and dynamically injected ads, among the most common causes of poor Cumulative Layout Shift.
  • Reserving space with min-height, or giving the ad unit exact CSS sizes per breakpoint, stops content from moving when the ad arrives.
  • Google Publisher Tag guidance notes that reserving the largest possible size eliminates shifts but leaves blank space when a smaller ad serves.
  • AdSense collapses unfilled ad units only when they are outside the viewport, to avoid page reflow; other unfilled units keep their size.
  • A good CLS is 0.1 or less at the 75th percentile of page loads, measured separately for mobile and desktop.
A tower crane rising over a building construction site
Image: Crane at construction site - geograph.org.uk - 7593527,Kevin Waterhouse,CC BY-SA 2.0,via Wikimedia Commons.

To stop AdSense ads from shifting your content, give every ad slot its space before the ad loads: a wrapper with min-height, or exact CSS sizes per breakpoint as Google documents for responsive units. Keep the space when a slot goes unfilled near the top of the page, and push automatically inserted ads away from the first screen. Then confirm the effect with field data, not only Lighthouse.

Why do ads cause layout shift?

The web.dev guide to optimizing CLS lists among the most common causes “Ads, embeds, and iframes without dimensions” and “Dynamically injected content such as ads, embeds, and iframes without dimensions”. It adds: “Ads are one of the largest contributors to layout shifts on the web.”

An empty <ins class="adsbygoogle"> has no height until the ad arrives. When it does, everything below moves down. If that happens while the user reads or is about to tap, it is both a CLS problem and an accidental-click risk.

CLS itself is measured in bursts called session windows: shifts less than one second apart, up to five seconds per window, and the worst window counts (web.dev CLS). A good score is 0.1 or less at the 75th percentile of page loads.

Method 1: reserve space with min-height

The simplest fix: wrap the unit and give the wrapper a minimum height that matches what usually serves there.

<div class="ad-slot ad-slot--in-content">
  <ins class="adsbygoogle"
       style="display:block"
       data-ad-client="ca-pub-0000000000000000"
       data-ad-slot="1234567890"
       data-ad-format="auto"
       data-full-width-responsive="true"></ins>
  <script>
       (adsbygoogle = window.adsbygoogle || []).push({});
  </script>
</div>
.ad-slot--in-content { min-height: 280px; }

@media (min-width: 768px) {
  .ad-slot--in-content { min-height: 250px; }
}

web.dev recommends exactly this: “add a min-height CSS rule to reserve space”, possibly with media queries “to account for subtle differences in ad or placeholder sizes across form factors”. min-height lets the box grow if a taller ad serves, so you never clip an ad.

Do not use height with overflow: hidden on the wrapper. A responsive ad can be taller than you expect, and clipping it hides part of the ad. Google’s code modification policy does not allow ad code implemented so that it is covered by content.

Method 2: exact sizes with CSS (no guessing)

If you know what you want per breakpoint, Google’s responsive code modification article lets you set the unit’s exact size with media queries. The slot then has its final dimensions before any ad request:

<style>
  .ad_article_top { width: 320px; height: 100px; }
  @media (min-width: 500px) { .ad_article_top { width: 468px; height: 60px; } }
  @media (min-width: 800px) { .ad_article_top { width: 728px; height: 90px; } }
</style>
<ins class="adsbygoogle ad_article_top"
     style="display:block"
     data-ad-client="ca-pub-0000000000000000"
     data-ad-slot="1234567890"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>

Google’s conditions: remove data-ad-format and data-full-width-responsive, use a unique class name, and keep the <style> in the page (external style sheets are “not officially supported”). The responsive ad units guide has the full rules.

How do I choose the reserved size?

Google’s ad tag documentation (Minimize layout shift, written for Google Publisher Tag but applicable to any ad slot) describes the trade-off:

Strategy Layout shift Blank space When it fits
Reserve the largest size that can serve Eliminated Possible, when a smaller ad serves Slots near the top of the page
Reserve the smallest size Some, when a larger ad serves None Slots that mostly fill with small ads
Reserve the most likely size (from your data) Reduced Some Most in-content slots
Exact CSS size (Method 2) None None beyond the chosen size When you accept a fixed format per breakpoint

The same page advises against shrinking a slot after a smaller creative serves, since that “would incur an additional layout shift”, and suggests centering the ad in the reserved box instead:

.ad-slot {
  display: flex;
  align-items: center;
  justify-content: center;
}
.ad-slot > ins.adsbygoogle { width: 100%; }

What about In-article, In-feed and Multiplex units?

Native units cannot use the exact-size method: Google states that the height of an In-article ad “is automatically adjusted by AdSense”, and In-feed units need a parent with “a variable height”, because a fixed-height parent can distort the ads. Two consequences:

  • Use min-height on the wrapper, never height. Start from the heights you observe in DevTools on a phone and on a desktop, and set one value per breakpoint.
  • Accept some residual shift. When the served native ad is taller than your reserve, the box grows. Placing these units lower in the article, where Google already suggests In-article code (“two paragraphs below the start”), keeps that growth away from the first screen.

Sources: Google’s Create an In-article ad unit and How to place In-feed ad code.

What happens when a slot stays unfilled?

AdSense adds data-ad-status to the <ins> once the request completes (data-ad-status article). For unfilled units, Google writes: “We only collapse ad units when they are not going to cause page reflow, meaning only ad units outside of the viewport are collapsed.” Units in view keep their size and show blank space.

That matches web.dev’s advice: “Removing the space set aside for elements can cause just as much CLS as inserting content.” So:

  • For slots at the top of the page, keep the reserved space when unfilled, and consider a neutral placeholder or house content.
  • For slots far below the fold, hiding unfilled units with CSS is fine; by the time the user scrolls there, the request is long finished.
/* Collapse only the below-the-fold slots you choose */
.ad-slot--lower:has(ins.adsbygoogle[data-ad-status="unfilled"]) {
  display: none;
}

The :has() selector collapses the wrapper as well, so the reserved min-height goes away too. The unfilled ad slots guide covers fallbacks and the policy limits.

How do I keep Auto ads from shifting content?

In-page Auto ads are inserted into your layout, so you cannot pre-reserve their space. Overlay formats (anchor, vignette, side rail) are placed “over a page’s content without affecting its layout”, per Google. To reduce shifts from in-page Auto ads:

  1. In Auto ads settings, lower “Maximum number of ads on a page” and raise “Minimum distance between ads on a page”.
  2. Use excluded areas to keep in-page Auto ads out of the area right under your header. Excluded areas use CSS selectors, so re-check them after a redesign.
  3. Place your own reserved manual units in the top positions; web.dev notes that content injected near the top of the viewport “usually causes greater layout shifts” than content injected lower.
  4. Compare field CLS on pages with and without Auto ads before changing settings site-wide. The Auto ads vs manual units guide explains page exclusions for a clean comparison.

How do I find which ad caused the shift?

  1. Open PageSpeed Insights and compare the field CLS (real users) with the lab CLS. web.dev explains that a large gap usually means post-load shifts, for example ads appearing as the user scrolls.
  2. In Chrome DevTools, record a trace in the Performance panel while scrolling. The Layout Shifts track shows each shift and the elements that moved.
  3. Remember that the elements listed are often the ones pushed down, not the cause. Look at what was inserted just above them.
  4. For real-user data on every page view, add the open-source web-vitals library and log the largest shift sources. See measuring real-user Core Web Vitals.

Common mistakes

  • Reserving space on desktop only. Mobile is where full-width responsive ads change height the most. Set breakpoint-specific min-height values.
  • Collapsing every unfilled slot. Hiding an on-screen slot after load is a shift. Collapse only slots out of view.
  • Lazy-loading ads above the fold. A slot that loads late near the top shifts content in front of the user. Load top slots normally; see lazy loading AdSense.
  • Fixed heights with overflow: hidden. They clip ads, and Google does not allow ads covered by content.
  • Trusting one Lighthouse run. Lab tests miss shifts that happen while scrolling. Check field data collected over several weeks.

Frequently asked questions

How much space should I reserve for an AdSense slot?

There is no single correct value. Google's ad tag guidance describes three choices: reserve the largest size that can serve (no shift, possible blank space), the smallest (no blank space, some shift when a larger ad serves), or the most likely size based on your history. For full control, give the unit exact CSS sizes per breakpoint, which Google documents as an allowed modification.

Does hiding unfilled ad slots cause layout shift?

It can. web.dev warns that removing reserved space can cause as much CLS as inserting content. AdSense itself only collapses unfilled units outside the viewport for that reason. If you hide unfilled slots with data-ad-status, prefer slots below the fold, or keep the space and show a placeholder in slots that are on screen at load.

Do Auto ads cause CLS?

In-page Auto ads are inserted into the content after the page loads, which can push content down. Overlay formats such as anchors and vignettes sit over the page without affecting layout. Measure with field data, compare pages with and without Auto ads, and use the ad load and excluded areas settings to keep inserted ads away from the top of the viewport.

Why is my CLS fine in Lighthouse but poor in Search Console?

Lighthouse measures a basic page load in a lab. Field data from the Chrome UX Report, used by Search Console and PageSpeed Insights, covers the full life of the page, including shifts after scrolling when lazy-loaded ads appear. Ads injected below the fold during scrolling count in the field but often not in the lab.

Sources

  1. Optimize Cumulative Layout Shift · web.dev
  2. Cumulative Layout Shift (CLS) · web.dev
  3. Minimize layout shift · Google Publisher Tag (Google for Developers)
  4. Use the data-ad-status parameter to hide unfilled ad units · Google AdSense Help
  5. How to modify your responsive ad code · Google AdSense Help