Table of Contents

Core web vitals flagged Image elements do not have explicit width and height

Resolving Layout Shifts: Fixing Core Web Vitals Issues with Images When optimizing for Core Web Vitals, developers often encounter warnings related to...

2026-08-10

Resolving Layout Shifts: Fixing Core Web Vitals Issues with Images

When optimizing for Core Web Vitals, developers often encounter warnings related to Cumulative Layout Shift (CLS). One of the most common culprits for CLS is dynamically loading content—especially images and ads—that causes surrounding elements to jump around as they load. The specific warning you are seeing, "Image elements do not have explicit width and height," points directly to this problem: the browser cannot predict the space an image will occupy until it has downloaded the file, leading to potential layout shifts.

This issue is particularly pronounced in responsive layouts, such as those built with older frameworks like Bootstrap, where classes like img-responsive manage width but often fail to reserve necessary vertical space if the aspect ratio isn't strictly defined. Understanding how to fix this requires moving beyond simple responsive classes and embracing explicit dimension setting.

Understanding Cumulative Layout Shift (CLS)

CLS measures the total amount of unexpected layout shift that occurs during the entire page lifecycle. A good CLS score requires that no content shifts unexpectedly while loading. When an image loads, if its dimensions are not explicitly set in the HTML, the browser has to wait for the image to load before inserting it into the flow, causing text and other elements below it to be pushed down—this is the layout shift.

The core principle to prevent this shift is ensuring that every element reserves its space before the content is rendered. For images, this means defining both the width and height attributes directly on the <img> tag.

The Solution: Explicit Dimensions for Responsiveness

While using srcset is essential for performance (allowing the browser to choose the most appropriate image file based on screen density), it does not inherently solve the CLS problem if dimensions are missing. The best practice is to combine explicit sizing with responsive techniques.

Instead of relying solely on CSS classes that manipulate width, we must define the intrinsic size of the image using HTML attributes:

<img 
    src="someimage.jpg" 
    width="1000" 
    height="667" 
    alt="something" 
    class="img-responsive"
  >
  

By setting width and height, you tell the browser exactly how much space to allocate for the image immediately upon parsing the HTML. Even with responsive classes, this explicit definition provides a stable anchor point, dramatically reducing or eliminating layout shifts when the image finally renders. This technique is fundamental in building robust user interfaces, similar to how careful resource management is vital in application design, whether you are using Laravel or any other framework.

Applying the Fix to Carousels and Layouts

Your example involving an Owl Carousel highlights this perfectly. When images inside a carousel load asynchronously, they can cause shifts if their container dimensions aren't stable. Since your layout is responsive, you need to ensure that the aspect ratio is maintained regardless of the viewport size.

A more modern, robust approach involves using CSS to define the aspect ratio (often using padding hacks or the modern aspect-ratio property) in conjunction with the explicit width/height attributes defined above. This ensures that the container maintains its intended shape even as the image scales within it.

For example, if you are constrained by a fixed height for your carousel items:

<div class="item">
      <img 
          src="image.jpg" 
          width="100%" 
          height="auto" 
          style="display: block; width: 100%; height: auto;" 
          alt="Description"
      >
  </div>
  

While the align-self or display: block properties help manage flow, the explicit width and height attributes remain the non-negotiable foundation for satisfying Core Web Vitals requirements. When managing complex component interactions, like dynamic carousels, ensuring these foundational HTML elements are correctly defined is paramount to achieving a smooth user experience.

Stefan

Stefan

SEO engineer and Laravel developer. Building tools to help Laravel applications rank higher in search results.

Share this article

Back to Blog