← Back to Frontend, page 1

frontend-0009 · Medium mid

A banner that loads after the page content pushes everything down. How do you measure the shift and stop it?

Layout Shift entries report each unexpected movement with a score. The fix is to reserve the banner space before it arrives, with a fixed height or aspect ratio, or to show it in a place where it cannot push content, such as an overlay.

Full answer

A layout shift happens when a visible element changes its start position between two frames without a recent user input. Each shift has a score: the fraction of the viewport affected, multiplied by the fraction of the distance moved. CLS reports the worst window of shifts, where a window ends after a 1 second gap or after 5 seconds in total.

Common causes:

  • Images or iframes without width and height, or without a CSS aspect ratio.
  • Banners, cookie notices and ads inserted above existing content.
  • Web fonts that swap with a different size.
  • Content added by JavaScript above what the user is reading.

To measure, observe layout-shift entries. Ignore entries with hadRecentInput, because a shift right after a click or tap is expected. Each entry lists the sources, which are the elements that moved. That tells you what to fix.

The fix depends on the cause:

  • Reserve space. Give the slot a min-height equal to the expected content, or an aspect-ratio.
  • Insert new content below the visible area or into an overlay that does not move other content.
  • Animate with transform, which does not change layout.

The page below inserts a 100 pixel banner above the main text after 100 milliseconds. The slot already has its 100 pixel height, so the banner fills space that exists and the score stays at zero. Remove the min-height rule and the text jumps down by 100 pixels, which gives a clearly positive score. The test reads the score for both versions.

Reserving too much space is also a bug, because it leaves an empty gap when no banner is shown. Choose the space from the real height for most users, and collapse the slot only when you know that no content will arrive.

html
<style>
  #slot { min-height: 100px; }        /* reserved space for the banner */
  #banner { height: 100px; background: #fc0; }
</style>
<div id="slot"></div>
<p id="text">Main article text that the reader is already looking at.</p>
<script>
  window.cls = 0;
  new PerformanceObserver((list) => {
    for (const e of list.getEntries()) if (!e.hadRecentInput) cls += e.value;
  }).observe({ type: 'layout-shift', buffered: true });
  setTimeout(() => {
    const b = document.createElement('div'); b.id = 'banner'; slot.append(b);
  }, 100);
</script>

Run in headless Chrome 156 through Playwright. Two full runs gave the same results. The fixed page scored 0. With the min-height rule removed, the same page scored about 0.03. Real network timing was not measured.

Key points

  • A shift is an unexpected move of a visible element without recent input.
  • Observe layout-shift entries and ignore those with hadRecentInput.
  • Entry sources name the elements that moved.
  • Reserve space with min-height or aspect-ratio, or insert content where it cannot push others.
  • Transform animations do not cause layout shifts.

Common mistakes

  • Counting shifts that follow a click, which are allowed.
  • Fixing the banner but leaving images without dimensions.
  • Using a placeholder height that is far from the real height, which still shifts.
  • Testing only on a fast connection where the late content arrives before first paint.

Follow-ups

  • How is CLS grouped into session windows?
  • How would you handle an ad slot whose height is not known in advance?
  • Why does a font swap shift text, and what reduces it?

Tips

  • Say that the cause is almost always content that arrives late and has no reserved space.
  • Mention the entry sources as the debugging tool.