Cumulative Layout Shift (CLS) is a metric that measures the visual stability of a webpage during the loading process. It quantifies how much the elements on a page shift and move around, causing a poor user experience. In this article, we will explore the concept of CLS, its impact on user experience, and how to mitigate it.
Hatched by Periklis Papanikolaou
Aug 07, 2023
3 min read
8 views
Cumulative Layout Shift (CLS) is a metric that measures the visual stability of a webpage during the loading process. It quantifies how much the elements on a page shift and move around, causing a poor user experience. In this article, we will explore the concept of CLS, its impact on user experience, and how to mitigate it.
First, let's understand why CLS is important. When a user visits a webpage, they expect the content to be stable and easily accessible. However, if the layout keeps shifting while the page is loading, it can lead to frustration and confusion. Users might accidentally click on the wrong element or lose their place on the page, resulting in a negative experience.
Now that we understand the significance of CLS, let's delve into some of the factors that contribute to it. One common culprit is images and videos that don't have specified dimensions. When these elements load, they can cause the layout to shift as their dimensions become known. By specifying the dimensions in the HTML, the browser can reserve the necessary space, preventing layout shifts.
Another factor that can cause CLS is the loading of web fonts. When a web font is not immediately available, the browser might use a fallback font until the desired font is downloaded. This can lead to a sudden shift in the text, affecting the overall layout. To mitigate this, it is recommended to use font-display: swap in the CSS, which ensures that the fallback font is visible until the desired font is ready.
Third-party scripts and iframes can also contribute to CLS. When these resources load, they often introduce new elements into the page, causing shifts in the layout. To minimize this impact, it is advisable to load third-party scripts asynchronously and set the dimensions of iframes in advance. This way, the browser can allocate the necessary space and avoid layout shifts.
Now that we have discussed the factors contributing to CLS, let's explore some actionable advice to mitigate it:
-
Optimize image and video loading: Specify dimensions for all media elements in the HTML. This allows the browser to reserve the space and prevent layout shifts.
-
Use font-display: swap: Ensure that the text remains visible even when web fonts are loading. This prevents sudden shifts in the layout and maintains a better user experience.
-
Load third-party scripts asynchronously and set iframe dimensions in advance: By loading third-party scripts asynchronously and specifying iframe dimensions, you can minimize layout shifts caused by these resources.
In conclusion, Cumulative Layout Shift is a crucial metric to consider when optimizing the user experience of a webpage. By addressing factors such as image and video loading, font display, and third-party scripts, we can mitigate layout shifts and provide a more stable and enjoyable experience for users. Remember to optimize images and videos, use font-display: swap, and load third-party scripts asynchronously to minimize CLS.
Sources
Hatch New Ideas with Glasp AI 🐣
Glasp AI allows you to hatch new ideas based on your curated content. Let's curate and create with Glasp AI :)
Start Hatching 🐣