Stockio

How to Fix Cumulative Layout Shift (CLS) in Containers

Find the containers behind layout shifts, reserve responsive media space, and verify fixes with browser traces and field data instead of guesswork.

By Ethan Clarke

Performance & SEO Specialist ·

On this page

A page can load quickly and still feel broken when a headline jumps, a card expands, or an advertisement pushes a button away from the pointer. Those movements often start in containers whose dimensions are unknown until a resource or script finishes loading.

Fixing cumulative layout shift requires more than setting a height on the element that moved. You need to identify what changed upstream, reserve the right space across breakpoints, and test the entire visit rather than only the first screenshot.

Establish a geometry budget

Start with expected dimensions for each unstable region: article media, embedded video, ads, product cards, and asynchronous results. Use the Aspect Ratio & Container Max-Width Tool below to calculate proportional heights from realistic container widths. The aspect ratio calculator helps replace guessed placeholder dimensions with a measurable layout contract.

For example, a 16:9 video at 960 pixels wide needs 540 pixels of height. At 352 pixels wide, it needs 198 pixels. A fixed 540-pixel placeholder at every breakpoint prevents one kind of shift while wasting most of a mobile screen. Responsive reservation is the goal, not arbitrary empty space.

Cumulative Layout Shift groups unexpected layout shifts into session windows, then reports the largest window score. Not every visible movement contributes equally, and certain shifts associated with recent user input are excluded. Use the CLS metric documentation to understand what the score measures before building acceptance criteria around it.

Aim for a CLS of 0.1 or less at the 75th percentile, evaluated separately for mobile and desktop. Treat that threshold as a baseline: a disruptive button jump can still deserve attention even when an aggregate score looks acceptable.

Trace the cause instead of the victim

Record a performance trace while reproducing the movement. Inspect layout-shift events and the elements marked as unstable. Those elements are often victims: a paragraph may move because a banner above it expanded, not because the paragraph's own CSS changed.

Reproduce delayed dependencies deliberately. Throttle the network, disable cache, postpone image responses, and wait for consent tools or third-party widgets. Then repeat common interactions, including opening filters, loading more results, and navigating through client-rendered routes.

Use this investigation sequence before editing styles:

  1. Identify when the first visible movement happens.
  2. Find the nearest ancestor whose dimensions changed.
  3. Determine which resource, style, or script triggered that change.
  4. Compare the reserved dimensions with the final rendered dimensions.
  5. Repeat at the viewport width where the issue is most severe.

Do not assume the largest image is always responsible. A late stylesheet can alter spacing across the page; a font swap can wrap a headline onto another line; a tiny notification inserted above the main content can move every section below it.

Capture a short trace before each fix and another afterward under the same conditions. Otherwise, a warm cache or a faster ad response may make an ineffective change appear successful.

Reserve media space with intrinsic dimensions

For ordinary images, supply intrinsic width and height that match the delivered asset, then use CSS to scale the image. The browser can derive its ratio before downloading the bytes. For deliberate crops, reserve the crop ratio in the wrapper and let the image fill it.

<figure class="story-media">
<img
  src="/media/workspace-1200.jpg"
  width="1200"
  height="800"
  alt="A workstation with a laptop and two reference displays"
>
</figure>
.story-media {
margin: 0;
width: 100%;
aspect-ratio: 3 / 2;
overflow: hidden;
}

.story-media img {
display: block;
width: 100%;
height: 100%;
object-fit: cover;
}

@media (max-width: 40rem) {
.story-media {
  aspect-ratio: 4 / 3;
}
}

This wrapper intentionally changes the visible crop on narrow screens. Review the focal point so important content is not cut off. The MDN aspect-ratio reference explains how preferred ratios interact with explicit sizes and automatic sizing.

Avoid conflicting constraints. A fixed height combined with a changing width may defeat the ratio you intended. Similarly, an image with a natural ratio different from an initially reserved ratio can cause movement if CSS does not preserve the chosen box.

Use the responsive image pipeline guide when a component receives multiple derivatives. Candidate files should preserve the expected composition unless the layout explicitly supports art direction with an appropriate reservation at each breakpoint.

Stabilize dynamic containers without hiding content

For async regions, choose a representative minimum height based on real content. A results panel can retain previous results during refresh rather than collapsing to a spinner. A skeleton should approximate the final row count and spacing, not occupy a random fixed rectangle.

Ads and embeds require a slot policy. Reserve the expected placement dimensions for each breakpoint, and agree on what happens when the provider returns a smaller creative or nothing at all. Immediately collapsing a large empty slot can create the very shift the placeholder was meant to prevent.

Do not solve unpredictable content by clipping it. Long translations, validation messages, and larger text settings need room to grow. Prefer inserting updates near their context or revealing details in response to deliberate interaction rather than injecting a new banner above everything after load.

Review font behavior alongside containers. A fallback font with significantly different metrics can change line breaks throughout a page. Font metric overrides and careful fallback choices can help, but they require visual testing with real headings and body text rather than a single specimen sentence.

Use transforms for decorative motion where appropriate. Animating height, margin, or top-level layout dimensions can move surrounding content. A transform may avoid layout changes, but it can still obscure controls or cause distracting motion, so it is not a blanket usability fix.

Verify the full visit and the field result

A local trace gives causal evidence; field measurements show whether the problem affects real visitors. Test cold and warm loads, multiple screen sizes, delayed third-party responses, and long sessions. Initial-load audits can miss shifts that happen after scrolling or route changes.

Return to the container sizing tool to check the exact width and height relationships at failing breakpoints. Record those relationships in component documentation so a future redesign does not replace a stable ratio with an unexplained magic number.

Use field attribution where your monitoring setup supports it, grouping affected routes and components without collecting private content. The web-vitals project provides a maintained implementation for measuring web performance metrics in production.

Finish with a regression checklist: media reserves space, fonts do not cause surprising wraps, refreshes preserve useful geometry, and third-party placements follow documented rules. Compare field percentiles after enough traffic arrives; do not declare a fix solely because one laboratory run reached zero.

Key takeaways

  • Diagnose upstream size changes, not only the elements highlighted as moving.
  • Reserve responsive geometry using intrinsic dimensions and deliberate aspect ratios.
  • Give ads, embeds, and async results explicit container policies.
  • Preserve text flexibility instead of hiding overflow to improve a score.
  • Verify both reproducible browser traces and real-user performance distributions.
Featured sponsor

Reach designers & developers at work

Put your product next to the tools people use every day. One sponsor per placement, no tracking pixels.

Become a sponsor

Keep reading

Related articles

More in Performance & SEO
UI Design

10 Best Figma Plugins for UI Designers

Make everyday Figma work smoother with ten useful plugins for content, icons, colour, accessibility, and handoff, plus sensible advice on checking permissions and keeping files tidy.

Includes: Aspect ratioMaya LindqvistMaya Lindqvist
Motion & Animation

8 Best Prototyping Tools for Interactive Designs

Compare eight interactive prototyping tools for interface flows, animated websites and detailed device behaviour, with practical strengths and limitations to guide your choice.

Includes: Aspect ratioAiko TanakaAiko Tanaka
AI for Design & UX

10 Best AI Presentation Makers

Explore ten AI presentation makers for turning an outline into editable slides, with honest notes on design control, collaboration, export, and choosing a tool for your audience.

Includes: Aspect ratioPiotr KowalskiPiotr Kowalski
Laptops & PCs

8 Best Laptops for College Students

Find a college laptop that fits your classes, backpack, and budget, with practical picks for writing papers, engineering software, creative projects, and everyday study.

Includes: Aspect ratioSofia MarchettiSofia Marchetti
Laptops & PCs

7 Best Laptop Accessories for Working From Home

Build a more comfortable home laptop setup with seven useful accessory picks, plus practical checks for monitor compatibility, USB-C power, desk space, and backups.

Includes: Aspect ratioMarcus BellMarcus Bell
HTML & CSS

Responsive Design Testing Tools and Emulators

Build a risk-based responsive test matrix, combine emulators with real devices, and catch layout, input, and viewport failures before release day.

Includes: Aspect ratioMarcus BellMarcus Bell