Understanding Cumulative Layout Shift (CLS): A Comprehensive Guide

12 Jun 2024 | 8 min read
Understanding Cumulative Layout Shift (CLS): A Comprehensive Guide

What is Cumulative Layout Shift (CLS)?

Cumulative Layout Shift (CLS) is a metric that measures how much a webpage’s content moves around while it loads. This movement, called “layout shift,” can happen when elements like images, ads, or fonts load at different times. When these elements load and shift the position of other content on the page, it cannot be enjoyable for users.

Imagine you are reading an article online. Suddenly, an ad pops up, causing the text to jump, and you lose your place. This is an example of a layout shift. CLS helps us measure how often and how much these shifts happen on a webpage.

A good CLS score means the page is stable and doesn’t have many shifts. Google recommends a CLS score of 0.1 or less for a good user experience. A higher score means the page has more unexpected movements, which can frustrate visitors.

Here’s how CLS works:

  • Every time content moves, it adds to the CLS score.
  • More prominent movements have a higher impact on the score.
  • More frequent movements also increase the score.

Keeping the CLS score low ensures your webpage is smooth and stable, making it easier for people to read and interact with your content.

How CLS is Measured?

Cumulative Layout Shift (CLS) is measured by how many visible elements on a webpage move around while the page is loading. Here’s a simple way to understand how CLS is calculated:

Start with Layout Shift Events: 

When a webpage loads, any visible element, such as text, images, or buttons, can move. Each time an element moves, it creates a layout shift event.

Measure Impact Fraction: 

We measure the “impact fraction for each layout shift event.” This is the area of the screen that is affected by the movement. For example, if an image moves down and takes up half the screen, the impact fraction is 0.5.

Measure Distance Fraction: 

Next, we measure the “distance fraction.” This is how far the element has moved relative to the screen size. If the image moves down by 25% of the screen height, the distance fraction is 0.25.

Calculate Layout Shift Score: 

We multiply the impact fraction by the distance fraction to get the layout shift score for each event. For example, if the impact fraction is 0.5 and the distance fraction is 0.25, the layout shift score for that event is 0.125 (0.5 * 0.25).

Sum Up the Scores: 

The CLS score is the sum of all layout shift scores for a webpage. If multiple shifts exist, we add their scores to get the total CLS score.

Here’s a quick example:

  • An image moves down and affects 50% of the screen (impact fraction = 0.5).
  • The image moves down by 25% of the screen height (distance fraction = 0.25).
  • The layout shift score is 0.125 (0.5 * 0.25).
  • If another element moves and has a score of 0.05, the total CLS score is 0.175 (0.125 + 0.05).

A good CLS score is 0.1 or less. Keeping your CLS score low ensures your webpage is stable and user-friendly.

Common Causes of High CLS

High Cumulative Layout Shift (CLS) scores happen when elements on your webpage move around unexpectedly while the page is loading. Here are some common causes of high CLS:

Images Without Dimensions: 

When you add photos to your page without specifying their width and height, the browser must know how much space to reserve. As a result, the layout can shift when the images finally load.

Ads, Embeds, and Iframes Without Dimensions: 

Similar to images, if you don’t set dimensions for ads, embedded content, or iframes, they can cause sudden layout shifts when they load. This is especially true for ads, which load slower than other elements.

Dynamically Injected Content: 

Adding new content to the page via JavaScript, such as pop-ups, banners, or new sections, can push other content around. If this content is added above existing content, it can create a significant shift.

Web Fonts Causing FOIT/FOUT: 

When web fonts load, text can appear differently than when the page initially loads. This can lead to a Flash of Invisible Text (FOIT) or a Flash of Unstyled Text (FOUT), which can cause layout shifts.

Slow Server Responses:

If your server is slow to respond, elements on your page may load at different times, causing page parts to move around as they load.

Content Loading Late: 

Sometimes, elements like images, ads, or iframes load slower than the rest of the page, causing layout shifts when they finally appear.

By understanding these common causes, you can take steps to reduce CLS and create a more stable and enjoyable user experience on your website.

Best Practices to Improve CLS

Improving Cumulative Layout Shift (CLS) is essential for a smooth and stable webpage. Here are some best practices to help you achieve a low CLS score:

Include Size Attributes for Images and Videos: 

Always set the width and height for your images and videos. This helps the browser allocate the right amount of space for these elements, preventing unexpected shifts when they load.

Reserve Space for Ads and Embeds: 

Allocate space for ads, embeds, and iframes before they load. Use CSS to define the height and width of these elements. This prevents them from pushing other content around when they appear.

Avoid Inserting Content Above Existing Content:

When adding new content to your page, place it below existing content whenever possible. This way, you avoid pushing the content down and causing layout shifts.

Use Transform Animations: 

For animations and transitions, use CSS properties like transform instead of properties that cause reflows, like width or height. Transform animations don’t affect the layout and are smoother.

Load Web Fonts Efficiently: 

Use font-display: swap in your CSS. This ensures that text is displayed with a fallback 

font while the custom font loads, reducing the risk of layout shifts caused by font changes.

Preload Important Resources: 

Preload essential resources like fonts and images. This ensures they load quickly and reduces the chance of layout shifts caused by slow-loading elements.

Use tools like Google PageSpeed Insights and Lighthouse to check your CLS score regularly. Make adjustments as needed to keep your score low.

Following these best practices ensures that your webpage remains stable and provides a better user experience.

Tools to Measure and Analyze CLS

To ensure your webpage has a low Cumulative Layout Shift (CLS) score, you need the right tools to measure and analyze it. Here are some valuable tools to help you:

Google PageSpeed Insights:

This tool analyzes your webpage and provides a detailed report on its performance, including the CLS score. It also suggests ways to improve your page’s stability.

Lighthouse in Chrome DevTools: 

Lighthouse is a powerful tool built into Chrome DevTools. It provides an in-depth audit of your webpage, including CLS. To use it, open Chrome DevTools, go to the “Lighthouse” tab and run an audit.

Web Vitals Chrome Extension: 

This browser extension gives real-time feedback on your web page’s Core Web Vitals, including CLS. It’s a handy tool for quick checks and immediate insights while you browse.

Google Search Console:

The Core Web Vitals report in Google Search Console shows you how your website performs based on user data. It highlights pages with high CLS and provides insights on where to focus your improvements.

WebPageTest: 

This tool allows you to run detailed performance tests on your website. It provides a breakdown of CLS and other metrics, helping you understand and fix issues causing layout shifts.

GTmetrix: 

GTmetrix analyzes your webpage and offers a comprehensive report, including the CLS score. It also provides actionable recommendations to improve your page’s performance and stability.

Using these tools, you can regularly monitor and analyze your webpage’s CLS score, ensuring your site remains user-friendly and stable.

Step-by-Step Guide to Fixing CLS Issues

Fixing Cumulative Layout Shift (CLS) issues is essential for a stable and user-friendly webpage. Here’s a simple step-by-step guide to help you:

Identify CLS Issues

Use tools like Google PageSpeed Insights, Lighthouse, or the Web Vitals Chrome Extension to check your CLS score. Look for specific elements causing layout shifts in the reports provided by these tools.

Set Size Attributes for Images and Videos

Ensure all images and videos have defined width and height attributes in your HTML or CSS. This helps the browser reserve suitable space while the elements load.

Reserve Space for Ads and Embeds

Using CSS, define the dimensions for ads, iframes, and other embedded content. Allocate enough space to prevent these elements from pushing other content around when they load.

Avoid Content Insertion Above Existing Content

When adding new content, place it below the existing content whenever possible. This prevents pushing down the content that users are already viewing.

Use Transform for Animations

Use CSS transform properties for animations instead of properties like width, height, or margin. Transform animations do not cause reflows and help keep the layout stable.

Optimize Web Fonts

Use the font-display swap property in your CSS. This ensures the text is visible with a fallback font until the custom font loads. This prevents layout shifts caused by font loading.

Preload Key Resources

Preload necessary resources like fonts and large images to ensure they load quickly. Use the <link rel= “preload”> tag in your HTML.

Monitor and Test Regularly

Check your CLS score regularly using the tools mentioned earlier. Test your site after making changes to ensure the improvements are adequate.

Fix Issues Highlighted by Tools

Follow the specific recommendations provided by tools like Google PageSpeed Insights and Lighthouse. Address each issue methodically to reduce your CLS score.

Following these steps can reduce your CLS score, ensuring your webpage remains stable and user-friendly.

The Impact of CLS on SEO and Conversions

Cumulative Layout Shift (CLS) affects both SEO and conversions on your website. Here’s how:

User Experience

High CLS means the page shifts unexpectedly, which frustrates users. Frustrated users are likelier to leave your site quickly, leading to a higher bounce rate.

SEO Rankings

Google considers user experience to be a ranking factor. A good CLS score helps improve your website’s overall performance in Google’s Core Web Vitals. Better scores can increase search engine rankings, making your site more visible to potential visitors.

Conversions

Stable pages provide a better user experience, making visitors more likely to stay and interact with your content. Fewer layout shifts mean users can easily click buttons and links, reducing the chance of misclicking. A pleasant browsing experience increases the chances of visitors converting into customers.

Trust and Credibility

A stable and well-performing website builds trust with your visitors. Users are more likely to return to a site that offers a smooth and reliable experience.

Maintaining a low CLS score enhances your site’s user experience, positively impacting 

SEO and conversion rates. A stable site ranks better and keeps visitors happy, encouraging them to stay longer and engage more with your content.

Join Our Community:
Subscribe for Updates

Recent Blogs

View All Blogs