How to Fix Lighthouse’s “Image elements do not have explicit width and height” Warning
The Lighthouse warning "Image elements do not have explicit width and height" goes away when you give the <img> both a width and a height attribute. On pages that use max-width: 100% in CSS, also add height: auto so the image doesn't stretch vertically. The browser can then reserve space for the image before it arrives, so the text below no longer jumps down when the image loads (a layout shift). On the page I tested, CLS, one of the Core Web Vitals metrics, went from 0.03 to 0. I ran Lighthouse for real in the test environment and include the output and scores from before and after the fix, unchanged.
The test environment was Ubuntu 24.04, Node.js 22, and Lighthouse 13.6.0 (the npm package). I ran Lighthouse from the command line with its default settings (mobile), and checked only the performance category. The test page was served from a small server running in the test environment (localhost:8080), and only the image was delayed by 1.5 seconds. I used Chrome for Testing 153, which comes with Playwright 1.63.0, to run Lighthouse, measure image sizes, and take screenshots. I did not test the Lighthouse panel in Chrome DevTools, PageSpeed Insights, results with the desktop settings, or results on a live public site.
- 1. The page that triggers the warning, and Lighthouse's output
- 2. Until the image arrives, the img has a height of 0
- 3. The fix: add width and height attributes, and set height: auto in CSS
- 4. Forgetting height: auto makes the image stretch vertically
- 5. Adding only width does not clear the warning
- 6. When you can't add attributes, reserve space with aspect-ratio in CSS
The page that triggers the warning, and Lighthouse's output
The warning appeared on a page where the <img> had neither width nor height. To keep the image from overflowing on smartphone screens, the page only set max-width: 100% in CSS.
<style>
body { margin: 0 auto; max-width: 640px; font-family: sans-serif; }
img { max-width: 100%; }
</style>
<h1>猫の手日記</h1>
<img src="/cat.png" alt="水色の猫の顔">
<p>画像の下に続く本文です。画像が読み込まれると、この段落が下に押し出されます。</p>
I ran Lighthouse on this page. From the JSON result, a small script (summarize.mjs) pulled out the performance score, the CLS, and the result for this audit (named unsized-images inside Lighthouse).
$ npx lighthouse http://localhost:8080/before.html --only-categories=performance --output=json --output=html --output-path=./out/before --chrome-flags="--headless=new --no-sandbox" --quiet
exit: 0
lighthouseVersion: 13.6.0
requestedUrl: http://localhost:8080/before.html
performance score: 100
CLS: 0.03 (score 1)
unsized-images: score=0.5 title="Image elements do not have explicit `width` and `height`"
- <img src="/cat.png" alt="水色の猫の顔"> url=http://localhost:8080/cat.png
In the HTML report, the item appeared under "Diagnostics" with an orange square. Expanding it showed a table listing the flagged <img> tag and the image URL. When I ran it with --locale=ja to get the report in Japanese, the item name was translated into Japanese. The Japanese label (in English, roughly "Image elements do not have explicit width and height") is what appeared in the report.

The performance score stayed at 100. As the report's "Unscored" label shows, this audit is not included in the score. In Lighthouse's settings, this audit has a weight of 0. The CLS value (weight 25) is what counts toward the score. On this page the CLS was 0.03, and its score was still 1 (the maximum), so the performance score did not drop.
Until the image arrives, the img has a height of 0
To see what was shifting, I used Playwright to open the page at a 360px-wide viewport. I measured the <img> size and the position of the paragraph below it with getBoundingClientRect(), once before the image arrived (at 0.5 seconds) and once after it loaded. These measurements are separate from Lighthouse's mobile settings (412px wide). Here is the output of the measurement script (measure.cjs).
$ node measure.cjs
before 0.5s: img 0x0, p.top=121, aspect-ratio="auto", complete=false
before loaded: img 360x203, p.top=310, aspect-ratio="auto", complete=true
Until the image arrived, the <img> had a width and height of 0, and the paragraph sat 121px from the top. Once the image loaded, the <img> grew to 360×203, and the paragraph was pushed down to 310px. This movement is the layout shift. The "Layout shift culprits" item in the Lighthouse report also listed the paragraph (body > p) as the element that shifted, with the cause given as "Unsized image element".

The fix: add width and height attributes, and set height: auto in CSS
I gave the <img> the image's original size (640×360) as width and height attributes, and added height: auto to the CSS.
<style>
img { max-width: 100%; height: auto; }
</style>
<img src="/cat.png" alt="水色の猫の顔" width="640" height="360">
When I ran Lighthouse again, the item name changed to "Image elements have explicit width and height", and the flagged image no longer appeared. CLS also dropped to 0.
$ npx lighthouse http://localhost:8080/after-attrs.html --only-categories=performance --output=json --output=html --output-path=./out/after-attrs --chrome-flags="--headless=new --no-sandbox" --quiet
exit: 0
lighthouseVersion: 13.6.0
requestedUrl: http://localhost:8080/after-attrs.html
performance score: 100
CLS: 0 (score 1)
unsized-images: score=1 title="Image elements have explicit `width` and `height`"
In the report, the item had a green circle and moved to "Passed audits".

Measuring the sizes the same way, the <img> was already 360×203 before the image arrived, and the paragraph was at 310px from the start.
after-attrs 0.5s: img 360x203, p.top=310, aspect-ratio="auto 640 / 360", complete=false
after-attrs loaded: img 360x203, p.top=310, aspect-ratio="auto 640 / 360", complete=true
The aspect-ratio shows auto 640 / 360 because the browser calculates the aspect ratio from the width and height attributes. The web.dev article on improving CLS explains that the browser calculates this ratio before the image loads, and that when the width is set by something like width: 100%, the height is derived from that ratio. The same article says that if the attribute numbers don't match the image's actual aspect ratio, a small shift remains after the image arrives. So the attributes should contain the image's real dimensions. The MDN reference for the img element also explains that giving both width and height lets the browser reserve space before the image loads.
Forgetting height: auto makes the image stretch vertically
I also tested a page where I added only the width and height attributes and kept max-width: 100% in the CSS. The Lighthouse warning went away and CLS was 0.
$ npx lighthouse http://localhost:8080/attrs-no-auto.html --only-categories=performance --output=json --output=html --output-path=./out/attrs-no-auto --chrome-flags="--headless=new --no-sandbox" --quiet
exit: 0
lighthouseVersion: 13.6.0
requestedUrl: http://localhost:8080/attrs-no-auto.html
performance score: 100
CLS: 0 (score 1)
unsized-images: score=1 title="Image elements have explicit `width` and `height`"
However, at a 360px-wide viewport, the <img> came out at 360×360. max-width: 100% shrinks the width to 360px, but the height stays at the height="360" value, which remains 360px.
attrs-no-auto 0.5s: img 360x360, p.top=468, aspect-ratio="auto 640 / 360", complete=false
attrs-no-auto loaded: img 360x360, p.top=468, aspect-ratio="auto 640 / 360", complete=true

Lighthouse does not flag this visual distortion. When an image is wider than the screen and you add attributes, also add height: auto in CSS. The web.dev article also shows the pattern of specifying height: auto so that the height is not fixed to a value (360px in the example).
Adding only width does not clear the warning
On the page where I wrote width="640" but no height, the warning remained. Before the image arrived, the <img> was 360×0. The width was set, but the height stayed at 0.
unsized-images: score=0.5 title="Image elements do not have explicit `width` and `height`"
- <img src="/cat.png" alt="水色の猫の顔" width="640"> url=http://localhost:8080/cat.png
width-only 0.5s: img 360x0, p.top=121, aspect-ratio="auto", complete=false
width-only loaded: img 360x203, p.top=310, aspect-ratio="auto", complete=true
Reading the source code for this audit in Lighthouse 13.6.0, the warning is cleared when both the width and height are set, or when one of them is set along with an aspect-ratio in CSS. The width or height can come from either HTML attributes or CSS. However, if the CSS value is auto, initial, unset, or inherit, it is treated as not set. Images with position: fixed or position: absolute, and CSS background images, are not checked to begin with.
When you can't add attributes, reserve space with aspect-ratio in CSS
If it's hard to add attributes to the <img>, for example in HTML output by a CMS, you can fix it with CSS alone. The web.dev article also lists a way to reserve space with the CSS aspect-ratio property instead of attributes. On a page where I set a width and an aspect-ratio, the warning went away and CLS was 0.
<style>
img { width: 100%; aspect-ratio: 16 / 9; }
</style>
<img src="/cat.png" alt="水色の猫の顔">
$ npx lighthouse http://localhost:8080/after-css.html --only-categories=performance --output=json --output=html --output-path=./out/after-css --chrome-flags="--headless=new --no-sandbox" --quiet
exit: 0
lighthouseVersion: 13.6.0
requestedUrl: http://localhost:8080/after-css.html
performance score: 100
CLS: 0 (score 1)
unsized-images: score=1 title="Image elements have explicit `width` and `height`"
Before the image arrived, the <img> was already 360×203, and the paragraph did not move.
after-css 0.5s: img 360x203, p.top=310, aspect-ratio="16 / 9", complete=false
after-css loaded: img 360x203, p.top=310, aspect-ratio="16 / 9", complete=true
In this example I set 16 / 9 on every img, so images with other aspect ratios are also fit into a 16:9 box. On a page where images with different aspect ratios are mixed together, it's better to give each image its own width and height attributes with its real dimensions.





