Skip to content
Tyrone Software
Web Development

Where layout shift really comes from in production pages

Abstract illustration for the article Where layout shift really comes from in production pages

Lighthouse gives the page a clean score, yet field data still reports a poor Cumulative Layout Shift. The lab run loads the page once, on a fast connection, with an empty cache and no consent banner. Real visitors get all the things that move the layout.

The usual suspects, in the order they tend to appear

  • Images and iframes without dimensions. Set width and height, or aspect-ratio in CSS, so the browser can reserve space before the file arrives.
  • A cookie or consent banner that pushes content down instead of overlaying it.
  • A web font that swaps in with different metrics than the fallback. CSS descriptors like size-adjust, ascent-override and descent-override let a fallback font match the web font's proportions, and there are tools that calculate the numbers for a given font.
  • Content injected above existing content after load: ads, related-article widgets, "we use cookies" strips, promo bars.
  • Animations that change top, height or margin. Animate transform instead, since it does not affect layout.

Log it where it happens

Shifts caused by user input do not count, so a script can ignore those and print the elements involved:

JavaScript snippet that logs layout shift entries with the affected nodes
A PerformanceObserver that skips input-driven shifts and prints the elements that moved.

Run it in the field on a sample of traffic, or at least in DevTools with the network throttled and the cache disabled. The sources list usually points straight at the offending node.

One detail worth knowing

CLS is not the sum of every shift on the page's life. It is the worst burst of shifts, where a burst is a run of shifts with gaps of less than a second, capped at five seconds. A page can be open for an hour and still be judged on a single bad moment, which makes the first few seconds after load the place to look.

Third-party scripts you cannot fully control

Ad slots, share-count widgets and A/B testing snippets often reserve no space until their own JavaScript runs, because the publisher only knows the final creative size after an auction or an experiment assignment. A min-height on the container, sized to the most common creative, prevents the worst jumps even when you cannot control the script itself. Where the tool supports it (many ad networks and most A/B testing platforms do), ask for a synchronous or reserved-slot mode instead of the default asynchronous insert.

A/B testing tools deserve a specific mention: a client-side test that hides content and swaps it in after a script runs is a common, self-inflicted source of layout shift, on top of the flash-of-original-content problem it already has. Where possible, decide the variant on the server and render the final markup once.

One more limit worth knowing: as of this writing, the Layout Instability API that CLS is built on is implemented in Chromium-based browsers, not in Firefox or Safari. Field tools built on Google's Chrome User Experience Report inherit that limitation, so a CLS number from real-user data describes Chromium users specifically, not your whole audience. Check your analytics provider's documentation for how it labels this.

Keep reading

Technologies

  • Python
  • PHP
  • Laravel
  • Django
  • PostgreSQL
  • MySQL
  • HTML5
  • CSS3
  • Bootstrap