DEV Community

Nwosa Tochukwu
Nwosa Tochukwu

Posted on

Cumulative Layout Shift (CLS)

What is Cumulative Layout Shift and why is it important?

Cumulative Layout Shift (CLS) is a measure of how much a website moves around while it's loading, and how it affects the user's experience. If a website moves around a lot while it's loading, it can be frustrating for the user, and they may have trouble clicking on buttons or links and we know that a bad experience will make the user most likely not to visit the website again.

in order to prevent such from happening, we use HTML and CSS to make sure that the website's layout is stable and doesn't move around unexpectedly. For example, we can use the "height" and "width" attributes in HTML to make sure that images and other elements have the right size so that the website doesn't have to shift things around while it's loading

Using HTML

for example, in this below code
<img src="image.jpg" alt="A cute puppy" width="200" height="150">

We're using the "width" and "height" attributes to specify the size of the image. By doing this, we're telling the browser exactly how much space the image will take up, so it can plan ahead and avoid any surprises, For most times if we do not specify the width and height attributes, the browser may end up allocating bigger or smaller space for the image and thereby causing our webpage to shift around while loading and also when we don't specify the width and height attributes for an image, the browser may need to make an extra request to the server to get the dimensions of the image before it can reserve space for it. This can slow down the loading of the webpage and lead to an inconsistent user experience. By specifying the width and height attributes, we can help the browser load the webpage faster and create a smoother user experience.

Using CSS

We can also use CSS to make sure that elements on the website are positioned correctly so that they don't shift around unexpectedly. For example, we can use the "position" property in CSS to specify where exactly we want an element should be placed on the page. The "position" property in CSS can be used to specify whether the element is relative to other elements or the viewport. By using CSS to control the layout of our webpage, we can make sure that everything stays in its proper place and that the user experience is consistent across different devices and screen sizes.

Here's an example of how this works in CSS

button {
  position: absolute;
  top: 10px;
  left: 10px;
}
Enter fullscreen mode Exit fullscreen mode

In the code above, we're using the "position" property to tell the browser where to place the "button" element on the page. By doing this, we're making sure that the button stays in the same place while the website is loading, so that the user doesn't have to deal with any unexpected layout shifts or changes that could confuse or frustrate the user. By taking control of the layout of our webpage using CSS, we can create a more consistent and predictable user experience that makes it easier for people to use our website.

Importance of Cumulative Layout Shift (CLS)

  • User experience: Users expect websites to be stable and predictable, so when elements shift unexpectedly, it can be frustrating and confusing. By preventing cumulative layout shift, we can create a better user experience and make sure that users can interact with the website in a smooth and seamless way.

  • Accessibility: For users with disabilities, unexpected layout shifts can be even more problematic. By making sure that the website's layout is stable and predictable, we can create a more accessible website that works for all users.

  • Search Engine Optimization (SEO): Cumulative layout shift is one of the factors that Google considers when ranking websites in search results. Websites with a lot of layout shifts may be penalized in search rankings, so it's important to prevent them in order to maintain good Search Engine Optimization (SEO).

  • Conversions: If users have trouble interacting with a website due to unexpected layout shifts, they may be less likely to convert (i.e. make a purchase, sign up for a newsletter, etc.). By creating a stable and predictable website, we can improve conversions and help businesses meet their goals.

  • Performance: Websites with a lot of layout shifts may take longer to load, which can affect performance and user satisfaction. By preventing cumulative layout shift, we can create a more efficient and fast-loading website that users will enjoy using.

Overall, preventing cumulative layout shift is all about making sure that the website's layout is stable and predictable, so that the user can have a smooth and enjoyable experience. By using HTML and CSS to specify the size and position of elements on the page, we can make sure that the layout of the webpage is stable and predictable, we can prevent any unexpected shifts or changes during loading that could confuse or frustrate the user. This can help us create a more user-friendly website that people will want to visit again and again.

Thank you for taking the time to read this. If you found it informative and engaging, I would greatly appreciate it if you could leave a like and a review. Your feedback would be invaluable to me.

I'm Nwosa Tochukwu a Junior Frontend developer

Top comments (0)