DEV Community

Cover image for Do width and height on img stop layout shift? I measured it
Sahadat Husain
Sahadat Husain

Posted on

Do width and height on img stop layout shift? I measured it

Yes. In Chromium, an image that arrived 600 ms late pushed the next paragraph down 183 px on a 390 px phone and scored a CLS of 0.0677. The same page with width="1280" height="717" on the <img> scored 0 and moved nothing. A CSS aspect-ratio rule did the same job. Only width without height did nothing.

Why does an image shift the layout?

Before the file arrives, the browser does not know the image's shape, so the box is empty. When the file lands, the box grows and everything below moves. MDN says that giving both width and height lets the browser compute the aspect ratio before loading and reserve the space. web.dev rates a CLS of 0.1 or less as good and above 0.25 as poor.

How I tested it

A page with a paragraph, one image and another paragraph. The server holds the image back for 600 ms. I log layout-shift entries with a PerformanceObserver and record where the lower paragraph sits before and after.

await page.addInitScript(() => {
  window.__cls = 0;
  new PerformanceObserver((l) => {
    for (const e of l.getEntries()) if (!e.hadRecentInput) window.__cls += e.value;
  }).observe({ type: 'layout-shift', buffered: true });
});
await page.goto(url, { waitUntil: 'commit' });
await page.waitForSelector('#below');
const top0 = await page.evaluate(() => document.getElementById('below').getBoundingClientRect().top);
await page.waitForFunction(() => document.getElementById('i').complete);
await page.waitForTimeout(300);
const { cls, top } = await page.evaluate(() => ({
  cls: window.__cls,
  top: document.getElementById('below').getBoundingClientRect().top,
}));
Enter fullscreen mode Exit fullscreen mode

The page CSS was img { max-width: 100% } in a 720 px column. I ran each variant five times at 390 px and 1280 px wide. All five runs of each variant gave the same CLS.

What did each markup do?

CLS and paragraph movement per markup, at 390 and 1280 pixels

  • No attributes: CLS 0.0677 at 390 px (paragraph moved 183 px), 0.0305 at 1280 px (385 px).
  • Only width="1280": the same as no attributes. A width alone gives no ratio.
  • width="1280" height="717", with height: auto in CSS: CLS 0 at both widths. Nothing moved.
  • CSS aspect-ratio: 1280/717, no attributes: CLS 0 at both widths.
  • Wrong ratio, width="1280" height="1280": CLS 0.0584 at 390 px, and the paragraph moved up 157 px. The box was reserved too tall, then shrank.

The final image height was 201 px at 390 px wide and 403 px at 1280 px wide in every variant. The attributes only change what happens before the file arrives.

Note: height: auto in CSS matters. Without it, a responsive image with max-width: 100% keeps the attribute height and gets squashed. Write the attributes, then let CSS scale the height: img { max-width: 100%; height: auto; }.

Is 0.0677 a problem?

On its own it sits under the 0.1 line. This page has one image. A real page has a hero, a card grid and a footer logo, and each late image adds its own shift. I did not sum a multi-image page, so treat the table as the cost per image.

What about lazy images?

MDN notes that unloaded lazy images have a width and height of 0, which makes the attributes especially important with loading="lazy". I did not measure that combination here.

Where do you get the numbers?

Read them from the file you serve, not from a guess. With sharp:

const { width, height } = await sharp('p-1280.jpg').metadata();
console.log(width, height);
Enter fullscreen mode Exit fullscreen mode
1280 717
Enter fullscreen mode Exit fullscreen mode

Print them into the template at build time. If you resize with a script, read the metadata of the output, not the source. A source of 4964 × 2782 and an output of 1280 × 717 have the same ratio, but rounding can differ by a pixel, and the browser only needs the ratio to be close. Keep the one ratio for all sizes of the same image.

What should you do?

  1. Write the real pixel width and height of the file you serve on every <img>.
  2. Add height: auto to your image CSS.
  3. When you serve several crops, give each <source> the same ratio or set the ratio in CSS.
  4. Check by throttling the network in DevTools and watching the text below the image.

If you generate many sizes, keep the ratio fixed when you resize. The Blinkimg API returns the output dimensions, so you do not have to guess them.

How I measured

  • Apple Silicon Mac, 2026-10-18. Node 24.22.0, Playwright 1.61.1, Chromium 149.0.7827.55.
  • Image: p-1280.jpg, 79,671 bytes, 1280 × 717, made with sharp 0.35.4 from a 4964 × 2782 camera photo. The server delayed it by 600 ms.
  • CLS is the sum of layout-shift values without recent input, median of 5 runs. Raw runs: measured.json.

What I did not test

I did not test Firefox or Safari, real network conditions, or a page with more than one image.


I build Blinkimg, an image compressor and converter with a developer API. I wrote this article with help from an AI assistant (Claude), which ran the code on my machine; every number in it comes from those runs.

Top comments (0)