{"id":2229,"date":"2023-06-14T01:45:03","date_gmt":"2023-06-14T01:45:03","guid":{"rendered":"https:\/\/ewebtoolz.com\/blog\/cumulative-layout-shift-cls-defined-measured-how-to-fix\/"},"modified":"2023-06-14T01:45:03","modified_gmt":"2023-06-14T01:45:03","slug":"cumulative-layout-shift-cls-defined-measured-how-to-fix","status":"publish","type":"post","link":"https:\/\/ewebtoolz.com\/blog\/cumulative-layout-shift-cls-defined-measured-how-to-fix\/","title":{"rendered":"Cumulative Layout Shift (CLS) \u2013 Defined, Measured, &#038; How to Fix"},"content":{"rendered":"<p> <br \/>\n<\/p>\n<div id=\"\">\n<p>Cumulative Layout Shift (CLS) measures the visual stability of a page as it loads. It does this by looking at how big elements are and how far they move. It\u2019s one of the three Core Web Vitals metrics Google uses to measure page experience.<\/p>\n<p>CLS is calculated during the five-second window where the most shifting occurs.<\/p>\n<div class=\"sidenote\">\n<p>Sidenote.<\/p>\n<p> Expected layout shifts, like after a user action, are fine and expected. Shifts within 500 ms of a user interaction are excluded from the calculations.<\/p><\/div>\n<p>Here\u2019s how it\u2019s measured:<\/p>\n<blockquote class=\"wp-block-quote\">\n<p>layout shift score = impact fraction x distance fraction<\/p>\n<\/blockquote>\n<p>Or said in a more understandable way, it\u2019s:<\/p>\n<blockquote class=\"wp-block-quote\">\n<p>layout shift score = % of the viewport that changed * the distance an unstable element moved<\/p>\n<\/blockquote>\n<p>The reason CLS is important is it\u2019s annoying when you try to click something on a page that shifts and then end up clicking on something you don\u2019t intend to.\u00a0<\/p>\n<p>It happens to me all the time. I click on one thing and, suddenly, I\u2019m clicking on an ad and am now not even on the same website. As a user, I find that frustrating.<\/p>\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"1246\" height=\"680\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image10.gif\" alt=\"Click a link that shifts, showing a layout shift and why it's an issue\" class=\"wp-image-161119\"\/><\/noscript><img decoding=\"async\" width=\"1246\" height=\"680\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image10.gif\" alt=\"Click a link that shifts, showing a layout shift and why it's an issue\" class=\"lazyload wp-image-161119\"\/><\/figure>\n<p>Common causes of CLS include:<\/p>\n<ul>\n<li>Images without dimensions.<\/li>\n<li>Ads, embeds, and iframes without dimensions.<\/li>\n<li>Injecting content with JavaScript.<\/li>\n<li>Applying fonts or styles late in the\u00a0load.<\/li>\n<\/ul>\n<p>Let\u2019s look at what your CLS score should be and how to improve it.<\/p>\n<div class=\"post-nav-link clearfix\" id=\"section1\"><a class=\"subhead-anchor\" data-tip=\"tooltip__copielink\" rel=\"#section1\"><svg width=\"19\" height=\"19\" viewbox=\"0 0 14 14\" style=\"\"><g fill=\"none\" fill-rule=\"evenodd\"><path d=\"M0 0h14v14H0z\"\/><path d=\"M7.45 9.887l-1.62 1.621c-.92.92-2.418.92-3.338 0a2.364 2.364 0 0 1 0-3.339l1.62-1.62-1.273-1.272-1.62 1.62a4.161 4.161 0 1 0 5.885 5.884l1.62-1.62L7.45 9.886zM5.527 5.135L7.17 3.492c.92-.92 2.418-.92 3.339 0 .92.92.92 2.418 0 3.339L8.866 8.473l1.272 1.273 1.644-1.643A4.161 4.161 0 1 0 5.897 2.22L4.254 3.863l1.272 1.272zm-.66 3.998a.749.749 0 0 1 0-1.06l2.208-2.206a.749.749 0 1 1 1.06 1.06L5.928 9.133a.75.75 0 0 1-1.061 0z\" style=\"\"\/><\/g><\/svg><\/a><\/p>\n<p><h2 class=\"wp-block-heading\">What\u2019s a good CLS\u00a0score?<\/h2>\n<\/p>\n<\/div>\n<p>A good CLS score is less than or equal to 0.1 and should be based on Chrome User Experience Report (CrUX) data. This is data from actual users of Chrome who are on your site and have opted in to sharing this information. You need 75% of page loads to have a CLS score of 0.1 or\u00a0lower.<\/p>\n<p>Your page may be classified into one of the following buckets:<\/p>\n<ul>\n<li>Good: &lt;=0.1<\/li>\n<li>Needs improvement: &gt;0.1 and &lt;=0.25<\/li>\n<li>Poor: &gt;0.25<\/li>\n<\/ul>\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"1600\" height=\"1634\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cumulative-layout-shift-cls.png\" alt=\"CLS thresholds for good, needs improvement, and poor\" class=\"wp-image-161157\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cumulative-layout-shift-cls.png 1600w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cumulative-layout-shift-cls-416x425.png 416w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cumulative-layout-shift-cls-768x784.png 768w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cumulative-layout-shift-cls-1504x1536.png 1504w\" sizes=\"(max-width: 1600px) 100vw, 1600px\"\/><\/noscript><img decoding=\"async\" width=\"1600\" height=\"1634\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cumulative-layout-shift-cls.png\" alt=\"CLS thresholds for good, needs improvement, and poor\" class=\"lazyload wp-image-161157\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cumulative-layout-shift-cls.png 1600w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cumulative-layout-shift-cls-416x425.png 416w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cumulative-layout-shift-cls-768x784.png 768w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cumulative-layout-shift-cls-1504x1536.png 1504w\" data-sizes=\"(max-width: 1600px) 100vw, 1600px\"\/><\/figure>\n<h3 class=\"wp-block-heading\">CLS data<\/h3>\n<p>72.8% of sites have good CLS scores as of April 2023. This is averaged across the site. As we mentioned, you need 75% of page loads to have a CLS score of 0.1 or lower to be classified as\u00a0good.<\/p>\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"1600\" height=\"1540\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-April-2023.png\" alt=\"Percentage of good CLS values from CrUX CWV data as of April 2023\" class=\"wp-image-161123\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-April-2023.png 1600w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-April-2023-442x425.png 442w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-April-2023-768x739.png 768w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-April-2023-1536x1478.png 1536w\" sizes=\"(max-width: 1600px) 100vw, 1600px\"\/><\/noscript><img decoding=\"async\" width=\"1600\" height=\"1540\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-April-2023.png\" alt=\"Percentage of good CLS values from CrUX CWV data as of April 2023\" class=\"lazyload wp-image-161123\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-April-2023.png 1600w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-April-2023-442x425.png 442w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-April-2023-768x739.png 768w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-April-2023-1536x1478.png 1536w\" data-sizes=\"(max-width: 1600px) 100vw, 1600px\"\/><\/figure>\n<p>CLS is the most improved Core Web Vital since Google\u2019s push for faster websites.\u00a0<\/p>\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"1600\" height=\"1523\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-scores-CWV.png\" alt=\"Percentage of good CLS scores from CrUX CWV data from November 2019 to April 2023\" class=\"wp-image-161124\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-scores-CWV.png 1600w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-scores-CWV-446x425.png 446w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-scores-CWV-768x731.png 768w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-scores-CWV-1536x1462.png 1536w\" sizes=\"(max-width: 1600px) 100vw, 1600px\"\/><\/noscript><img decoding=\"async\" width=\"1600\" height=\"1523\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-scores-CWV.png\" alt=\"Percentage of good CLS scores from CrUX CWV data from November 2019 to April 2023\" class=\"lazyload wp-image-161124\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-scores-CWV.png 1600w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-scores-CWV-446x425.png 446w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-scores-CWV-768x731.png 768w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/percent-good-CLS-scores-CWV-1536x1462.png 1536w\" data-sizes=\"(max-width: 1600px) 100vw, 1600px\"\/><\/figure>\n<p>When we ran a study at the page level using <a href=\"https:\/\/ahrefs.com\/site-audit\">Site Audit<\/a> data, we saw that CLS is similar on desktop and mobile.<\/p>\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"1600\" height=\"2760\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cls-by-device.png\" alt=\"Breakdown of CLS by device&#10;\" class=\"wp-image-161126\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cls-by-device.png 1600w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cls-by-device-246x425.png 246w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cls-by-device-768x1325.png 768w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cls-by-device-890x1536.png 890w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cls-by-device-1187x2048.png 1187w\" sizes=\"(max-width: 1600px) 100vw, 1600px\"\/><\/noscript><img decoding=\"async\" width=\"1600\" height=\"2760\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cls-by-device.png\" alt=\"Breakdown of CLS by device&#10;\" class=\"lazyload wp-image-161126\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cls-by-device.png 1600w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cls-by-device-246x425.png 246w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cls-by-device-768x1325.png 768w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cls-by-device-890x1536.png 890w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/cls-by-device-1187x2048.png 1187w\" data-sizes=\"(max-width: 1600px) 100vw, 1600px\"\/><\/figure>\n<p>We also noted many sites struggle with CLS, especially on slower connections.<\/p>\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"1600\" height=\"1429\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image5-4.png\" alt=\"Breakdown of CLS by connection type\" class=\"wp-image-161127\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image5-4.png 1600w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image5-4-476x425.png 476w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image5-4-768x686.png 768w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image5-4-1536x1372.png 1536w\" sizes=\"(max-width: 1600px) 100vw, 1600px\"\/><\/noscript><img decoding=\"async\" width=\"1600\" height=\"1429\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image5-4.png\" alt=\"Breakdown of CLS by connection type\" class=\"lazyload wp-image-161127\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image5-4.png 1600w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image5-4-476x425.png 476w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image5-4-768x686.png 768w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image5-4-1536x1372.png 1536w\" data-sizes=\"(max-width: 1600px) 100vw, 1600px\"\/><\/figure>\n<p>CLS is worse in the page-level data than the origin data. It\u2019s likely that people are improving their main pages, which get more traffic, while leaving a lot of other pages with failing scores.<\/p>\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"1600\" height=\"1426\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/page-level-CLS-metrics.png\" alt=\"Core Web Vitals metric breakdown at the page level instead of origin level&#10;\" class=\"wp-image-161129\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/page-level-CLS-metrics.png 1600w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/page-level-CLS-metrics-477x425.png 477w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/page-level-CLS-metrics-768x684.png 768w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/page-level-CLS-metrics-1536x1369.png 1536w\" sizes=\"(max-width: 1600px) 100vw, 1600px\"\/><\/noscript><img decoding=\"async\" width=\"1600\" height=\"1426\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/page-level-CLS-metrics.png\" alt=\"Core Web Vitals metric breakdown at the page level instead of origin level&#10;\" class=\"lazyload wp-image-161129\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/page-level-CLS-metrics.png 1600w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/page-level-CLS-metrics-477x425.png 477w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/page-level-CLS-metrics-768x684.png 768w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/page-level-CLS-metrics-1536x1369.png 1536w\" data-sizes=\"(max-width: 1600px) 100vw, 1600px\"\/><\/figure>\n<p>There are different ways of measuring CLS: field data and lab\u00a0data.\u00a0<\/p>\n<p>Field data comes from the <a href=\"https:\/\/developer.chrome.com\/docs\/crux\/\">Chrome User Experience Report (CrUX)<\/a>, which is data from real users of Chrome who choose to share their data. This gives you the best idea of real-world CLS performance. It\u2019s also what you\u2019ll actually be measured on by Google for Core Web Vitals.\u00a0<\/p>\n<p>Lab data is based on tests with the same conditions to make tests repeatable. Google doesn\u2019t use this for Core Web Vitals. But it\u2019s useful for testing because CrUX\/field data is a 28-day rolling average, so it takes a while to see the impact of changes.<\/p>\n<p>The best tool to measure CLS depends on the type of data you want (lab\/field) and whether you want it for one URL or\u00a0many.<\/p>\n<h3 class=\"wp-block-heading\">Measuring CLS for a single URL<\/h3>\n<p><a href=\"https:\/\/pagespeed.web.dev\/\">Pagespeed Insights<\/a> pulls page-level field data that you can\u2019t otherwise query in the CrUX dataset. It also runs lab tests for you based on Google <a href=\"https:\/\/developer.chrome.com\/docs\/lighthouse\/overview\/\">Lighthouse<\/a> and gives you origin data so you can compare page performance to the entire site.<\/p>\n<h3 class=\"wp-block-heading\">Measuring CLS for many URLs or an entire site<\/h3>\n<p>You can get CrUX data in Google Search Console that is bucketed into the categories of good, needs improvement, and\u00a0poor.<\/p>\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"1437\" height=\"1490\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image11-1.png\" alt=\"Core Web Vitals data in Google Search Console\" class=\"wp-image-161130\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image11-1.png 1437w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image11-1-410x425.png 410w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image11-1-768x796.png 768w\" sizes=\"(max-width: 1437px) 100vw, 1437px\"\/><\/noscript><img decoding=\"async\" width=\"1437\" height=\"1490\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image11-1.png\" alt=\"Core Web Vitals data in Google Search Console\" class=\"lazyload wp-image-161130\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image11-1.png 1437w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image11-1-410x425.png 410w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image11-1-768x796.png 768w\" data-sizes=\"(max-width: 1437px) 100vw, 1437px\"\/><\/figure>\n<p>Clicking into one of the issues gives you a breakdown of page groups that are impacted. The groups are pages with similar values that likely use the same template. You make the changes once in the template, and that will be fixed across the pages in the\u00a0group.<\/p>\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"1421\" height=\"1552\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image8-3.png\" alt=\"Page groups for CLS issues\" class=\"wp-image-161131\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image8-3.png 1421w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image8-3-389x425.png 389w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image8-3-768x839.png 768w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image8-3-1406x1536.png 1406w\" sizes=\"(max-width: 1421px) 100vw, 1421px\"\/><\/noscript><img decoding=\"async\" width=\"1421\" height=\"1552\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image8-3.png\" alt=\"Page groups for CLS issues\" class=\"lazyload wp-image-161131\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image8-3.png 1421w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image8-3-389x425.png 389w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image8-3-768x839.png 768w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image8-3-1406x1536.png 1406w\" data-sizes=\"(max-width: 1421px) 100vw, 1421px\"\/><\/figure>\n<p>If you want both lab data and field data at scale, the only way to get that is through the PageSpeed Insights API. You can connect to it easily with Ahrefs\u2019 <a href=\"https:\/\/ahrefs.com\/site-audit\">Site Audit<\/a> and get reports detailing your performance. This is free for verified sites with an <a href=\"https:\/\/ahrefs.com\/webmaster-tools\">Ahrefs Webmaster Tools<\/a> (AWT) account.<\/p>\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"1600\" height=\"644\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image6-5.png\" alt=\"Core Web Vitals data in Ahrefs' Site Audit\" class=\"wp-image-161133\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image6-5.png 1600w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image6-5-680x274.png 680w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image6-5-768x309.png 768w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image6-5-1536x618.png 1536w\" sizes=\"(max-width: 1600px) 100vw, 1600px\"\/><\/noscript><img decoding=\"async\" width=\"1600\" height=\"644\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image6-5.png\" alt=\"Core Web Vitals data in Ahrefs' Site Audit\" class=\"lazyload wp-image-161133\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image6-5.png 1600w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image6-5-680x274.png 680w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image6-5-768x309.png 768w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image6-5-1536x618.png 1536w\" data-sizes=\"(max-width: 1600px) 100vw, 1600px\"\/><\/figure>\n<p>Note that the Core Web Vitals data shown will be determined by the user-agent you select for your crawl during the setup. If you crawl from mobile, you\u2019ll get mobile CWV values from the\u00a0API.<\/p>\n<p>In <a href=\"https:\/\/pagespeed.web.dev\/\">PageSpeed Insights<\/a>, if you select CLS in the \u201cDiagnostics\u201d section, you can see all the related issues like \u201cAvoid large layout shifts.\u201d These are the issues you\u2019ll want to\u00a0solve.<\/p>\n<figure class=\"wp-block-image size-full\"><noscript><img decoding=\"async\" width=\"1999\" height=\"944\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image1-3.png\" alt=\"Issues related to CLS in Google PageSpeed Insights\" class=\"wp-image-161134\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image1-3.png 1999w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image1-3-680x321.png 680w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image1-3-768x363.png 768w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image1-3-1536x725.png 1536w\" sizes=\"(max-width: 1999px) 100vw, 1999px\"\/><\/noscript><img decoding=\"async\" width=\"1999\" height=\"944\" src=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image1-3.png\" alt=\"Issues related to CLS in Google PageSpeed Insights\" class=\"lazyload wp-image-161134\" srcset=\"https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image1-3.png 1999w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image1-3-680x321.png 680w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image1-3-768x363.png 768w, https:\/\/ahrefs.com\/blog\/wp-content\/uploads\/2023\/06\/image1-3-1536x725.png 1536w\" data-sizes=\"(max-width: 1999px) 100vw, 1999px\"\/><\/figure>\n<p>In most cases, to optimize CLS, you\u2019re going to be working on issues related to images, fonts or, possibly, injected content. Let\u2019s look at each\u00a0case.<\/p>\n<h3 class=\"wp-block-heading\">1. Reserve space for images, videos, iframes<\/h3>\n<p>For images, what you need to do is reserve the space so that there\u2019s no shift and the image simply fills that space. This can mean setting the height and width of images by specifying them within the &lt;img&gt; tag like\u00a0this:<\/p>\n<p><code>&lt;img src=\u201ccat.jpg\" width=\"640\" height=\"360\" alt=\u201ccat with string\" \/&gt;<\/code><\/p>\n<p>For responsive images, you need to use a srcset like\u00a0this:<\/p>\n<p><code>&lt;img<\/code><\/p>\n<p><code>width=\"1000\"<\/code><\/p>\n<p><code>height=\"1000\"<\/code><\/p>\n<p><code>src=\"https:\/\/ahrefs.com\/blog\/cumulative-layout-shift-cls\/puppy-1000.jpg\"<\/code><\/p>\n<p><code>srcset=\"https:\/\/ahrefs.com\/puppy-1000.jpg 1000w, https:\/\/ahrefs.com\/puppy-2000.jpg 2000w, https:\/\/ahrefs.com\/puppy-3000.jpg 3000w\"<\/code><\/p>\n<p><code>alt=\"Puppy with balloons\" \/&gt;<\/code><\/p>\n<p>You\u2019ll also want to reserve the space needed for things like videos and iframes. For dynamic content like ads, you will want to reserve the max space needed.<\/p>\n<p>There\u2019s also a relatively new CSS property called aspect-ratio that will allow you to set a dynamic width based on the screen size, and the browser will calculate the appropriate height for\u00a0you.<\/p>\n<h3 class=\"wp-block-heading\">2. Optimize fonts<\/h3>\n<p>For fonts, the goal is to get the font on the screen as fast as possible and to not swap it with another font. When a font is loaded or changed, you end up with a noticeable shift like a Flash of Invisible Text (FOIT) or Flash of Unstyled Text (FOUT).<\/p>\n<p>If you can use a system font, do that. There\u2019s nothing to load, so there are no delays or changes that will cause a\u00a0shift.<\/p>\n<p>If you have to use a custom font, the current best method for minimizing CLS is to combine\u00a0<a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/HTML\/Link_types\/preload\">&lt;link rel=\u201dpreload\u201d&gt;<\/a> (which is going to try to grab your font as soon as possible) and font-display: optional (which is going to give your font a small window of time to\u00a0load).\u00a0<\/p>\n<p>If the font doesn\u2019t make it in time, the initial page load will simply show a default font. Your custom font will then be cached and show up on subsequent page\u00a0loads.<\/p>\n<h3 class=\"wp-block-heading\">3. Use animations that don\u2019t trigger layout changes<\/h3>\n<p>There are some CSS property values that trigger layout shifts, such as \u201cbox-shadow,\u201d \u201cbox-sizing,\u201d \u201ctop,\u201d \u201cleft,\u201d and more that should not be animated. Instead, you\u2019ll want to use \u201ctransform\u201d animations to avoid layout shifts.\u00a0<\/p>\n<h3 class=\"wp-block-heading\">4. Make sure your pages are eligible for bfcache<\/h3>\n<p>The back\/forward cache keeps pages in the browser cache. It allows for instant loading of a page that was already loaded, meaning no layout shifts will happen.\u00a0<\/p>\n<p>There\u2019s a decent amount to this one optimization. The main strategies are listed below, and you can read more about them <a href=\"https:\/\/web.dev\/bfcache\/#optimize-your-pages-for-bfcache\">here<\/a>.<\/p>\n<p>Main strategies:<\/p>\n<ul>\n<li>Never use the unload event<\/li>\n<li>Minimize use of Cache-Control: no-store<\/li>\n<li>Update stale or sensitive data after bfcache restore<\/li>\n<li>Avoid window.opener references<\/li>\n<li>Always close open connections before the user navigates away<\/li>\n<li>Test to ensure your pages are cacheable<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\">Final thoughts<\/h2>\n<p>Since CLS was introduced, we\u2019ve already seen innovations like bfcache and CSS aspect-ratio that help with the issue. I expect that we\u2019ll see more innovation and more new ways to prevent layout shifts in the future.\u00a0<\/p>\n<p>If you have any questions, message me <a href=\"https:\/\/twitter.com\/patrickstox\">on Twitter<\/a>.<\/p>\n<\/p><\/div>\n<p><script async src=\"\/\/platform.twitter.com\/widgets.js\" charset=\"utf-8\"><\/script><br \/>\n<br \/><br \/>\n<br \/><a href=\"https:\/\/ahrefs.com\/blog\/cumulative-layout-shift-cls\/\">Source link <\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Cumulative Layout Shift (CLS) measures the visual stability of a page as it loads. It does this by looking at how big elements are and how far they move. It\u2019s one of the three Core Web Vitals metrics Google uses to measure page experience. CLS is calculated during the five-second window where the most shifting [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":2230,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-2229","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-seo"],"_links":{"self":[{"href":"https:\/\/ewebtoolz.com\/blog\/wp-json\/wp\/v2\/posts\/2229","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ewebtoolz.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ewebtoolz.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ewebtoolz.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ewebtoolz.com\/blog\/wp-json\/wp\/v2\/comments?post=2229"}],"version-history":[{"count":0,"href":"https:\/\/ewebtoolz.com\/blog\/wp-json\/wp\/v2\/posts\/2229\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ewebtoolz.com\/blog\/wp-json\/wp\/v2\/media\/2230"}],"wp:attachment":[{"href":"https:\/\/ewebtoolz.com\/blog\/wp-json\/wp\/v2\/media?parent=2229"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ewebtoolz.com\/blog\/wp-json\/wp\/v2\/categories?post=2229"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ewebtoolz.com\/blog\/wp-json\/wp\/v2\/tags?post=2229"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}