Image Layout Shift (CLS): Width, Height and Aspect Ratio
Width and height for images and video, aspect-ratio for iframes, and why a theme's width: auto, a lazy loader or a carousel can still shift the page.
- Good ≤ 0.1
- Needs improvement ≤ 0.25
- Poor > 0.25
Layout shift from images in short
An image, video or iframe shifts the page when the browser doesn't know its height before the file arrives. A width and height attribute with the file's own pixel size, plus max-width: 100%; height: auto in the CSS, reserves the right space at any screen width. An iframe gets no ratio from its attributes and needs CSS aspect-ratio.
Why does an image without a width and height move the page?
The browser lays out the page before the photo has arrived. An <img> without a width and height takes no height until the file comes in; then the browser learns the photo's size and everything below the image drops by that height. That drop is a layout shift, and it counts toward the page's Cumulative Layout Shift (CLS).
On a test product page with one photo above the price and eight paragraphs, loaded in Chrome at phone width with the photo arriving after the first paint, the text dropped 196 pixels: a CLS of 0.19 on the first visit. On the repeat visit, with the photo in the browser cache, the CLS was 0. So a missing size shifts the page for first-time visitors, and you, with the photo in your own cache, will probably never see it.

Before

After
The test product page at phone width on a first visit, with no width and height on the photo.
How do you give every image a width and height?
Give every <img> its file's own pixel size in the width and height attributes, and add img { max-width: 100%; height: auto; } to the CSS. The browser turns the two numbers into a ratio and reserves the height before the file arrives, at any screen width.
- WordPress: WordPress writes both attributes for images you add to posts and pages from the media library; the images a theme, page builder, slider or plugin prints itself are the ones without them. WP Rocket (Media, Add Missing Image Dimensions) and Perfmatters (Lazy Loading, Add Missing Image Dimensions) add them to images on your own domain. More per plugin in the WordPress guide.
- Shopify: the
image_tagfilter writes both attributes. A theme section or an app that prints its own<img>needswidth="{{ image.width }}" height="{{ image.height }}"; the Shopify guide has the rest. - Next.js:
next/imagetakes a width and height, orfillinside a parent that has its own size.
In a template:
<img src="skillet.jpg" width="1200" height="675" alt="Cast iron skillet">
img {
max-width: 100%;
height: auto;
} height: auto takes the height from the ratio, and max-width keeps the image inside its column. Chrome DevTools lists that ratio in Styles under img[Attributes Style] as aspect-ratio: auto 1200 / 675, even when a theme's width: auto cancels it, so check the image itself: reloaded with the cache disabled, it has its height before the file arrives.
According to the State of Web Vitals, 92% of sites whose homepage images all have a width and height pass CLS on phones, against 88% of sites with at least one image without them. That is a correlation.
What does "Image elements do not have explicit width and height" mean?
Lighthouse's diagnostic "Image elements do not have explicit width and height" lists every image on the page that lacks a width or a height, from its attributes or its CSS, unless a CSS aspect-ratio makes up for the missing one. The diagnostic has no weight in the Performance score, but every image it lists can shift the page for a first-time visitor. The fix is the file's own width and height on each listed image: WP Rocket and Perfmatters can add them on WordPress, and Shopify's image_tag filter writes them.
Lighthouse counts an image as sized as soon as both attributes are in the HTML, so an image whose theme sets width: auto passes and still shifts the page.
The Layout shift culprits insight guesses the cause of a shift from its timing, so its row "Unsized image element" can name an image that was only pushed: check that the image really has no width and height before you change it (how to check).
Why does an image with a width and height still shift?
A theme rule width: auto on images cancels the width and height attributes: the image is back at zero height until the file arrives, exactly as if the attributes were missing. Attributes with another ratio than the file shift the page too, by the difference: use the file's real width and height.
CLS on the test product page per setup:
| Image setup | First visit | Repeat visit |
|---|---|---|
| No width and height | 0.19 | 0 |
Width and height, theme sets width: auto | 0.19 | 0 |
The same, plus aspect-ratio: 16 / 9 | 0.19 | 0 |
| 800 by 600 on a 16:9 photo | 0.05 | 0 |
Width and height, max-width: 100%; height: auto | 0 | 0 |
In the theme's stylesheet the rule usually looks like this:
img {
width: auto;
height: auto;
max-width: 100%;
} With width: auto the width comes from the file, so until the file arrives the image is 0 pixels wide and the ratio gives it 0 height. Remove the width: auto and keep height: auto and max-width: 100%; setting aspect-ratio next to it doesn't fix it.
![Chrome DevTools Elements panel on the test product page: the img with width 1200 and height 675 selected; in Styles the theme rule img with width: auto, height: auto and max-width: 100%, and under it img[Attributes Style] with width 1200px and height 675px struck through and aspect-ratio: auto 1200 / 675 still listed.](/static/img/resize-680-474/s/cls-image-width-auto-devtools.webp)
Chrome DevTools on the test product page before the photo arrives: the theme's width: auto and height: auto override the 1200 and 675 from the attributes, the aspect-ratio is still listed, and the image is 0 by 0 pixels.
Add Missing Image Dimensions (WP Rocket, Perfmatters) only adds attributes. Without access to the theme's code, override the rule: on WordPress in Appearance > Customize > Additional CSS (in a block theme, the Site Editor's Styles > Additional CSS), on Shopify in Online Store > Edit theme > Theme settings > Custom CSS:
/* the selector DevTools shows next to width: auto */
.entry-content img {
width: revert-layer;
} width: revert-layer gives the image back the width from its attributes, and the theme's max-width: 100% still fits it to the column. On the test product page in Chrome the override took the CLS from 0.19 to 0 and kept a 200 pixel image at 200 pixels; width: 100% also gave 0, but stretched that image to the full column. A theme update keeps the rule as long as the selector stays the same. When the theme's image rule sits inside an @layer, revert-layer rolls back to that rule and the image still shifts; then use width: 100% where every image fills the column, or ask the theme's developer to change the rule in its layer. The auto-sizing images article has the long story.
When do you use CSS aspect-ratio instead?
A width and a ratio in the CSS reserve the space for an image whose template can't write a width and height. On the test product page, an image without attributes and with width: 100% and aspect-ratio: 16 / 9 kept its space as well as the attributes did, a CLS of 0.
img.hero {
width: 100%;
height: auto;
aspect-ratio: 16 / 9;
} For a grid of product photos in mixed shapes, give every image the same box and let object-fit: cover crop each photo to it:
.product-image {
width: 100%;
aspect-ratio: 1 / 1;
object-fit: cover;
} Next to a theme's width: auto, aspect-ratio has no width to work with and leaves the image at zero height until the file arrives: remove the width: auto first.
How do you stop videos, iframes and embeds from shifting?
A <video> with a width and height reserves its space the way an image does, as long as its poster image has the video's ratio. An <iframe> gets no ratio from its width and height: with height: auto it is 150 pixels tall at any width, so it needs aspect-ratio in CSS, or a still image in a box of the final size that loads the iframe later.
<video src="skillet.mp4" poster="skillet.jpg" width="1280" height="720" controls></video> The video takes the same max-width: 100%; height: auto as an image. On the test product page, a video without a width and height had no height until its poster arrived, then pushed the text down 214 pixels: a CLS of 0.21, against 0 with them. The poster's ratio takes over when the poster arrives, so a poster in another shape shifts the page.
On the test product page, an <iframe width="560" height="315"> at full width was 380 by 150 pixels on a phone, and 380 by 214 with aspect-ratio added:
iframe {
width: 100%;
height: auto;
aspect-ratio: 16 / 9;
} An embed script that inserts or resizes the iframe after the load shifts the page on every visit: give the box around it the final size. For YouTube and Google Maps I put a placeholder image of the final size on the page and load the iframe on the first interaction. Nothing moves, whether the iframe comes on a tap, a hover or when it scrolls into view; the code is in perfect YouTube embeds and Google Maps without losing PageSpeed.
An SVG in an <img> needs a width and height like any other image. Ad slots have their own fix in the CLS guide.
Which width and height go on srcset and picture?
All files in one srcset should have the same ratio, so one width and height on the <img> fits every file the browser picks. A <picture> with another crop on phones gets a width and height on each <source>, and Chrome, Firefox and Safari all use them.
<picture>
<source media="(max-width: 799px)" srcset="skillet-phone.jpg" width="800" height="600">
<img src="skillet.jpg" width="1200" height="675" alt="Cast iron skillet">
</picture> On the test product page with a 4:3 crop for phones, the <source> with its own width and height gave a CLS of 0. Without them the browser reserved the <img>'s 16:9 box, and the text moved down 71 pixels when the 4:3 photo arrived: 0.05.
File sizes and formats for the srcset are in Improve image delivery.
Do lazy loading and carousels cause layout shift?
Native loading="lazy" on an image with a width and height moves nothing, because the box is reserved before the file loads. A script lazy loader whose placeholder file in the image's src has another shape than the photo shifts the page when the photo replaces it, on every visit. A carousel that moves its slides with the CSS left property shifts the page with every slide; one that moves them with a transform keeps the CLS at 0.
On the test product page, a script loader with a 1 by 1 pixel placeholder GIF made the photo's box square despite its width and height attributes, until the photo replaced it: a CLS of 0.11 on the first and the repeat visit, because the script swaps the file every time. A placeholder moves the shift to the moment the photo replaces it, unless it already has the photo's ratio: give it the final ratio, or use native lazy loading.
A lazy image without a size shifts the page when its file arrives with the image already in view, after a scroll. That shift counts toward CLS, and a Lighthouse run never scrolls.
WordPress adds loading="lazy" to images in your content itself; keep one lazy loader, WordPress's own or a plugin whose placeholder has the image's ratio. The photo in the first screen and a slider's first slide should never be lazy loaded: LCP request discovery explains why.
An autoplay carousel that moved its slides with left had a CLS of 0.22 on both visits: an autoplay slide is no input from the visitor, so every move counts. With transform: translateX() it had 0, because a transform moves the slides without a new layout. Give the carousel a fixed ratio and animate only the transform:
.carousel {
aspect-ratio: 16 / 9;
overflow: hidden;
}
.carousel .track {
transition: transform .6s; /* the script changes transform, never left */
} How do you find the image that shifts your page?
Field data lists the element that moved, and an image that pushes the page is listed as the element under it: look at what sits directly above the listed element. When that is an image, video or iframe, the fix is on this page.
On the test product page, every image shift was recorded on the paragraphs below the photo, because the photo grows downwards from a top edge that doesn't move. In CoreDash, group CLS on mobile by Attribution Element (CLS), then take a row with a high p75 CLS, open a page with that element and look above it. A shift from a lazy image without a size comes after a scroll: Last Interaction (CLS) shows it.

CoreDash, CLS by Attribution Element (CLS): each row is an element that moved; the image to fix sits directly above it on the page.
Test with Disable cache on, so Chrome loads the page the way a first-time visitor gets it: in Chrome DevTools, tick Disable cache in the Network panel, choose Slow 4G and reload at a phone size. The Core Web Vitals Visualizer, a free Chrome extension, puts an overlay on everything that moved. The full route is in finding and fixing CLS issues.
How do you check that the image no longer shifts?
Reload the page in Chrome DevTools with Disable cache on and Slow 4G: the image has its height before the file arrives, and a recording of the reload in the Performance panel shows no layout shift under it. In real-user data the element under the image drops out of the Attribution Element (CLS) list within days, and Search Console takes up to 28 days to follow.
Some images get their size from code you can't change: a page builder or slider without a setting for it, or an app that prints its own image tag. In a Core Web Vitals power hour we open that template together and work out where the width and height can go.

Need a faster site?
I'm Arjen, and I've been fixing slow sites for 17 years. If you're running into issues you can't fix yourself, send me your URL and let's have a chat.
Send me your URL
Why isn't our fix working?
Sometimes the CDN still serves the old HTML, or the fix went into the desktop template and mobile uses another one. Send me your URL and the problem, and I reply with a few times. I check your field data first, then we spend one hour on your site, and within a day you have my notes in writing. €350 excl. VAT.
Send me your URLNo developer to build the fix? I do the work myself, on a copy of your site.