Cumulative Layout Shift, The Layout Instability Metric
Tags: webperf, performance, webdev
Published: 2020-09-21 15:11:04

---
title: "Cumulative Layout Shift, The Layout Instability Metric"
published: true
description: Cumulative Layout Shift (CLS) is one of Google’s new Core Web Vitals metrics, dedicated to layout instability. How to measure CLS, optimize for it, visualize it in Chrome and what’s are its limitations? Let’s find answers.
tags: webperf, performance, webdev
canonical_url: https://blog.dareboost.com/en/2020/09/cumulative-layout-shift-visual-instability/
cover_image: https://dev-to-uploads.s3.amazonaws.com/i/j2f4bu810qinchagtss8.jpg
---

<p><em>Have you ever started reading an exciting news article but then lose your line because all the text shifted downwards? This happens to me a lot, mostly because of ads loading around the content I’m reading.</em><br> <em>This kind of user experience can be frustrating, but until now we’ve had trouble measuring it quantitatively. Cumulative Layout Shift addresses this challenge.</em></p> <!-- more --> <p>When you see a web page, you automatically break it down into visual elements. You perceive their relationship to each other according to their arrangement on the page. This is how you can identify where the relevant content is located. If this content moves by itself, or if another element is injected into the layout and moves the others, then you may lose sight of what you are focusing on.</p>

{% youtube s-QqF7pkMvE %}

<p><em>LeMonde.fr Home page load. Inserted and moving contents are identified by light blue areas. The contents of about 2/3 of the screen moves during the page load.</em></p> <p>And it’s even worse if you thought the layout was stable, and you were about to interact with the page. For example by clicking on a button (image on the left). If other content appears right before the click and pushes the other content downward (image on the right) you might interact with the wrong element of the page.</p> <figure> <img src="https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_320/https://boris.schapira.dev/assets/images/web/2020-09-15/1.png" srcset="https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_320/https://boris.schapira.dev/assets/images/web/2020-09-15/1.png 320w, https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_640/https://boris.schapira.dev/assets/images/web/2020-09-15/1.png 640w, https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_960/https://boris.schapira.dev/assets/images/web/2020-09-15/1.png 960w, https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_960/https://boris.schapira.dev/assets/images/web/2020-09-15/1.png 960w" sizes="80vw" alt="Two images of a user holding a phone in their hand. In the second image, a tap is implied." loading="lazy" width="960" height="720"> <figcaption><p>Within a product listing, the user is about to click on an image of a chair, when suddenly a text insert appears, shifting all the content downwards. The user unintentionally clicks on an image of a backpack.</p> </figcaption> </figure> <div class="inline-note"><p><strong>Overview</strong></p> <p>In this article, we will discuss the <a href="#layout-instability-api">Layout Instability API</a> and its use to <a href="#cls-compute">compute the Cumulative Layout Shift (CLS)</a>. We’ll see <a href="#cls-in-chrome">how to visualize CLS in Chrome</a> and <a href="#optimize-cls">how to optimize for it</a>. Finally, we will address some of its <a href="#wtf">unusual characteristics</a> and <a href="#conclusion">conclude</a>.</p> </div> <h2><span id="layout-instability-api">A Dedicated In-Browser API…</span></h2> <p>Viewing the elements that are shifted when loading and while using a page has been a long-standing need. A decade ago, you would need a custom-configured rendering engine (in the following example, Gecko, the Firefox rendering engine) to show every paint reflow.</p> <p><strong>Caution</strong></p> <p>This video contains lots of repaint. Don’t play if you are photosensitive or have vestibular disorders.</p>

{% youtube dndeRnzkJDU %} 

<p>In Chrome today, you can use the <a href="https://wicg.github.io/layout-instability/">Layout Instability API</a>. Its original intent was to find <a href="https://bugs.chromium.org/p/chromium/issues/detail?id=581518">more relevant signals than the <code>onload</code> event</a> to determine when a page is loaded. Layout stability, or “lack of shift” was one of them. As stated in the API abstract, it “provides web page authors with insights into the stability of their pages based on movements of the elements on the page”. To do that, the API monitors the position of the visual representation of every node that is visible in the viewport. If a node’s visual representation shifts from the previous frame by 3 or more pixel units in either the horizontal or vertical direction, the API notifies a layout shift and <strong>scores it</strong>.</p> <h2><span id="cls-compute">… Used To Penalize Jank</span></h2> <p>Let’s go back to our example:</p> <figure> <img src="https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_320/https://boris.schapira.dev/assets/images/web/2020-09-15/1.png" srcset="https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_320/https://boris.schapira.dev/assets/images/web/2020-09-15/1.png 320w, https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_640/https://boris.schapira.dev/assets/images/web/2020-09-15/1.png 640w, https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_960/https://boris.schapira.dev/assets/images/web/2020-09-15/1.png 960w, https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_960/https://boris.schapira.dev/assets/images/web/2020-09-15/1.png 960w" sizes="80vw" alt="Two images of a user holding a phone in their hand. In the second image, a tap is implied." loading="lazy" width="960" height="720"> <figcaption><p>Within a product listing, the user is about to click on an image of a chair, when suddenly a text insert appears, shifting all the content downwards. The user unintentionally clicks on an image of a backpack.</p> </figcaption> </figure> <p>How does the Layout Instability API score this? To compute the layout shift score, the algorithm first evaluates the area affected by the shift and multiplies it by the length of the shift.</p> 

{% youtube SgMZkHRHOB0 %}

<p>The area affected by the shift, or <strong>impact fraction</strong>, is the total area of the moved node, from its first position to its final position. Here, 78&nbsp;% +32&nbsp;% = 110&nbsp;%. As you can see, the impact fraction can be greater than100&nbsp;% (the viewport size).</p> <p>The length of the shift, or <strong>distance fraction</strong>, is measured according to the viewport’s largest dimension. So in this example, we’ll consider the height. The existing element moves by a height equivalent to 34&nbsp;% of the height of the viewport.</p> <p>In this example, and for this particular layout shift, <strong>the layout shift score</strong> is:</p> <p><em>layout shift score = 110&nbsp;% (impact fraction) × 34&nbsp;% (distance fraction) = <strong>0.374</strong></em></p> <p><a href="https://www.dareboost.com/en/doc/website-speed-test/metrics/cumulative-layout-shift-cls">The Cumulative Layout Shift (CLS)</a> metric is the sum of (nearly) all individual layout shift scores.</p> <div class="inline-note"><p><strong>The 500-millisecond window</strong></p> <p>To prevent websites from being penalized for transitions or animations that are the result of user interaction with the interface, the CLS algorithm does not record the layout shift for 500 ms after each active user interaction with the document (e.g. clicks, type, tap), or any event that directly changes the size of the viewport. We can consider that such shifts, resulting from an action, are expected by the user.</p> </div> <p>According to Google, to provide a good experience, sites should strive to have <strong>a CLS of less than 0.1</strong> for most (75%) users. This is bad news for the developers of the above example!</p> <p>Keep in mind that the impact fraction and distance fraction are highly dependent on the viewport’s characteristics. A change in your viewport’s dimensions or even orientation can change your layout –&nbsp;adapting responsively to the available space. Thus your impact fractions,distance fractions and, in the end, your Cumulative Layout Shift will change.</p> <h2><span id="cls-in-chrome">How To View Layout Shifts in Chrome</span></h2> <p>Layout shifts can be displayed in Chrome.</p> <ol> <li>In the DevTools, open the Command Menu with <kbd>Control</kbd> + <kbd>Shift</kbd> + <kbd>P</kbd> or <kbd>Command</kbd> + <kbd>Shift</kbd> + <kbd>P</kbd> (macOS)</li> <li>Start typing ‘Rendering’</li> <li>Run the “Show Rendering” command.</li> <li>Enable the <strong>Layout Shift Regions</strong> checkbox. As you interact with a page, layout shifts are highlighted blue.</li> </ol> <div class="inline-note"><p><strong>Warning</strong></p> <p>As stated above, this feature may cause flashing. If you are photosensitive or have vestibular disorders, use it with caution.</p> </div> <p>Since Chrome 84, you can also visualize Layout Shifts and get information about their attribution (even down to the shifted nodes) in the Performance tab, on the ‘Experience’ line.</p> <figure> <img src="https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_320/https://boris.schapira.dev/assets/images/web/2020-09-15/performance.png" srcset="https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_320/https://boris.schapira.dev/assets/images/web/2020-09-15/performance.png 320w, https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_640/https://boris.schapira.dev/assets/images/web/2020-09-15/performance.png 640w, https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_677/https://boris.schapira.dev/assets/images/web/2020-09-15/performance.png 677w" sizes="80vw" alt="A screenshot of the DevTools Performance tab, when zooming and selecting a layout shift." loading="lazy" width="677" height="756"> <figcaption><p>On a timeline, a succession of screens helps to understand the visual sequence. Below, several lines expose what happened at the same time, including the Experience line, that contains red inserts, one for each Layout Shift. At the very bottom, a description gives more precise information about the selected Layout Shift.</p> </figcaption> </figure> <h2><span id="optimize-cls">How To Optimize Your Cumulative Layout Shift</span></h2> <p>A page accumulates layouts shifts throughout its use. Each time new elements are visually inserted without the necessary space to display them being reserved by the browser. These insertions will often be images, but you also have to be careful with ads, web fonts, embedded content, and contents injected following an interaction that generate waiting times of more than 500&nbsp;ms.</p> <h3>Images</h3> <p>When it receives the HTML code for your page, the browser doesn’t know much about the images used in it. With no more information than the URL of an image, the browser does not know its size, so it cannot allocate the proper space for the image to display. This has become even more complex since the advent of Responsive Web Design. The display size of an image often depends on the context.</p> <p>To give as much information as possible to the browser so that it can do its job to the best of its ability, <strong>always set a width and a height attribute on your <code>&lt;img&gt;</code> tags (in pixels)</strong>. Even if these are not the actual display dimensions (which may depend on style rules), the browser will use them to infer the width / height ratio (called aspect-ratio) of the image, and thus calculate the height to be reserved based on the display width defined by the CSS.</p> <div class="inline-note"><p><strong>Did you know?</strong></p> <p>Edge, Firefox and Chrome have an internal mapping of aspect-ratio for images that defines both width and height attributes. An experiment is ongoing on Chrome to open the feature to developers.</p> <div class="language-css highlighter-rouge"><div class="highlight"><pre class="highlight"><code data-lang="css"><span class="nc">.el</span> <span class="p">{</span>
    <span class="py">aspect-ratio</span><span class="p">:</span> <span class="m">16</span> <span class="p">/</span> <span class="m">9</span><span class="p">;</span>
<span class="p">}</span>
<span class="c">/* internal rule for img, using the --webkit prefix (in Chrome)
img[width][height] {
  --webkit-aspect-ratio: attr(width) / attr(height);
}*/</span>
</code></pre></div></div> <p><a href="https://css-tricks.com/a-first-look-at-aspect-ratio/">Learn more about <code>aspect-ratio</code> with Chris Coyier, on CSS Tricks.</a></p> </div> <p>And of course, remember to <a href="https://blog.dareboost.com/en/2017/10/optimize-images-to-reduce-page-weight-file-formats-tools-and-rwd/#serve-the-right-file-size-regarding-to-the-context">serve the right file size regarding the context</a>. Use <code>srcset</code> to help the browser select the best image in your image set with the same aspect ratio (this depends on the rendering width). Use <code>picture</code> and <code>source</code> for different aspect-ratio or different image formats based on the context and browser support.</p> <h3>Ads, embed, iframes, and on-load dynamically created content</h3> <p>As with the images, the browser needs to allocate the appropriate space to render the final content. But you can’t give much info to the browser, as you have yourself little information about the future content. In the following I will only talk about ads, because they raise the most challenges. But the mechanics are the same for the other types of injected content.</p> <div class="inline-note"><p>In the following, we will discuss the impact of content injections on the main page template. It’s worth noting that if an iframe produces CLS within itself, it does not impact the main page.</p> </div> <p>More often than not, advertising space is allocated to a bidding service that injects a container into a dedicated node. Then, this space is ‘rented’ to the highest bidder, allocated to a network that then injects an advertisement. The space may be resized several times before the final ad is displayed, and in the case of timed ads, additional injections may occur multiple times during the users session.</p> <h4>How to prevent layout shifts from happening?</h4> <p>Explicitly define the space allocated to the ad container. If the ad is smaller than the allocated space, plan a background cover that will skin the available space. If the advertisement is larger than the reserved space, some of it will not be displayed.</p> <p>Sometimes no advertising fills the area. Plan for visuals of the correct size so that you don’t have to collapse the advertising space and provoke a layout shift. If your network provides you a way (through a JS callback, for example) to inject useful content in the absence of advertising, of course, do not hesitate.</p> <p>Another way to solve this problem is to position the ads in the negative space left by the content of the site, out of the page flow. If your ad has a fixed position in the page template, and that position does not interfere with the flow of the rest of the content, then inserting the ad does not cause any shift.</p> <figure> <img src="https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_320/https://boris.schapira.dev/assets/images/web/2020-09-15/negative_en.png" srcset="https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_320/https://boris.schapira.dev/assets/images/web/2020-09-15/negative_en.png 320w, https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_640/https://boris.schapira.dev/assets/images/web/2020-09-15/negative_en.png 640w, https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_960/https://boris.schapira.dev/assets/images/web/2020-09-15/negative_en.png 960w, https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_960/https://boris.schapira.dev/assets/images/web/2020-09-15/negative_en.png 960w" sizes="80vw" alt="A capture of a page from LeMonde.fr where a space is reserved for an advertisement in the margin. If the advertisement is larger than expected, it takes up more space in the margin, but does not shift the content." loading="lazy" width="960" height="540"> <figcaption><p>Using negative space to position ads.</p> </figcaption> </figure> <p>You can also move your advertising slots lower down on your pages. This will prevent the layout shift impact fraction from being too large, but this will likely reduce your revenue.</p> <h3>Web fonts</h3> <p>When a browser needs to display text with a web font, it identifies whether the font is needed and available (on the system or in the browser’s cache). If the font is necessary but unavailable, then the browser fetches it. During the fetching phase, the browser follows the <code>font-display</code> instructions (or lack of) to determine whether it should render the text with a fallback font or allocate the space with invisible text. When the font is available, or if the waiting time is too long, then the browser proceeds with the final rendering.</p> <p>In other words, whenever the browser has to fetch a font, it allocates the area with text using a fallback font, visible or not. And when the font is finally available, the browser renders the text area again using the web font.</p> <p>However, the temporary font does not necessarily have the same characteristics as the final one. Baselines, point sizes, kernings can differ and the final text may appear shorter or longer than the temporary text, causing layout shifts.</p> <div class="inline-note"><p>Every time I need to remember how font display management works, I go back to <a href="https://font-display.glitch.me/">the font-display playground</a>, by <a href="https://twitter.com/notwaldorf">Monica Dinculescu</a>.</p> </div> <p>To avoid these layout shifts, you can <a href="https://blog.dareboost.com/en/2020/05/optimize-third-parties-performance/">speed up font loading</a>. You can also choose web fonts with characteristics similar to system fonts, or consider the web font as optional the first time the page is loaded. It will then not be used on this page, even if downloaded and cached. Instead, it will be available on the next page. You can read more about this in ‘<a href="https://tech.ebayinc.com/engineering/ebays-font-loading-strategy/">eBay’s Font Loading Strategy</a>’ by <a href="https://twitter.com/senthil_hi">Senthil Padmanabhan</a>.</p> <h3>Content injected more than 500 ms after an interaction</h3> <p>The CLS algorithm ignores Layout Shifts for 500&nbsp;ms after each active user interaction with the document or change in viewport size. Thanks to this behavior, most user interactions do not cause layout-shifts that are recorded in the Cumulative Layout Shift.</p> <p>But you have to make sure your UI will react to the user interaction within the500&nbsp;ms window. If your UI relies on network requests or heavy javascript processing, shifts might occur after 500&nbsp;ms and will so be taken into account in the Cumulative Layout Shift value.</p> <p>To optimize your CLS:</p> <ul> <li>Make sure that your transitions and animations that follow user interaction last less than 500 ms.</li> <li>Measure and optimize your APIs response time.</li> <li>Anticipate the result of the network call(s) or long treatment by allocating the result area during the 500 ms, even if it is to be filled later on (you can use <a href="https://uxdesign.cc/the-optimistic-ui-with-react-f1420e317d54">an optimistic UI pattern</a>).</li> <li>Implement a specific UI paradigm for long processing and network call (queuing and completion notification system, modals etc)</li> <li>Only animate <a href="https://csstriggers.com/">CSS properties that do not trigger layout changes in Blink</a>.</li> </ul> <p>Of course, <strong>always test</strong>. While conducting some experiments to write this article, I discovered that <a href="https://bugs.chromium.org/p/chromium/issues/detail?id=1099895">layout shifts are created when the duration of a <code>translate</code> transition is 0</a> (this is fixed now). Edge cases like this will always exist. <a href="https://bugs.chromium.org/p/chromium/issues/list">Please report them to the Chromium Team</a>.</p> <div class="inline-note"><p><strong>Tip: two-step processing</strong></p> <p>Take the example of a “Load 20 more articles” button at the end of a page. If the processing (aka network call + rendering) takes less than 500&nbsp;ms, no problem. But if it takes longer than 500&nbsp;ms, the rendering may cause the footer to shift and increment your Cumulative Layout Shift.</p> <p>To avoid this, you can monitor the elapsed time and split the task in two parts if need be. Instead of rendering systematically after the network call, you can change the button label to “Show all 20 retrieved items”. This will force users to interact again with the page and grant you 500 additional milliseconds for rendering the content.</p> <p>Of course, do not hesitate to ask the users to check if the use of the functionality better meets their needs. Optimizing the CLS should not be at the expense of the UX.</p> </div> <h2><span id="wtf">Counter-intuitive behaviors</span></h2> <h3>Continuous accumulation in Single-Page Applications (SPAs)</h3> <p>While most performance indicators are related to the loading of the page, the CLS is focused on the whole experience of loading and interacting with a website.</p> <p>Many websites are structured on a page to page, URL to URL navigation paradigm. For them, the CLS is reinitialized with each navigation. Some sites, on the other hand, are built around a single page the content of which evolves through interactions, through JavaScript. These are called Single-Page Applications (SPAs).</p> <p>On SPAs (including applications that use progressive enhancement <a href="https://github.com/turbolinks/turbolinks">turbolinks</a>-like libraries), the Cumulative Layout Shift <strong>may never stop increasing</strong> during a user’s sessionsession, as the navigation between contents is not achieved by navigating between pages. The Cumulative Layout Shift will only be collected when the user leaves the page. If the Cumulative Layout Speed value you see in the Search Console (in the ‘<a href="https://blog.dareboost.com/en/2020/06/core-web-vitals-page-experience/">Core Web Vitals</a>’ section) seems high, maybe that’s an explanation.</p> <div class="inline-note"><p><strong>Note</strong></p> <p>This specific point about Cumulative Layout Shift is particularly important. As it means that the goal of SPA developers should not be to reduce the number of layout shifts, but to design interfaces that don’t produce any layout shift <strong>at all</strong>. If they don’t, the more popular their interfaces are, the longer the session durations will be, and therefore the higher their reported CLS will be.</p> </div> <h3>Scrollbars appearance penalty</h3> <p>This may seem counterintuitive to some, but horizontal and vertical scrollbars are part of the viewport. A great example of this is they’re included in the <a href="https://drafts.csswg.org/css-values-3/#viewport-relative-lengths">viewport-percentage length such as <code>vw</code> or <code>vh</code></a>. This implies that the appearance or disappearance of scrollbars does not modify the viewport size even though it changes the area available for content, and causes layout shift.</p> <p>Unfortunately, on some Operating Systems, like Windows, scrollbars are hidden until they’re needed, which can lead to a scrollbar missing from First Paint, then added later during the page load, and that may cause layout shifts.</p> <p>To avoid this behavior, you can add <code>html { overflow-y: scroll }</code> to enforce a scrollbar at First Paint, or wait for the implementation of CSS Overflow Module Level 4 and the new <code>scrollbar-gutter</code> property.</p> <div class="twitter-tweet twitter-tweet-rendered" style="display: flex; max-width: 550px; width: 100%; margin-top: 10px; margin-bottom: 10px;"><iframe id="twitter-widget-1" scrolling="no" frameborder="0" allowtransparency="true" allowfullscreen="true" class="" style="position: static; visibility: visible; width: 550px; height: 596px; display: block; flex-grow: 1;" title="Twitter Tweet" src="https://platform.twitter.com/embed/index.html?creatorScreenName=borisschapira&amp;dnt=true&amp;embedId=twitter-widget-1&amp;frame=false&amp;hideCard=false&amp;hideThread=false&amp;id=1239454788116533248&amp;lang=fr&amp;origin=https%3A%2F%2Fblog.dareboost.com%2Ffr%2F2020%2F09%2Fcumulative-layout-shift-stabilite-page%2F&amp;siteScreenName=borisschapira&amp;theme=light&amp;widgetsVersion=219d021%3A1598982042171&amp;width=550px" data-tweet-id="1239454788116533248"></iframe></div> <h3>Impact of the timing-function</h3> <p>In the following example, we compare two flex arrangements. In the first one, the central element doubles in size when hovered, using an “ease” timing-function. In the second example, a “linear” timing-function is used.</p>

{% youtube RN_bTHjllVE %}

<p>As you can see, the second example generates much less Cumulative Layout Shift. To explain this, we need to go back to the Layout Instability API spec, and more precisely, to what is called “<a href="https://wicg.github.io/layout-instability/#sec-unstable-nodes">Unstable nodes</a>”.</p> <blockquote> <p>A Node N has shifted in a coordinate space C if the starting point of N in C differs from the previous frame starting point of N in C by 3 or more pixel units in either the horizontal or vertical direction.</p> </blockquote> <p>This “three pixels between two frames” rule introduces a speed criterion in the instability measurement. All elements moving below this speed are not considered unstable, and these animations do not produce a layout-shift.</p> <p>Unfortunately, this means that to reduce CLS, some site owners might be tempted to use linear animations, which have a constant speed, rather than ones more intuitive to humans, such as “ease”, that speeds up at the beginning of the animation and slows down at the end.</p> <figure> <img src="https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_320/https://boris.schapira.dev/assets/images/web/2020-09-15/timings.png" srcset="https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_320/https://boris.schapira.dev/assets/images/web/2020-09-15/timings.png 320w, https://res.cloudinary.com/borisschapira/image/fetch/c_limit,f_auto,q_auto,w_558/https://boris.schapira.dev/assets/images/web/2020-09-15/timings.png 558w" sizes="80vw" alt="The linear and ease functions, represented by bezier curves." loading="lazy" width="558" height="351"> <figcaption><p>The linear function has a constant growth. The ease function accelerates until the middle of the time, then slows down.</p> </figcaption> </figure> <h3>Other strange behaviors of the metric</h3> <ul> <li>Animated elements that create layout shifts may continue to do so if they are not visible to the user (for example, even with a color that blends with the background). But if you use <code>visibility: hidden</code>, they won’t.</li> <li><a href="https://web.dev/cls/">The web.dev CLS page</a> recommends using <code>transform</code> and <code>translate</code> to create animations that do not produce layout-shift. However, <a href="https://bugs.chromium.org/p/chromium/issues/detail?id=1099895&amp;q=cls&amp;can=2">a bug in Chromium</a> seems to create layout-shifts when the transition time is 0 in Chrome, until version 86.</li> </ul> <h2><span id="conclusion">Conclusion</span></h2> <p>Cumulative Layout Shift is a metric that measures the layout instabilities of a web page. To optimize it, and <strong>improve the user experience</strong>, you should avoid injecting content for which the browser has not allocated the right amount of space. There are several techniques that exist for images, but this will, however, make advertising management much more complex.</p> <p>The way the CLS is calculated results in some unusual features. The current algorithm of CLS is probably not a good fit for Single Page Applications, as the attribution of results at page-level is not properly handled. Thus, <a href="https://blog.dareboost.com/en/2020/06/core-web-vitals-page-experience/">Core Web Vitals as a ranking signal</a> might be particularly challenging in such cases.</p> <p>Keep in mind that CLS is a new measure. We still lack feedback at scale. When in doubt, always focus on improving the User Experience instead of the metric itself. Some CLS optimizations may work against this goal and should be avoided.</p> <hr> <h3>Browser Support</h3> <p>Steve Kobes announced that Blink (the Chrome rendering engine) <a href="https://groups.google.com/a/chromium.org/forum/#!msg/blink-dev/jF1-M8KWAMU/ubGV4Fx2BgAJ">intended to implement</a> such an API in October 2018. Work on the draft Layout Instability API began in May 2019, and the API released with Chrome 77 in September 2019 and later in Edge (80+). Daniel Holbert created a ticket for Firefox in July 2020. We did not find any mention of the Layout Instability API in Safari’s public ticketing platform.</p> <h2>Resources</h2> <ul> <li><a href="https://tests.boris.schapira.dev/cls/">My own Cumulative Layout Shift experiments</a></li> <li>“<a href="https://wicg.github.io/layout-instability/">Layout Instability API specification</a>”, by Steve Kobes, Nicolás Peña Moreno &amp; Emily Hanley</li> <li>“<a href="https://web.dev/cls/">Cumulative Layout Shift</a>”, by <a href="https://twitter.com/philwalton">Philip Walton</a> and <a href="https://twitter.com/bibydigital">Milica Mihajlija</a></li> <li>“<a href="https://web.dev/optimize-cls/">Optimize Cumulative Layout Shift</a>”<br> and “<a href="https://addyosmani.com/blog/infinite-scroll-without-layout-shifts/">Infinite Scroll without Layout Shifts</a>”, by <a href="https://twitter.com/addyosmani">Addy Osmani</a></li> <li>“<a href="https://tamethebots.com/blog-n-bits/web-perf-iv-a-new-hope">Web Vitals - A New Hope in Web Performance Measurement</a>”, by <a href="https://twitter.com/davewsmart">Dave Smart</a></li> </ul> <hr> <p><em>Thanks to <a href="https://twitter.com/DamienJubeau">Damien Jubeau</a> and to <a href="https://twitter.com/TheRealNooshu">Matt Hobbs</a> for their contributions and for helping me improve my English!</em></p>