How do you optimize Cumulative Layout Shift (CLS)?
guide // , 5 min read, by Darthwares
To optimize Cumulative Layout Shift (CLS), reserve space for dynamic content, set image dimensions, and use CSS transforms. Google Search Central recommends maintaining a CLS score of less than 0.1 for a good user experience. Core Web Vitals measure real-world loading, interactivity, and visual stability. You can review page performance in the Search Console Core Web Vitals report.
On this page // 6 sections
The short version
- Always specify width and height attributes or CSS aspect-ratio on images and video elements.
- Reserve space in the initial layout for late-loading elements like ads or dynamic widgets.
- Animate element positions using CSS transform: translate() rather than top, right, bottom, or left properties.
- Aim for a CLS score of 0.1 or less across at least 75% of page visits.
What is Cumulative Layout Shift and how is it measured?
Cumulative Layout Shift (CLS) measures visual stability during the entire lifecycle of a page. A layout shift occurs any time a visible element changes its position from one rendered frame to the next. web.dev documents that CLS measures the largest burst of layout shift scores for every unexpected layout shift on the page.
Unlike other Core Web Vitals measured in seconds or milliseconds, CLS is a unitless value based on how much content shifts and by how far. Developer feedback led web.dev to adopt a session window strategy that groups related layout shifts together intuitively. A session window begins with the first shift and expands until a gap with no shifts occurs. When this change was introduced, it capped the CLS of a page so no page score worsened, and 55% of origins saw no change in CLS at the 75th percentile.
Why does Google track Cumulative Layout Shift?
Google tracks CLS because visual instability damages real-world user experience. Google Search Central explains that Core Web Vitals are a set of metrics measuring loading performance, interactivity, and visual stability. Google highly recommends achieving good Core Web Vitals scores for success with Search and overall user experience.
web.dev guidelines state that sites should aim for a CLS score of 0.1 or less for at least 75% of page visits. Site owners can inspect how their pages perform by viewing the Core Web Vitals report in Search Console.
How do you fix common causes of high CLS?
To eliminate layout shifts, you must reserve appropriate space before content renders. web.dev recommends always including width and height size attributes on image and video elements, or reserving space using CSS aspect-ratio. Providing aspect ratio details allows the browser to calculate and reserve sufficient space for element height and area before downloading assets.
When adding late-loading content to the page flow, reserve layout space in the initial layout to prevent sudden shifts. web.dev also notes that making pages eligible for the back/forward cache (bfcache) is a highly effective way to keep CLS scores low. When moving elements around, avoid changing top, right, bottom, or left CSS properties, and use transform: translate() instead.
- Always specify width and height attributes on images and videos.
- Reserve initial layout space for late-loading dynamic content.
- Use transform: translate() instead of top, right, bottom, or left properties for element animations.
- Ensure pages are eligible for the back/forward cache.
How do you diagnose layout shifts on a page?
Diagnosing layout shifts begins with manual testing and browser API inspection. web.dev suggests taking 5 to 10 minutes to interact with your site to trigger unexpected layout shifts. Highlighting layout shift regions visually helps you quickly identify shift locations and timing.
MDN Web Docs explains that all shift debugging tools, including browser developer tools, rely on the Layout Instability API. The LayoutShift interface of the Performance API provides insights into element movements on the page. You can log API data to the browser console, send it to a server endpoint, or collect it in analytics. Inspecting the node property identifies the exact HTML element that moved.
- 01Spend 5 to 10 minutes manually interacting with the page to trigger potential layout shifts.
- 02Highlight layout shift regions in browser developer tools to visually spot visual movements.
- 03Use the Layout Instability API or browser console to inspect reported LayoutShift entries.
- 04Check the node property on reported entries to identify the exact HTML element causing the shift.
| Symptom | Cause | Recommended Fix |
|---|---|---|
| Images jump as they load | Missing dimensions on media elements | Add width and height attributes or CSS aspect-ratio |
| Text shifts down when ads load | Late-loading content placed in flow without reserved space | Reserve space for late-loading content in the initial layout |
| Elements shift during CSS transitions | Animating top, right, bottom, or left properties | Animate movements using transform: translate() |
How does user input affect layout shift calculations?
Layout shifts that occur shortly after user interaction are excluded from CLS scoring. Shifts happening within 500 milliseconds of user input have the hadRecentInput flag set by the browser.
web.dev explains that measurement scripts ignore layout shifts with this flag set. Because user actions such as clicks or taps create expected page changes, shifts within this 500-millisecond window do not count toward your total CLS metric.
Common questions
What is considered a good Cumulative Layout Shift score?
Google Search Central and web.dev specify that sites should aim for a CLS score of 0.1 or less for at least 75% of page visits to deliver a good user experience.
How does the Layout Instability API help debug shifts?
MDN Web Docs explains that the Layout Instability API powers browser dev tools and monitoring scripts. It reports LayoutShift entries, where the node property identifies the exact element that shifted.
Why should you use CSS transform instead of top or left for animations?
web.dev documents that changing top, right, bottom, or left properties causes layout shifts. Using transform: translate() moves elements on the screen without triggering unexpected layout shifts.
Do layout shifts caused by user clicks affect CLS?
Layout shifts occurring within 500 milliseconds of user input set the hadRecentInput flag. These shifts are excluded from CLS calculations because they follow direct user interaction.
Primary sources and further reading
This article was researched and written by an AI model and published automatically. It was written from sentences that were checked to appear on the pages below, but a person has not reviewed the text. If something is wrong, tell us at chibivikramathithan@darthwares.com and we will correct it.