CLS Layout Instability Sources

CLS debugging involves identifying which elements shift and determining what caused those unexpected movements. The goal is to connect each layout shift to a concrete source, such as late-loading content, images without reserved space, or dynamically inserted UI.

Cumulative Layout Shift (CLS) measures unexpected visual movement of page content during loading and interaction. A layout shift occurs when visible content changes position without a user-initiated action causing it. Debugging therefore starts by finding the shifted element and determining which DOM or rendering change moved it.

Common sources include images or media whose dimensions were not reserved, asynchronously injected content such as ads or banners, web fonts that change text metrics, and UI components that are inserted above existing content. A shift can also result from changing dimensions, margins, or other layout-affecting styles after the initial render.

Browser DevTools can help expose layout shifts through its Performance tooling, where layout-shift events can be inspected alongside the affected nodes and surrounding activity. The Layout Instability API also exposes `layout-shift` performance entries, which can be collected for field diagnostics when appropriate.

A useful debugging distinction is between the element that moved and the code or resource that caused it to move. For example, a paragraph may be the shifted element while a late-loading image above it is the actual cause. Fixes should target the cause by reserving space, stabilizing dimensions, or changing when content is inserted.

CLS is a page-level metric rather than simply a count of shifted elements. Individual layout-shift entries contribute weighted scores, and shifts caused by recent user input are treated differently from unexpected shifts. This means debugging should consider both the affected elements and the timing/context of each shift.

Key points

Interview questions

What is a layout shift, and what makes it unexpected?

A layout shift happens when an already-rendered element changes its position between rendered frames. It is considered unexpected when it is not the result of an intentional user interaction or another expected transition. Typical examples include content appearing above an existing paragraph or an image expanding after its resource loads. Debugging focuses on finding what changed the layout and why.

How would you debug a page with poor CLS?

I would first reproduce the problem and inspect the page with the browser Performance tooling. I would locate `layout-shift` events and identify the affected DOM nodes, then correlate their timing with image loads, font changes, DOM insertion, or style recalculation. I would distinguish the element that moved from the change that caused the movement. Finally, I would verify the fix with another measurement rather than relying only on visual inspection.

What are common causes of CLS and how do you prevent them?

Images and embedded content should have stable dimensions so the browser can reserve their space before the resource arrives. Dynamically inserted content should generally avoid pushing existing content unexpectedly, especially near the top of the viewport. Font loading can also change text metrics and cause reflow, so font strategy and fallback metrics matter. Animations that affect layout can also create instability and should instead use properties that do not require geometric repositioning when possible.

Why is the element that shifts not necessarily the root cause?

Layout is relational: one element can move because another element changes size or is inserted elsewhere in the flow. For example, a paragraph can move downward because an image above it gains height after loading. The paragraph is therefore an affected element, while the image is part of the causal chain. Effective debugging traces the shift back to the change that altered the layout rather than modifying every element that moved.

How would you distinguish a CLS problem caused by images from one caused by fonts?

For images, I would look for shifts occurring around image resource completion and check whether the rendered box had dimensions reserved before loading. For fonts, I would inspect shifts associated with font loading and compare fallback and final font metrics, including changes in line wrapping or element height. Performance traces can correlate these events with layout-shift entries. The fix differs: image issues usually require stable geometry, while font issues often require a more deliberate loading strategy or compatible fallback metrics.