---
title: "How do I stop images causing layout shift?"
description: "Images without dimensions make the page jump as they load. Learn how width, height and aspect-ratio reserve space, and when to lazy-load or prioritise an image."
url: https://www.danny.engineering/answer/html/how-do-i-stop-images-causing-layout-shift
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:25:00.000Z
tags: ["HTML"]
---

# How do I stop images causing layout shift?

## The short answer


Give every `<img>` a `width` and `height` attribute that match the image's real proportions, and let CSS keep it responsive with `height: auto`. The browser uses the two numbers to work out the aspect ratio and reserves the right amount of space before the file arrives, so nothing below it moves.


```html
<img src="/team.jpg" alt="The team at the summer meetup" width="1200" height="800" />
```


```css
img {
  max-width: 100%;
  height: auto;
}
```


## Why it happens


When the browser lays out a page, it needs to know how tall each element is. An image with no dimensions has no known size until enough of the file has downloaded for the browser to read its header. Until then the image takes up zero height. Once the size is known, the browser makes room for it and everything below jumps down. That jump is what Cumulative Layout Shift (CLS), one of the Core Web Vitals, measures.


In the past, responsive images made this worse. Developers removed `width` and `height` so that `max-width: 100%` would work, which meant the browser had no information at all. Modern browsers fixed this: they turn the `width` and `height` attributes into a default `aspect-ratio` for the image. With `height: auto` in your CSS, the image scales with its container and still reserves the right space.


## How to fix it


### Add width and height attributes


Use the image's intrinsic pixel size, or any pair of numbers with the same ratio. The values do not fix the displayed size once your CSS sets `max-width: 100%` and `height: auto`; they only tell the browser the shape.


For responsive images with `srcset`, all candidates should share one aspect ratio, so one `width` and `height` pair covers them. For art direction with `<picture>`, each `<source>` can have its own `width` and `height` when the crops differ.


```html
<picture>
  <source media="(max-width: 600px)" srcset="/hero-square.jpg" width="600" height="600" />
  <img src="/hero-wide.jpg" alt="Mountains at sunrise" width="1600" height="900" />
</picture>
```


### Use aspect-ratio in CSS


When you do not know the image size in advance, or the image fills a box of a fixed shape, set the shape on the element with the CSS `aspect-ratio` property. Pair it with `object-fit` so the picture is cropped rather than stretched.


```css
.card-image {
  width: 100%;
  aspect-ratio: 16 / 9;
  object-fit: cover;
}
```


This works for anything that loads late, such as embedded videos, ads or iframes, not only images.


### Lazy-load images below the fold


`loading="lazy"` tells the browser to wait until an image is near the viewport before fetching it. It saves bandwidth on long pages. It does not cause layout shift as long as the image also has dimensions, because the space is still reserved.


```html
<img src="/gallery-12.jpg" alt="Rock pool at low tide" width="800" height="600" loading="lazy" />
```


### Do not lazy-load the LCP image, and give it fetchpriority


The Largest Contentful Paint (LCP) element is often a hero image near the top of the page. Lazy-loading it delays its download until after layout, which makes LCP slower. Leave `loading` off that image (the default is `eager`) and add `fetchpriority="high"` so the browser fetches it ahead of other images. `fetchpriority` is supported in all current major browsers; older ones ignore it safely.


```html
<img src="/hero.jpg" alt="Product dashboard" width="1600" height="900" fetchpriority="high" />
```


Use `fetchpriority="high"` on one or two images at most. If everything is high priority, nothing is.


## Common pitfalls

- Setting `height` in CSS to a fixed value while `width` is fluid. The image distorts. Use `height: auto` or `aspect-ratio` with `object-fit`.
- Adding `loading="lazy"` to every image by default, including the hero. Check which image is the LCP element and leave it eager.
- Using dimensions with the wrong ratio. The browser reserves the wrong space, and the page still shifts when the real image arrives.
- Forgetting web fonts and late-loading banners. Images are a common cause of CLS but not the only one.
- In Next.js, the `Image` component requires `width` and `height` (or `fill` inside a sized parent) for the same reason.

## Related reading

- [MDN: The img element](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img)
- [MDN: aspect-ratio](https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio)
