
Cumulative Layout Shift (CLS) is a critical metric that measures the stability of content on a web page as it loads. It evaluates the extent to which content shifts around during the loading or resizing of page elements. A poor CLS score can adversely affect your website's performance in various ways:
In this post, we explore comprehensive strategies to address and prevent these issues using Giraffe Hosting's Website Acceleration Suite.
Use of Correct Image Dimensions: Specifying width and height attributes on tags is crucial. This approach reserves space in the document layout before images load, thereby preventing layout shifts. Accurate dimensions that match the true size of the image are essential. This is equally important for lazy-loaded images.
Specifying width and height attributes in tags is essential for reserving space in the document layout before images load, preventing layout shifts. This is crucial for both standard and lazy-loaded images.
<!-- Standard Image Loading -->
<img src="example.jpg" width="600" height="400" alt="Descriptive Text">
<!-- Lazy-Loaded Image -->
<img src="example-lazy.jpg" width="600" height="400" alt="Descriptive Text" loading="lazy">
Optimizing images with compression tools and lightweight formats like WebP, SVG, or JPEG enhances loading speeds. Smaller image files reduce CLS due to quicker loading.
<!-- Using WebP format -->
<img src="image.webp" width="600" height="400" alt="Descriptive Text">
Fonts can delay rendering, causing layout shifts. Preloading fonts and using font-display: swap helps maintain layout stability.
<!-- Preloading Font -->
<link rel="preload" href="font.woff2" as="font" type="font/woff2" crossorigin>
<!-- CSS for font-display -->
<style>
@font-face {
font-family: 'MyWebFont';
src: url('font.woff2') format('woff2');
font-display: swap;
}
</style>
Setting explicit dimensions for videos and iframes is crucial. The aspect ratio placeholder technique can effectively reserve space for these elements.
<!-- Aspect Ratio Placeholder for a 16:9 Video -->
<div style="position: relative; padding-top: 56.25%;">
<iframe src="video.mp4" style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;" frameborder="0" allowfullscreen></iframe>
</div>
Limiting or asynchronously loading third-party widgets can prevent layout shifts. Prioritize essential content, loading additional elements later.
<!-- Asynchronously Loading a Widget -->
<script src="widget.js" async></script>
Reducing render-blocking CSS, eliminating unused rules, and deferring non-critical stylesheets are essential. CSS minification and specificity reduction are also important.
<!-- Inline Critical CSS -->
<style>
/* Critical above-the-fold CSS rules */
</style>
<!-- Deferred Non-Critical CSS -->
<link rel="stylesheet" href="styles.css" media="print" onload="this.media='all'">
<noscript><link rel="stylesheet" href="styles.css"></noscript>
Loading non-essential JavaScript asynchronously and deferring scripts can minimize CLS. Optimizing JavaScript code to reduce layout shifts is also key.
<!-- Asynchronous Script Loading -->
<script async src="non-critical.js"></script>
<!-- Deferred Script Loading -->
<script defer src="sequential-execution.js"></script>
Utilize tools like Lighthouse, Google PageSpeed Insights, and Webpagetest to measure CLS improvements. Also, check real user data in the “page experience” and “core web vitals” sections of Google Search Console for both mobile and desktop versions.
A proactive strategy in design changes and content uploads, aligned with thorough testing, is essential to achieve an excellent CLS score and ensure a seamless user experience. With Giraffe Hosting's advanced tools and optimizations, maintaining an optimal CLS becomes more manageable and effective.