# "Image elements do not have explicit width and height": how to fix it

**Audit ID:** `unsized-images` · **Category:** Performance

<!--QA-->
> **Quick answer:** Lighthouse flags "Image elements do not have explicit width and height" when an img has no size the browser can read before the file loads, so content below it shifts and CLS rises. Fix it by adding the real pixel size as width and height attributes, then keep it responsive with max-width: 100% and height: auto in CSS.
<!--/QA-->

Lighthouse prints **Image elements do not have explicit `width` and `height`** when an `<img>` on the page has no size the browser can read before the file downloads. Without that size, the browser lays the page out with a zero-height box, then pushes everything below it down when the image arrives. That jump is a layout shift, and it counts toward [Cumulative Layout Shift (CLS)](/audits/cumulative-layout-shift), a Core Web Vital. In our study of 269 Shopify storefronts, 127 of them (47.2%) failed this audit. This page covers what Lighthouse checks, why `width: 100%` in CSS does not pass it, and the one-line fix for HTML, WordPress, Shopify, React and Next.js.

## TL;DR

- **What:** A list of visible `<img>` elements with no explicit width and height, from HTML attributes or CSS.
- **Why it matters:** The browser cannot reserve space for the image, so content below it shifts down when it loads. That raises CLS.
- **Fix:** Add the image's real pixel size as `width` and `height` attributes, then keep it responsive with `max-width: 100%; height: auto;` in CSS.

## What does "Image elements do not have explicit width and height" mean?

The browser builds the layout before most images finish downloading. If the `<img>` tag tells it the image's size, it reserves a box of the right shape and the page does not move later. If the tag does not, the box starts at zero height and grows when the file arrives.

Lighthouse lists every image that has no usable size. Per the audit source, an image passes when it has any one of these:

| The image has | Passes? |
|---|---|
| `width` and `height` HTML attributes | Yes |
| CSS `width` and CSS `height`, neither set to `auto` | Yes |
| A width (HTML or CSS) plus CSS `aspect-ratio` | Yes |
| A height (HTML or CSS) plus CSS `aspect-ratio` | Yes |
| Only CSS `width: 100%` with `height: auto` | **No** |
| No size at all | **No** |

Lighthouse skips images that cannot shift the layout: CSS background images, images inside a shadow root, images with `position: fixed` or `position: absolute`, inline SVG data URIs, and images that are not displayed. The audit carries no weight in the Performance score by itself. It explains the score through CLS, which carries 25% of it.

## Why does `width: 100%` not fix it?

Because CSS `height: auto` is not an explicit height. The rule `img { width: 100%; height: auto; }` tells the browser how wide to draw the image, but not how tall, so it still has to wait for the file to know the shape. Lighthouse treats `auto`, `initial`, `unset` and `inherit` as "not set".

The fix is not to remove that CSS. Keep it, and add the HTML attributes. Modern browsers use the `width` and `height` attributes to compute an aspect ratio before the image loads, so the box has the right height at any rendered width.

## How do I fix it?

### 1. Add `width` and `height` attributes with the image's real size

Use the intrinsic pixel size of the file, not the size it displays at:

```html
<img src="/hero.jpg" alt="Spring collection" width="1600" height="900">
```

Then keep the image responsive in CSS:

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

The browser reads 1600 by 900 as a 16:9 ratio and reserves a 16:9 box at whatever width the layout gives it. The image still scales, and nothing shifts.

### 2. For `srcset` images, use the size of the `src` file

Every file in a `srcset` should have the same aspect ratio. Put the dimensions of the default `src` in the attributes. If you use `<picture>` with art direction (different crops per breakpoint), set `width` and `height` on each `<source>` element too; current Chrome, Firefox and Safari read them.

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

### 3. Use CSS `aspect-ratio` when you cannot touch the HTML

If a CMS or a component library writes the `<img>` tag, set a width and an aspect ratio in CSS. Lighthouse accepts a width plus `aspect-ratio`:

```css
.product-card img {
  width: 100%;
  aspect-ratio: 4 / 5;
  object-fit: cover;
}
```

Use `object-fit: cover` so images with a slightly different ratio crop instead of stretching.

### 4. Reserve space for lazy-loaded images too

`loading="lazy"` does not remove the need for dimensions. A lazy image without them shifts the page when the visitor scrolls to it, which still counts toward CLS in the field. Lighthouse lists lazy images in this audit as well.

## How do I fix it in WordPress, Shopify, React, or Next.js?

- **WordPress:** images inserted through the block editor get `width` and `height` automatically. The usual offenders are theme templates, page builders and slider plugins that output `<img>` without them, and SVG logos uploaded through a plugin. Fix the theme template, or set `aspect-ratio` on the container in Additional CSS.
- **Shopify:** use the `image_tag` filter in Liquid. It writes `width` and `height` from the image object: `{{ product.featured_image | image_url: width: 800 | image_tag }}`. Hand-written `<img src="{{ ... | img_url }}">` tags in older themes and app blocks are the usual offenders.
- **React:** pass `width` and `height` as props on every `<img>`. For images from a CMS, read the dimensions from the CMS response; most image APIs return them.
- **Next.js:** `next/image` requires `width` and `height` (or `fill` inside a sized parent) and writes them for you. Offenders are plain `<img>` tags and `fill` images whose parent has no height.

## Can I fix this for third-party images?

Usually yes, because the space belongs to your layout even when the image does not. Review widgets, ad slots and embedded badges inject their own `<img>` tags. Wrap them in a container with a fixed `min-height` or an `aspect-ratio`, so the space is reserved before the widget renders. That fixes the shift even though Lighthouse may still list the vendor's tag.

## How do I verify the fix?

1. Re-run Lighthouse. The images you fixed should be gone from **Image elements do not have explicit `width` and `height`**.
2. In DevTools, select the image in the Elements panel. The computed `aspect-ratio` should read `auto 1600 / 900` (your dimensions), which shows the browser is using the attributes.
3. Open the Performance panel, record a reload, and check the **Layout shifts** track. The shift at the moment the image loaded should be gone.
4. Watch [CLS](/audits/cumulative-layout-shift) in the Lighthouse report and in field data. That is the metric this audit protects.

## What mistakes should I avoid?

- **Using display size instead of file size.** `width="300" height="300"` on a 1600 by 900 file gives the wrong ratio. The box is square, the image is wide, and the layout still moves or the image distorts.
- **Removing `height: auto` from the CSS.** Without it, a fixed `height` attribute stretches the image when the width scales down.
- **Setting only `width`.** One attribute is not enough. Lighthouse needs both, or one plus CSS `aspect-ratio`.
- **Adding dimensions and then lazy-loading the hero.** That fixes this audit and hurts [Largest Contentful Paint](/audits/largest-contentful-paint). Never lazy-load the image in the first screen.

## Related audits

- [Cumulative Layout Shift](/audits/cumulative-layout-shift): the metric unsized images raise
- [Image elements do not have alt attributes](/audits/image-alt): the other attribute every `<img>` needs
- [Serve images in next-gen formats](/audits/modern-image-formats): smaller files for the same images
- [Largest Contentful Paint](/audits/largest-contentful-paint): why the first-screen image must not be lazy-loaded

---

Audit your URL at https://lighthouse-md.com.
