Mastering Cumulative Layout Shift: Boosting User Experiences
Hey there, fellow internet adventurers!
Have you ever found yourself casually browsing a website, when suddenly, out of nowhere, the page layout jumps around like a hyperactive squirrel on a sugar rush?
Well, you’re not the only one! We’ve all been victims of the dreaded Cumulative Layout Shift (CLS) on websites. You know, that annoying moment when you’re about to click on a button, and suddenly it jumps out of reach, leaving you helplessly clicking on an ad instead.
Fear no more, readers! In this article, we’re diving headfirst into the wild world of CLS, exploring what it is, why it happens, and most importantly, how to improve this problem for a better browsing experience.
So, grab your favorite beverage, get comfy, and let’s unravel the mysteries of CLS together!
Table of Contents
- What is cumulative layout shift?
- What causes cumulative layout shift?
- Why is cumulative layout shift important?
- How is cumulative layout shift measured?
- What is a good cumulative layout shift score?
- 6 tips to improve cumulative layout shift
What is cumulative layout shift?
Cumulative Layout Shift (CLS) is a metric used to measure the visual stability of a web page during its loading process. In particular, it quantifies the number of unexpected layout shifts that occur as the page loads and is an essential factor in determining the overall user experience.
Also, web page loading can cause dynamic content elements like images, videos, and ads to change, causing unexpected content shifts and changes in the page’s layout. CLS aims to capture these sudden changes in layout and quantify their impact on the user experience.
As a result, CLS is a critical metric for web performance and user experience optimization. It helps ensure that web pages load smoothly and avoid frustrating and disruptive layout shifts, ultimately leading to a more pleasant browsing experience for users.
What causes cumulative layout shift?
1. Images and media without dimensions
Generally, images and media without dimensions are like the troublemakers of the web because they ruin our surfing experience and leave us confused.
Without explicit dimensions in HTML code, an image or media element can cause an unstable layout and a CLS when the actual dimensions load. So, the browser is unaware of the intended size and renders the page with temporary dimensions.
Mischievous website elements can be attributed to laziness, oversight, or a combination. Therefore, developers may forget to specify dimensions or rely on external sources or dynamic content, which makes it difficult to accurately predict dimensions.
2. Ads, embeds, and iframes
Ads, such as banner ads, pop-ups, and video ads, can cause layout instability by causing content to be pushed aside. These ads can impact user experience and load late, disrupting the page’s flow, and also cause voids to fill with other content, affecting user experience. Additionally, embeds and iframes add functionality and interactivity to web pages and can cause layout stability issues. However, they can cause content to rearrange, which makes it challenging for users to consume the website content consistently.
3. Web fonts and asynchronous loading
Apparently, web fonts and asynchronous loading are mischievous in causing CLS. Though web fonts enhance website design, they can also negatively impact the browsing experience, adding flair and personality.
When a webpage loads, it typically starts by rendering the content in a default font, then fetches desired web font files, and restyles the text with the chosen font, but the browser doesn’t know the size and shape until the files arrive.
As a result, this uncertainty creates CLS, where the text takes up less space and expands when web font is applied, which leads directly to layout shifts and causes buttons to slip out of reach.
4. Dynamic content and late-loaded elements
Dynamic content, including images, videos, advertisements, and user-generated content, enhances website engagement and relevance but can also disrupt the layout if not appropriately handled.
Meanwhile, late-loaded elements, such as images or advertisements, are fetched after page rendering and can disrupt the layout and cause sudden changes. They can disrupt the main content and cause a disruption.
The issue arises when late-loaded elements’ space is not properly accounted for during the initial layout calculation, causing a default layout that doesn’t match the final one. So, this results in a frustrating and visually jarring experience for users.
5. Responsive design and media queries
Don’t get us wrong, responsive design is a hero in its own right. It’s what makes websites adapt beautifully to different screen sizes, ensuring an optimal user experience across devices. But sometimes, even our beloved heroes can have unintended consequences.
Here’s how it works: Responsive design uses media queries to detect the screen size of a device and apply specific styles accordingly. These media queries act as the Sherlock Holmes of the web, sniffing out the dimensions and characteristics of the user’s device.
Media queries can cause content shifts when they cause layout changes, which creates content to shift. For example, on a mobile device, the text may jump to a different position due to the query adjusting the layout to fit the screen size.
hile the intentions are good, this unexpected layout shift can be infuriating. It disrupts your reading flow, leads to accidental taps on wrong elements, and often leaves you wondering if you accidentally fell into a virtual funhouse. ###
Sounds awesome, right? Well, not always.
To address cumulative layout shifts and improve the user experience, web developers can take several steps, which we will discuss below. And by following best practices and guidelines, developers can minimize cumulative layout shifts and create more user-friendly web pages.
Why is cumulative layout shift important?
CLS is definitely an essential metric in web development, measuring visual stability and user experience. In addition, it quantifies page layout shifts during loading, helping developers identify and fix issues that can negatively impact user experience and satisfaction.
There are several reasons why cumulative layout shift is essential:
Content accessibility. Layout shifts impact users with disabilities and assistive technologies. Therefore, optimizing CLS can create an inclusive, accessible web experience for all users.
Search Engine Optimization (SEO). Google prioritizes user experience in search rankings. Websites with poor user experience may be penalized in search results, while optimizing CLS can improve performance, potentially boosting organic traffic.
Ad performance. Online advertising layout changes can affect the effectiveness, causing accidental clicks or frustration. So, minimizing CLS ensures consistent display and intentional interaction, potentially improving performance and revenue.
Conversion rates and revenue. Positive user experience impacts conversion rates and online revenue. Hence, developers can create an environment that promotes user engagement and increases business goals by reducing CLS.
In conclusion, CLS is important because it impacts user experience, accessibility, search engine rankings, ad performance, and website revenue. By focusing on reducing CLS, developers can enhance user engagement and satisfaction.
How is cumulative layout shift measured?
CLS is measured using a formula that considers the impact and distance fractions for each layout shift on the page. Here’s how it works:
1. Impact fraction
Let’s understand this in a simple way! Impact fraction measures layout shifts’ impact on webpages, considering distance and area, revealing user experience disruption. Normally, smaller shifts have a higher impact fraction than larger ones.
Also, web performance tools like Google’s Web Vitals automatically measure and report the impact fraction for webpages by analyzing layout shifts during page load and generating a cumulative score, so you don’t have to worry about how to calculate it.
Therefore, web developers can use the impact fraction to identify disruptive website changes, optimize elements, adjust loading priorities, and use preloading and lazy loading techniques to reduce CLS and improve user experience.
2. Distance fraction
Distance fraction is a metric in CLS that quantifies layout shifts’ impact on browsing experience, considering element size and movement. It measures the proportion of viewport element shifts, calculated by dividing its distance by the viewport size, affecting screen real estate.
Consider an ad banner with a horizontal shift of 50 pixels and a width of 300 pixels. If the viewport is 1920 pixels wide, we calculate the distance fraction like this: 50 pixels (distance) divided by 1920 pixels (viewport width) equals approximately 0.026.
The resulting value of 0.026 is the distance fraction for this particular layout shift. By comparing this fraction to a predefined threshold (usually 0.1), web developers can identify if a shift exceeds an acceptable level or if further optimization is needed.
3. Cumulative layout shift
To calculate CLS, the impact fraction and distance fraction are summed up for all layout shifts that happen during the entire lifespan of the page load. This cumulative score provides a comprehensive measure of the overall visual stability of the page.
To obtain a meaningful CLS score, measuring layout shifts across different devices and network conditions is important. Multiple page loads are typically simulated, and the CLS scores are averaged to provide a representative value.
Web developers and performance analysts can access CLS data using browser tools or web performance monitoring tools, providing insights into layout shifts, their impact on elements, causes, and potential improvement solutions.
What is a good cumulative layout shift score?
Google suggests aiming for a CLS score of less than 0.1 for a good user experience, ensuring minimal layout shift during page load without significantly affecting element positions. A good CLS score depends on the website’s context and goals.
Additionally, a CLS score below 0.1 prevents sudden jumps and element movements, maintaining visual continuity and preventing usability issues, especially on mobile devices with slower connections or limited processing power.
Ultimately, a good cumulative layout shift score reflects the commitment to providing a smooth and visually stable user experience, reducing frustration, and enhancing the overall usability of a website.
6 tips to improve cumulative layout shift
1. Set dimensions for elements
One effective strategy to reduce CLS is to set dimensions for elements within the web page. This allows the browser to reserve space for them during initial rendering, ensuring accurate page layout calculations and reducing the likelihood of subsequent shifts.
Here are a few key considerations when setting dimensions for elements to improve CLS:
Ensure height and width attributes are specified in HTML for proper image allocation.
Set explicit dimensions for videos and iframes, including width, height, and CSS padding-bottom for dynamic content allocation.
Ensure container elements have predefined dimensions for dynamic content for a stable layout.
Ensure visual stability across devices using CSS media queries and layout grids by responsive design.
Remember that an effective approach to improving CLS involves optimizing all aspects of web pages, including asynchronous loading, third-party script optimization, and critical rendering path optimization, to ensure a smooth and visually stable user experience.
2. Preload key resources
Preloading key resources early in page rendering improves CLS and user experience by preventing abrupt visual shifts and ensuring correct processing and positioning.
Here are some key considerations when implementing preloading techniques:
Identify critical resources affecting web page layout through analysis and observing elements.
Strategically set HTML attributes by resource hints for efficient resource allocation.
Consider lazy loading to improve CLS by deferring non-critical resources’ loading until viewport entry, preventing layout shifts.
Test and optimize preloading strategies using tools like PageSpeed Insights for optimal website performance and user experience.
To prioritize the early loading of resources, HTML offers preloading attributes such as
rel="preload"for CSS and font files and
<script>tags in the HTML head section.
So, preloading techniques help website developers address CLS, enhancing usability, engagement, and user satisfaction, improving conversion rates and increasing user satisfaction.
3. Avoid inserting content dynamically
One way to improve CLS is to avoid inserting content dynamically, as this can cause unexpected layout changes and contribute to a higher CLS score.
To reduce this issue and improve CLS, it’s recommended to follow certain best practices:
Reserve dynamic content space in the layout to prevent sudden shifts. Preload or lazy-load content to ensure content is ready for display without sudden layout changes.
Optimize media layout by aspect ratios, reducing layout shifts and ensuring proper space allocation.
Prioritize critical content loading for optimal layout stability.
Regularly test and monitor your website for CLS.
In summary, implementing best practices and maintaining layout stability can improve visual consistency and user experience by avoiding dynamic content insertion, resulting in a consistent browsing experience.
4. Optimize web fonts
In general, web fonts enable designers and developers to create custom typography, enhancing visual presentation flexibility, but they can introduce layout shifts if not appropriately managed.
These are some tips to optimize web fonts and minimize CLS:
Preload web fonts with the HTML
preload to attribute fetches and prepare them in advance, prioritizing font loading and reducing layout shifts due to font loading delays.
swapby font display swap to keep text visible to users before the web font finishes loading.
- Optimize font files by using tools for speed and quality. M
inimize font variations by using separate font files or system fonts.
Improve performance with pre-installed system fonts.
Define font sizes and line heights to prevent layout shifts.
- Regularly test and monitor the website’s CLS score, font impact, and optimization effectiveness.
Implementing these optimization techniques can minimize font layout shifts, improve visual stability, and enhance user experience, boosting search engine rankings and satisfaction.
5. Lazy-load content below the fold
Here are some suggestions you should consider:
mIdentify below-the-fold resources causing layout shifts.
Insert lightweight placeholders for resources before lazy-loading, providing visual cues and predefined dimensions.
Implement lazy loading to detect visible placeholders, swap with content, and initiate the loading.
Address lazy-loaded content issues with alternative content, retrying, and fallback options.
In conclusion, by applying lazy-loading techniques, you can improve your website performance by reducing CLS, enhance user experience, develop core web vitals, and boost search engine rankings and engagement.
6. Ensure responsive design
One effective strategy to improve CLS is by implementing responsive design techniques. The responsive design focuses on creating webpages that adapt and respond to different screen sizes and devices, providing a consistent and optimized user experience across platforms.
Here are some ways in which responsive design can help improve CLS:
The responsive design utilizes fluid grid systems for dynamic content adjustment.
The responsive design prioritizes elements, breaks down layouts, and minimizes CLS.
Media queries optimize the responsive design by targeting screen sizes and device characteristics.
Responsive design optimizes images and media using adaptive and lazy loading techniques.
Regular testing and responsive design optimization ensure compatibility across devices and screen sizes.
So, responsive design techniques ensure a consistent and stable user experience across devices and screen sizes, reducing CLS and enhancing user satisfaction and engagement. Additionally, this approach improves content loading speed and overall website experience.
In conclusion, cumulative layout shift (CLS) measures web page visual stability. Therefore, it is important for consistent user experience in a digital world where attention and competition resolution are required for website owners and developers.
Throughout this article, we have explored CLS causes, impacts, and strategies to reduce it, including optimizing images, using aspect ratios, using media dimensions, loading critical content before non-critical elements, etc.
By implementing these techniques and adopting a proactive approach to web design and development, website owners can reduce CLS, improve user experience, and increase search engine rankings, organic traffic, and retention rates.
However, it is essential to recognize that CLS is an ongoing challenge as websites continuously evolve with the addition of new content, images, and advertisements. So, don’t forget to continuously monitor and measure CLS!
& Maintenance Services
Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.Get Started