Domain Search Form
£14.99
£33.98
£23.24
£41.99

Strategies to Mitigate Cumulative Layout Shift (CLS)

Written by Giraffe Hosting Limited
Published 13 December 2023
Strategies to Mitigate Cumulative Layout Shift (CLS) for a Smooth Web Experience
Published: 13 December 2023
Category: 
Written by: Giraffe Hosting Limited
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.

Table of Contents

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:

  • Frustrating User Experience: Users expect content to be stable as a page loads. Unstable elements that move around or change position create a jarring and frustrating experience, leading users to perceive the site as poorly constructed or slow.
  • Confusion and Disorientation: Sudden layout shifts interrupt users' browsing flow, forcing them to reorient and refocus on the content. This interruption makes absorbing information more challenging.
  • Impaired Comprehension: Research indicates that unpredictable content movement impairs users' comprehension. Layout shifts hinder processing speed and information retention.
  • Increased Website Abandonment: High CLS often correlates with higher bounce rates, as users leave due to an unsmooth experience, contributing to a perception of unreliability.
  • Lower Conversion Rates: CLS directly impacts conversion rates, especially for sites that rely on effectively conveying information.
  • Reduced Search Rankings: CLS is part of Google’s core web vitals and a factor in search engine rankings. Sites with significant layout shifts may receive less favorable rankings.

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

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.

Optimized Code Example:

<!-- 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">

Image Compression

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.

Optimized Code Example:

<!-- Using WebP format -->
<img src="image.webp" width="600" height="400" alt="Descriptive Text">

Best Practices

  • Convert images to WebP or JPEG formats for efficient compression.
  • Use SVG for vector graphics, which scales without loss of quality.

Font Loading Strategies

Fonts can delay rendering, causing layout shifts. Preloading fonts and using font-display: swap helps maintain layout stability.

Optimized Code Example:

<!-- 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>

Best Practices:

  • Subset fonts to include only needed characters.
  • Use variable fonts to combine multiple font styles in one file.
  • Load fonts from the same domain as your CSS for efficient caching.

Handling Videos and Embeds

Setting explicit dimensions for videos and iframes is crucial. The aspect ratio placeholder technique can effectively reserve space for these elements.

Optimized Code Example:

<!-- 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>

Managing Third-Party Widgets

Limiting or asynchronously loading third-party widgets can prevent layout shifts. Prioritize essential content, loading additional elements later.

Optimized Code Example:

<!-- Asynchronously Loading a Widget -->
<script src="widget.js" async></script>

Optimizing CSS

Reducing render-blocking CSS, eliminating unused rules, and deferring non-critical stylesheets are essential. CSS minification and specificity reduction are also important.

Optimized Code Example:

<!-- 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>

JavaScript Load Management

Loading non-essential JavaScript asynchronously and deferring scripts can minimize CLS. Optimizing JavaScript code to reduce layout shifts is also key.

Optimized Code Example:

<!-- Asynchronous Script Loading -->
<script async src="non-critical.js"></script>

<!-- Deferred Script Loading -->
<script defer src="sequential-execution.js"></script>

Best Practices:

  • Use async for scripts that don’t depend on other scripts.
  • Use defer for scripts that require sequential execution but don’t need to block HTML parsing.
  • Optimize JavaScript to minimize DOM manipulations, especially on initial load.

Testing Your Fixes

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.

Final Thoughts

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.

Choose Giraffe Hosting Limited for your Domain Registration, Cloud Hosting, WordPress Hosting, and Virtual Private Server Hosting; Powered by renewable energy, contributed to sustainable growth.
Copyright © Giraffe Hosting Limited. 2007 – 2026 All rights reserved.
Explore
Services
Support