Skip to content

The cookie banner that moves your whole page: fixing CLS

From the work/ 17 min read

Why a cookie banner pushes the page down and raises CLS, how to spot it in DevTools, and the CSS for a bottom banner that moves nothing, measured locally.

  • Performance
  • Core Web Vitals
In this article (10)
  1. What CLS measures and when it becomes a problem
  2. Why the cookie banner pushes the whole page down
  3. Find the shift in Chrome DevTools
  4. Reserve space for the banner
  5. The fix: a cookie banner fixed to the bottom
  6. Images and ads without dimensions
  7. Measure before and after
  8. Common mistakes
  9. Frequently asked questions
  10. Checklist

If your cookie banner appears at the top a second after load, it pushes the whole page down and raises your CLS (Cumulative Layout Shift). Take it out of the flow: fix it to the bottom over the content, or reserve its space in the HTML. On our test page, mobile CLS fell from 0.16 to 0.

Below you'll find why it happens, how to spot the shift in Chrome DevTools, the full CSS for a banner that pushes nothing and doesn't hide keyboard focus, and how to measure before and after. Every number was measured locally on a test page with Playwright, and we show how. If you already worked on your hero image's LCP, this is the next Core Web Vital on the list.

What CLS measures and when it becomes a problem

CLS measures how much content that was already on screen moves without anyone asking it to. According to web.dev's CLS guide, a layout shift happens whenever a visible element changes its start position from one rendered frame to the next. Each shift gets a score: the share of the viewport affected (impact fraction) times how far things moved (distance fraction). That distance is divided by the viewport's largest dimension, width or height, whichever is greater.

Shifts are not summed over the whole visit. They are grouped into session windows: shifts less than one second apart, in a window of five seconds at most. The page's CLS is the window with the highest total. The thresholds apply to the 75th percentile of page loads, segmented by mobile and desktop:

CLSRating
0.1 or lessGood
Above 0.1, up to 0.25Needs improvement
Above 0.25Poor

Three rules from the same guide explain most of what follows:

  • Only existing elements count. Adding a new element, or resizing one, is not a layout shift as long as it does not move other visible elements. A banner that appears over the content does not count; one that pushes the content does.
  • Shifts the user causes don't count. Shifts within 500 ms of a tap, click or keypress carry the hadRecentInput flag and are excluded. Scrolling, dragging and pinch-zoom are not that kind of input.
  • Animations with transform don't cause shifts. Moving something with translate doesn't count; moving it by changing top, bottom or margins does.

The pattern is almost always the same. The consent script loads async, which is right for LCP according to web.dev's best practices for cookie notices. It arrives after the page has painted, builds the banner and inserts it as the first child of <body>, in the normal document flow. The header, title, image and copy all drop by the banner's height at once. That same guide puts it plainly: top-of-screen cookie notices are a very common source of layout shift.

We reproduced it on a test product page: header, title, image and paragraphs, plus a consent script injected on the load event, the way a tag manager would, that responds 300 ms later. On mobile (390 × 844) the banner is 136 px tall and CLS came out at 0.1611, in "needs improvement", from a single shift. On desktop (1366 × 768) the same banner is 76 px tall and CLS was 0.0332. The formula explains the gap: on mobile the banner is taller, moves nearly the whole screen and the distance is divided by 844; on desktop it is divided by 1366.

the same cookie banner in two positionsbefore the bannerfirst paintbanner on top136 pxCLS 0.161everything dropsfixed at the bottomCLS 0nothing moveson top it pushes the page; fixed, it overlays itmeasured locally: test page at 390×844, Chromium 151 via Playwright

Scroll the figure sideways to see all of it

That is why you barely notice it on the laptop you work on, while your customers see it on their phones. And the banner shows up on every visit that has no stored consent yet, starting with every new customer's first one.

Find the shift in Chrome DevTools

Open your store in an Incognito window so there is no stored consent and the banner shows. Then, in Chrome DevTools:

  1. Open the Performance panel. It immediately shows your local LCP and CLS and, under the metric cards, a Layout shifts tab with a table of the shifts, per the Performance panel overview.
  2. Check Screenshots and click Record and reload. DevTools first navigates to about:blank, then records the full load.
  3. On the Layout shifts track, each shift is a purple diamond, and nearby shifts are grouped into clusters drawn as purple lines. Hovering a diamond highlights the element that moved on the page. Clicking it shows timings, scores, elements and potential culprits in the Summary tab, according to the Performance features reference.
  4. In the Insights panel on the left, open Layout shift culprits. Per its documentation, it offers a best guess at the cause: unsized images, injected iframes, unoptimized animations or web fonts. If it names none, the same page notes that every shift can still be fixed.

To see it without recording, open the Command Menu with Ctrl+Shift+P (Cmd+Shift+P on Mac), type rendering, pick Show Rendering and check Layout Shift Regions. When you reload, areas that shift flash purple for a moment, as the rendering performance guide describes.

where to look in the Chrome DevTools Performance panelRecord and reloadrecords the load from about:blank0 s1 s2 s3 sNetworkconsent.jsLayout shifts0.1611Mainclick the diamondSummaryscore 0.1611#site-header · #mainInsightsLayout shiftculpritsa sketch of the panel, not a screenshotscore and elements measured on the local test page, banner on top

Scroll the figure sideways to see all of it

If you'd rather have a number, paste this into the Console once the banner has appeared. It uses the LayoutShift interface and applies web.dev's session windows. It is the same script we measured with:

let cls = 0, windowValue = 0, windowStart = 0, lastShift = -Infinity;

new PerformanceObserver((list) => {
  for (const entry of list.getEntries()) {
    if (entry.hadRecentInput) continue;
    const t = entry.startTime;
    if (t - lastShift >= 1000 || t - windowStart >= 5000) {
      windowValue = 0;
      windowStart = t;
    }
    windowValue += entry.value;
    lastShift = t;
    cls = Math.max(cls, windowValue);
    console.log("shift", entry.value.toFixed(4), "CLS", cls.toFixed(4),
      entry.sources.map((s) => s.node));
  }
}).observe({ type: "layout-shift", buffered: true });
  1. cls holds the worst window; windowValue and windowStart track the current one; lastShift is the time of the previous shift.
  2. hadRecentInput drops shifts within 500 ms of a tap, click or keypress.
  3. If a second or more has passed since the last shift, or five since the window started, a new window begins.
  4. The shift's score is added to the window and the maximum is kept: that is the CLS.
  5. entry.sources lists up to five elements that moved, each with its node.
  6. buffered: true also delivers shifts that happened before the observer existed, so it works even when you paste it after the load.

Be careful with what sources tells you: those are the elements that moved, not the cause. In our test it listed the header (#site-header) and #main, not the banner, and the Debug layout shifts guide warns that the root cause can be a different element. When everything at the top moves and the shift lines up with the consent script arriving, the banner is the culprit. MDN marks this API as experimental and not Baseline, since it doesn't work in some widely used browsers, so measure in Chrome.

Reserve space for the banner

The first option web.dev offers is to reserve the space in the DOM before the banner arrives. If the content already starts lower, the banner fills a slot that was there all along and nothing moves. The slot has to come from the server's HTML, not from a script that runs after paint:

<!-- only when the request has no consent cookie -->
<div class="cookie-slot"></div>

<style>
  .cookie-slot { min-height: 8.5rem; }
  @media (min-width: 35rem) {
    .cookie-slot { min-height: 5rem; }
  }
</style>
  1. The server only prints .cookie-slot when the request carries no consent cookie. Print it for everyone and people who already accepted see an empty gap at the top.
  2. min-height: 8.5rem (136 px) is the banner's height on mobile.
  3. From 35rem wide (560 px), the banner fits in 80 px and the slot drops to 5rem.
  4. The consent script puts the banner inside .cookie-slot instead of at the start of <body>.

With those heights, CLS was 0 on mobile and desktop. But the approach is fragile, and we measured that too:

  • The height changes with the width. On our page the banner is 136 px tall up to 480 px wide, 80 px between 560 and 700, and 76 px from 767. With a single 80 px slot, the desktop one, the banner overflowed it by 56 px on mobile and CLS was 0.0601. Another language, another font or longer copy changes those heights.
  • If a script removes the slot, the shift comes back. When consent lives in localStorage and the server can't see it, the script deletes the slot once it learns it isn't needed: everything moves up 136 px and CLS was 0.1611, the same as reserving nothing.
  • Closing the banner moves the page, but it doesn't count. When we removed the slot after Accept, there was a 0.1611 shift flagged hadRecentInput, because it happened within 500 ms of the click, and CLS stayed at 0.

Reserving space works when the server knows whether consent exists and the banner's height is stable. If you can't guarantee both, take the banner out of the flow.

web.dev's other option is to keep the banner out of the document flow: a sticky footer or a modal over the content. Its cookie notice guide prefers the footer because it is unobtrusive and typically does not cause CLS. This is the CSS we tested:

.cookie-banner {
  position: fixed;
  inset: auto 0 0 0;
  z-index: 100;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem 1.5rem;
  max-height: 50vh;
  overflow-y: auto;
  padding: 1rem 1rem calc(1rem + env(safe-area-inset-bottom));
  background: #14171a;
  color: #f4f5f6;
}
.cookie-banner p { flex: 1 1 32ch; margin: 0; }
.cookie-banner a { color: inherit; }
.cookie-banner .actions { display: flex; gap: 0.5rem; }
.cookie-banner button {
  min-height: 44px;
  padding: 0 1.25rem;
  font: inherit;
  color: inherit;
  background: transparent;
  border: 1px solid currentColor;
  border-radius: 4px;
}
.cookie-banner :focus-visible { outline: 3px solid #ffd166; outline-offset: 2px; }
html { scroll-padding-bottom: var(--cookie-banner-h, 0px); }
body { padding-bottom: var(--cookie-banner-h, 0px); }
@media (prefers-reduced-motion: no-preference) {
  .cookie-banner { animation: cookie-banner-in 200ms ease-out; }
}
@keyframes cookie-banner-in { from { transform: translateY(100%); } }
  1. position: fixed with inset: auto 0 0 0 pins it to the bottom, full width and out of the flow: it appears over the content without moving anything.
  2. max-height: 50vh with overflow-y: auto keeps it from covering more than half the screen on a small phone or in landscape; if the copy doesn't fit, it scrolls inside.
  3. env(safe-area-inset-bottom) adds the safe area on phones with a home indicator; everywhere else it is 0.
  4. #f4f5f6 text on #14171a gives a 16.48:1 contrast ratio, above the 4.5:1 WCAG AA asks for. The link inherits the color and keeps its underline.
  5. Both buttons share one style and are 44 px tall. The focus outline is a 3 px yellow ring with 12.48:1 contrast against the banner background.
  6. scroll-padding-bottom and padding-bottom use the banner's height, which the script sets. The first keeps the focused element from ending up under the banner; the second lets the end of the page scroll above it.
  7. The entrance is a transform, which doesn't count as a layout shift, and it only runs when the visitor hasn't asked for reduced motion.

And this is the JavaScript that shows and removes it:

function showCookieBanner(banner) {
  const root = document.documentElement;
  const observer = new ResizeObserver(() => {
    root.style.setProperty("--cookie-banner-h", banner.offsetHeight + "px");
  });
  document.body.prepend(banner);
  observer.observe(banner);
  banner.addEventListener("click", (event) => {
    const button = event.target.closest("button[data-consent]");
    if (!button) return;
    saveConsent(button.dataset.consent);
    observer.disconnect();
    banner.remove();
    root.style.removeProperty("--cookie-banner-h");
  });
}
  1. ResizeObserver copies the banner's real height into --cookie-banner-h whenever it changes, for example when the phone rotates.
  2. prepend puts it at the start of <body>. Since it is fixed, that moves nothing, and its controls are the first thing the Tab key reaches.
  3. One listener handles both buttons through their data-consent. saveConsent is yours: the call to your consent platform or your own cookie. In the test it was a stub that called nothing.
  4. Once the visitor chooses, it removes the banner and the variable, and padding-bottom goes back to 0. In the test, CLS stayed at 0 after Reject.

With the fixed banner, the same test pages logged no shifts at all: CLS 0 on mobile and desktop. With the same entrance animating bottom instead of transform, Chromium logged 9 small shifts of the banner itself and CLS was 0.0143 on mobile. Far from the threshold, but not zero, and it adds to whatever else you have.

Don't cover what it shouldn't

Something fixed to the bottom can cover things, and that is an accessibility problem. WCAG 2.2 success criterion 2.4.11 Focus Not Obscured (Minimum), level AA, requires that a component receiving keyboard focus is not entirely hidden by author-created content, and its Understanding document uses a sticky cookie banner as an example. Technique C43 solves it with scroll-padding plus page padding-bottom, which is what our CSS does. We checked it: we placed a link right under the banner and tabbed to it. Without scroll-padding-bottom, the link's center stayed under the banner; with it, Chromium scrolled the page and the link came into view.

On mobile, also check your theme's sticky Add to cart bar if it has one: in our test the banner covered it completely. Lift it with the same variable and translate, which doesn't count as a shift: .your-sticky-bar { translate: 0 calc(-1 * var(--cookie-banner-h, 0px)); }. Lifting it by changing bottom made Chromium log a 0.0187 shift.

Finally, we ran axe-core 4.12.1 with the WCAG 2.0, 2.1 and 2.2 A and AA rules on the page with the banner showing: zero violations, and all four contrast checks on the banner passed.

Images and ads without dimensions

The banner is the most visible case, but the mechanism repeats across the other causes in web.dev's Optimize CLS guide: images without dimensions; ads, embeds and iframes without dimensions; injected content; and web fonts. Something arrives late and takes space nobody reserved. For images, the fix is one line:

<img src="/hero.svg" alt="Linen shirt" width="1200" height="800">

<style>
  img { max-width: 100%; height: auto; }
</style>
  1. width and height give the proportion, 3:2 here. According to web.dev, the browser derives a default aspect ratio from them and reserves the height before the file arrives.
  2. max-width: 100% with height: auto keeps it responsive without losing that proportion. If you prefer CSS, aspect-ratio reserves the space the same way.

We measured it with an image that takes 800 ms to arrive. Without width and height, the copy dropped when the image landed and CLS was 0.1835 on mobile and 0.0862 on desktop; with them, 0. We cover it in more depth, along with load priority, in how to make your hero image load first.

For ads, embeds and iframes, web.dev recommends reserving the space with a min-height on the container and not collapsing it when no ad comes back, showing a placeholder instead, because removing reserved space causes as much CLS as inserting content. For web fonts, font-display: optional avoids a re-layout and size-adjust brings the fallback font's size closer to the web font's. We did not measure those two causes.

what moves the page and how to stop itcausefixCLS on mobilebanner on top, in the flowfixed at the bottom0.161 → 0slot sized for desktop onlyone height per breakpoint0.060 → 0script removes the slotdecide it on the server0.161animating bottom on entryanimate transform0.014 → 0image without width/heightwidth and height on the image0.184 → 0ad or iframe with no heightmin-height on its containernot measuredweb font with other metricsfont-display + size-adjustnot measuredevery cause has a fix that moves nothingmeasured locally on a test page at 390×844; not measured = per web.dev

Scroll the figure sideways to see all of it

Measure before and after

There are two kinds of measurement, and you want both. Lab tools (DevTools, Lighthouse, a test page) give you a number right away, but web.dev warns that they generally only see shifts during load, so what happens on scroll or minutes later slips past them. Field data from real users is what counts for Core Web Vitals.

All our numbers are lab numbers, measured locally on a test page. We built each variant as an in-memory page served through Playwright 1.62's request routing, with no server and no network. We loaded it in Chromium 151 at 390 × 844 with mobile emulation and at 1366 × 768, injected the script above before load, waited 2.5 s and read the CLS. All three runs of every variant returned the same value, and a second, independent session-window calculation matched each one:

VariantMobile (390 × 844)Desktop (1366 × 768)
Banner on top, in the flow0.16110.0332
Banner fixed to the bottom, transform entrance00
Banner fixed to the bottom, animating bottom0.01430.0031
Reserved slot, one height per breakpoint00
Reserved slot, desktop height only0.06010
Slot removed by a script0.16110.0350
Image without width and height0.18350.0862
Image with width and height00

On your store, measure the same way: open a product page in Incognito, record with Record and reload or paste the script, note the CLS with mobile emulation on, make the change and repeat. Then watch the field data. According to the CrUX documentation, PageSpeed Insights shows a 28-day average of real users that updates daily, so the fix takes weeks to show fully, and Search Console's Core Web Vitals report splits mobile and desktop and groups similar pages. If you want your own field data, the web-vitals library measures CLS in each visitor's browser.

Common mistakes

  • Measuring on desktop only. The same banner scored 0.0332 at 1366 × 768 and 0.1611 at 390 × 844.
  • Assuming the banner doesn't count because it is new. The new element doesn't count, but everything it pushes does: in our test, the header and the main content.
  • Reserving a single height. The desktop height left a 56 px shift on mobile.
  • Removing the slot with JavaScript. If the server doesn't know whether consent exists, the script that removes the slot causes the same shift you were trying to avoid.
  • Animating the entrance with top, bottom or margins. Use transform and respect prefers-reduced-motion.
  • Fixing the banner and forgetting focus. Without scroll-padding-bottom, the focused link can end up under the banner, which fails WCAG 2.2 criterion 2.4.11.
  • Dismissing the banner on a timer. A click shields the shift with hadRecentInput; an automatic dismissal that moves the page has no such shield.

Frequently asked questions

Does a cookie banner fixed to the bottom count as a layout shift?

No, as long as it doesn't move other elements. According to web.dev, adding a new element is not a layout shift if it doesn't change the start position of other visible elements. In our test, the fixed banner produced zero shifts on mobile and desktop.

Does closing the banner raise CLS?

Not when it closes on a click or tap: shifts within the next 500 ms carry hadRecentInput and are excluded. We measured it by removing the reserved slot after Accept: there was a shift, but CLS stayed at 0. An automatic dismissal, with no click, does count.

Why doesn't PageSpeed Insights show the shift I see?

Its lab section only measures the load, so a banner that appears later, for example after a timeout, can fall outside it. Its field section is a 28-day average of real users, so today's fix takes time to show. Confirm the change in DevTools and let the field data catch up.

Is a modal better than a bottom bar?

Both sit over the content and neither pushes the page. web.dev's cookie notice guide calls smaller, partial-screen modals a good alternative and asks for care with large ones, because some visitors leave instead of accepting. If you use a modal, focus has to move into it and stay there while it is open, as we explain in how to build an accessible modal with dialog.

Checklist

  • The banner is out of the flow: position: fixed at the bottom, or inside a slot the server prints with the right height at every breakpoint.
  • Its entrance uses transform and doesn't run under prefers-reduced-motion: reduce.
  • scroll-padding-bottom and padding-bottom use its real height, and focus never ends up underneath it.
  • It covers no more than half the screen and doesn't hide the sticky buy bar on mobile.
  • Text contrast is at least 4.5:1, focus is visible, and Accept and Reject are the same size.
  • Every image has width and height; ads and iframes get a min-height.
  • You measured CLS in Incognito, with mobile emulation and on desktop, before and after the change.
  • You check the field data in PageSpeed Insights or Search Console over the following weeks.

With the banner out of the flow, the page stops jumping on phones, where it shows the most. Once that's done, move on to your hero image's LCP and to the slow Add to cart button and INP: with all three, you've covered the Core Web Vitals.

Saw this on Instagram? Comment CLS on the Reel and I'll send you the guide.

Written by

Luis Morales

Luis Morales

Frontend and Shopify developer, EcomLabs

@luismoralesdev on Instagram

Published November 11, 2026

← Blog