Optimagio

Prevent Layout Shifts: Set Width & Height Attributes Correctly

Learn how to correctly set width and height attributes on images to maintain aspect ratios and prevent layout shifts during loading, improving Core Web Vitals scores.

Optimagio Team 5 min read
Prevent Layout Shifts: Set Width & Height Attributes Correctly

The Hidden Cost of Unspecified Image Dimensions

You've experienced it countless times: reading an article on your phone when suddenly the text jumps downward as an image loads. Or trying to click a button that moves just as your finger touches the screen. These frustrating experiences are caused by Cumulative Layout Shift (CLS), and images without proper dimensions are often the primary culprit.

When browsers don't know an image's dimensions in advance, they can't reserve the appropriate space in the layout. The result is content that shifts around as images load, creating a poor user experience and negatively impacting your Core Web Vitals scores. Google considers CLS so important that it's one of the three key metrics in Core Web Vitals, directly affecting your search rankings.

How Width and Height Attributes Prevent Layout Shifts

The solution is surprisingly simple: always include width and height attributes in your image HTML. These attributes provide the browser with the intrinsic dimensions of your images, allowing it to calculate and reserve the appropriate space in the layout before the images actually download.

Modern browsers use these attributes to create an aspect ratio box—a placeholder that maintains the correct proportions regardless of the final rendered size. This approach works even when you use CSS to make images responsive, as the browser can scale the reserved space proportionally.

Without Dimensions (Causes Shift)
Without Dimensions (Causes Shift)
With Dimensions (Prevents Shift)
With Dimensions (Prevents Shift)

Implementing Responsive Images Without Layout Shifts

The common misconception is that setting fixed width and height attributes will make images non-responsive. This isn't true when implemented correctly. The HTML attributes provide the intrinsic dimensions for aspect ratio calculation, while CSS controls the responsive behavior.

The key technique involves combining the HTML attributes with CSS that sets max-width: 100% and height: auto. This approach ensures that images scale properly on different screen sizes while maintaining their aspect ratio and preventing layout shifts.

  1. 1Add width and height attributesInclude the actual pixel dimensions in your image HTML: <img src="image.jpg" width="800" height="600" alt="Description">
  2. 2Apply responsive CSSAdd CSS rules: img { max-width: 100%; height: auto; } to ensure proper scaling
  3. 3Test across devicesVerify that images maintain aspect ratio and don't cause layout shifts on various screen sizes

Advanced Techniques for Complex Layouts

For more complex scenarios like responsive images with srcset or art-directed pictures, you need additional techniques to prevent layout shifts. When using srcset with images of different aspect ratios, you should base your width and height attributes on the largest variant and use CSS to maintain consistency.

The picture element introduces additional complexity because different source images might have different aspect ratios. In these cases, you can use CSS aspect-ratio property or the padding-top technique to ensure consistent spacing regardless of which image source the browser selects.

/* CSS aspect-ratio technique */
.responsive-image {
  width: 100%;
  aspect-ratio: 16/9;
}

/* Padding-top technique for older browsers */
.aspect-box {
  position: relative;
  width: 100%;
  padding-top: 56.25%; /* 16:9 aspect ratio */
}

.aspect-box img {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

Testing and Debugging Layout Shift Issues

Identifying and fixing layout shifts requires proper testing tools and methodologies. Browser developer tools include specific features for detecting layout shifts, and several online tools can help you measure and improve your CLS scores.

The most effective approach is to test your pages with throttled network connections, as this simulates real-world conditions where images load slowly and layout shifts become most apparent. Combine this with mobile device testing to ensure your solutions work across all user scenarios.

Chrome DevToolsUse the Performance panel with Layout Shift recording enabled to visualize exactly when and where shifts occur during page load.
Lighthouse AuditsRun Lighthouse performance audits to get specific CLS measurements and actionable recommendations for improvement.
WebPageTestTest your site with different connection speeds and devices to identify layout shift issues under various conditions.
  • All images have width and height attributes
  • CSS ensures responsive behavior without breaking aspect ratio
  • No layout shifts visible on slow 3G connection
  • CLS score below 0.1 in Lighthouse audit

Automate image optimization with Optimagio

Doing this by hand for every image does not scale. Optimagio optimizes and converts your images (WebP and AVIF) automatically across your API, web app, and CMS — so every page ships the smallest possible files without manual work. See plans and pricing →

FAQ

Frequently asked questions

Why do images cause layout shifts if I don't set dimensions?

Without width and height attributes, browsers cannot reserve space for images before they load. When images finally load, they push existing content down or sideways, causing visible layout shifts.

Should I use pixels or percentages for width and height attributes?

Use actual pixel dimensions for the width and height attributes in HTML. Then use CSS with percentage or max-width values to make the image responsive while maintaining the correct aspect ratio.

Does setting width and height affect image loading performance?

No, setting dimensions doesn't affect loading performance. It only helps the browser reserve the correct space, preventing layout shifts and improving perceived performance and user experience.

How do I handle responsive images with srcset and different aspect ratios?

For images with varying aspect ratios in srcset, use the width and height of the largest variant. Then use the CSS aspect-ratio property or padding-top technique to maintain consistency across different screen sizes.

Will setting width and height attributes make my images non-responsive?

No. The HTML attributes provide the intrinsic dimensions for aspect ratio calculation, while CSS controls the responsive behavior. Use max-width: 100% and height: auto in CSS to maintain responsiveness.