research/lazy-images-without-layout-shift
Lazy-Loaded Images That Do Not Shift the Layout: Width, Height and Aspect-Ratio Tested
Engineering Note · November 16, 2026 · 8 minute read · HTML · CSS · Performance
A lazy-loaded image shifts the page when the browser has no idea how tall it will be until the file arrives. Give it width and height attributes that match the file and add height: auto to your responsive image CSS, or give it a CSS aspect-ratio, and the shift goes away. We measured it. With no dimensions, Chromium recorded layout shift at both desktop and phone widths. With either fix, it recorded none. Two traps showed up along the way. Leaving out height: auto stretches the image on a phone, and attributes with the wrong ratio still shift.
Everything below was run with Node.js 22.23.4, Playwright 1.63.0 and Chromium 153.0.8010.12 (headless) on Linux. The test image was an 800 by 450 JPEG made with ffmpeg 6.1.1. The outputs are the real ones from this run.
What is layout shift, and why do lazy images cause it?
Layout shift is content moving after it has appeared: you start reading a paragraph and it jumps down because something above it finished loading. Browsers measure it, and Chromium reports each shift to a PerformanceObserver as a layout-shift entry with a score.
loading="lazy" tells the browser to wait before fetching an image until it is close to the screen. That saves bandwidth. It also means the image arrives later than the text around it, so the gap between “page drawn” and “image known” is wider. If the browser has reserved no space, the jump happens in front of the reader.
How did we test it?
A small Node.js server served one page and the image. It held every image response for 600 milliseconds, so the page always drew before the image arrived. The page was a heading, the image, then a paragraph:
<!doctype html><meta name="viewport" content="width=device-width">
<style>body{margin:16px;font:16px/1.5 sans-serif} img{display:block;max-width:100%} /* case CSS */</style>
<h1>Heading</h1>
<!-- case <img> -->
<p id="after">The paragraph under the image.</p>
<script>
window.__shifts = [];
new PerformanceObserver(l => { for (const e of l.getEntries()) if (!e.hadRecentInput) __shifts.push(+e.value.toFixed(4)); })
.observe({ type: 'layout-shift', buffered: true });
</script>
A Playwright script loaded each case, read the image’s box size, waited for the image to finish plus 300 milliseconds, read the box again, and summed the shift scores. It ran every case at a 1280-pixel desktop width and a 390-pixel phone width.
The five cases:
| Case | <img> | Extra CSS |
|---|---|---|
| A | loading="lazy", no width or height | none |
| B | width="800" height="450" | img{height:auto} |
| C | width="800" height="450" | none |
| D | no width or height | img{width:100%;aspect-ratio:16/9} |
| E | width="800" height="600" (wrong ratio) | img{height:auto} |
What did the browser record?
Chromium 153.0.8010.12
vw=1280 A no width/height box before 0x0 after 800x450 CLS 0.0206
vw=1280 B width+height, height:auto box before 800x450 after 800x450 CLS 0.0000
vw=1280 C width+height, no height:auto box before 800x450 after 800x450 CLS 0.0000
vw=1280 D CSS aspect-ratio only box before 1248x702 after 1248x702 CLS 0.0000
vw=1280 E wrong ratio attrs (4:3) box before 800x600 after 800x450 CLS 0.0069
vw=390 A no width/height box before 0x0 after 358x201 CLS 0.0277
vw=390 B width+height, height:auto box before 358x201 after 358x201 CLS 0.0000
vw=390 C width+height, no height:auto box before 358x450 after 358x450 CLS 0.0000
vw=390 D CSS aspect-ratio only box before 358x201 after 358x201 CLS 0.0000
vw=390 E wrong ratio attrs (4:3) box before 358x269 after 358x201 CLS 0.0092
The scores are small because the test page is nearly empty: only one short paragraph moves. On a real page with more content under the image, more moves. What matters here is zero against not zero, and the “box before” column, which shows what the browser reserved before the image arrived.
Case A, no dimensions, reserved a 0 by 0 box. When the image arrived, the box grew and the paragraph moved down. Shift recorded at both widths.
Case B, the attributes plus height: auto, reserved exactly the final box at both widths: 800 by 450 on desktop and 358 by 201 on the phone. No shift. The browser used the two attributes as a ratio and scaled the height to the width that CSS allowed.
Case D, CSS aspect-ratio without attributes, also reserved the final box and scored zero. On desktop the box was 1248 pixels wide because the rule said width:100%, so the image was scaled up past its natural 800 pixels. That is the CSS doing what we told it, and a reminder to match the rule to the image.
What are the two traps?
Attributes without height: auto stretch the image
Case C scored zero shift, and on desktop it looked fine. On the phone the box was 358 by 450. The max-width: 100% rule shrank the width, but nothing told the height to follow, so it kept the 450 pixels from the attribute. An 800 by 450 photo drawn into a 358 by 450 box is squashed sideways.
This is the case most likely to be mistaken for success. A layout-shift report will call it perfect. Someone has to look at a phone.
height: auto is the fix (case B). If you would rather crop than scale, object-fit: cover keeps the picture’s proportions inside the fixed box. We tried that too: the box stayed 358 by 450 and the computed object-fit was cover, so the photo was cropped, not stretched.
Attributes with the wrong ratio still shift
Case E declared 800 by 600, a 4:3 shape, for a 16:9 file. The browser reserved a 4:3 box (358 by 269 on the phone), then corrected it to the real 358 by 201 when the file arrived. That correction is a shift: 0.0092 on the phone, 0.0069 on desktop.
This happens when a template hard-codes one size for every image, or when an image is replaced with one of a different shape and the markup is not updated. The attributes do not need to be the file’s exact pixel size, only the same ratio.
Does it matter if the image is below the fold?
We moved the image down the page behind a tall spacer and scrolled to it, at the 390-pixel width. We also recorded whether the browser had requested the image before we scrolled.
== spacer 3000px
A no width/height requested before scroll: yes box 358x201 object-fit fill shifts []
B width+height, height:auto requested before scroll: yes box 358x201 object-fit fill shifts []
C2 width+height, object-fit requested before scroll: yes box 358x450 object-fit cover shifts []
== spacer 5000px
A no width/height requested before scroll: no box 358x201 object-fit fill shifts [0.0139]
B width+height, height:auto requested before scroll: no box 358x201 object-fit fill shifts []
C2 width+height, object-fit requested before scroll: no box 358x450 object-fit cover shifts []
== spacer 8000px
A no width/height requested before scroll: no box 358x201 object-fit fill shifts [0.0139]
B width+height, height:auto requested before scroll: no box 358x201 object-fit fill shifts []
C2 width+height, object-fit requested before scroll: no box 358x450 object-fit cover shifts []
At 3,000 pixels down, Chromium fetched the “lazy” image before we scrolled, so case A’s box grew while it was off screen and nothing was reported. At 5,000 and 8,000 pixels, the browser waited for the scroll. Case A then grew in view and recorded a shift. Cases B and C2 recorded none at any distance.
So whether a missing dimension hurts depends on how far down the image is and how far ahead the browser fetches. We saw the change somewhere between 3,000 and 5,000 pixels in headless Chromium on a local network. We did not pin down the exact distance, and it may differ with network conditions and between browsers. The safe habit does not depend on it: set the dimensions on every image.
What should we remember?
- Put
widthandheighton every<img>, in the same ratio as the file. - In CSS, pair
max-width: 100%withheight: auto. Without it, phones get a stretched image and no warning. - Use
aspect-ratiowhen you know the shape but not the size, such as a card grid that always crops to 16:9. - Use
object-fit: coverwhen the box shape should win over the file’s shape. - Check on a phone width. Case C scored a clean zero and was still wrong.
Limitations
- We tested headless Chromium only. We did not run Firefox or Safari, so we make no claim about them.
- The image delay was a fixed 600 milliseconds on a local server. We did not test real networks, slow connections or a CDN.
- The shift scores come from a nearly empty page and are not comparable to a real page’s Core Web Vitals report. We used them to tell zero from non-zero.
- We tested one image per page and one 16:9 file. We did not test
srcsetwith candidates of different shapes or<picture>art direction, where each source may need its own dimensions. - We did not find the exact distance at which Chromium starts fetching lazy images.
Related reading
- Website speed is a product feature, not a technical detail
- Autoplaying a background video only while it is on screen
- Generating Open Graph images from the same component that renders the page
Frequently asked questions
Why does a lazy-loaded image cause layout shift?
Until the file downloads, the browser does not know how tall the image is. With no width and height, it reserves no space, and everything below jumps down when the image arrives. In our Chromium test the image box was 0 by 0 before loading and 358 by 201 pixels after, and the paragraph under it moved.
Do I still need width and height attributes if my CSS makes images responsive?
Yes. The browser uses the two attributes to work out the image's aspect ratio before it loads. Pair them with max-width:100% and height:auto in CSS. In our test that combination reserved the right box at 1280 and 390 pixels wide and recorded no layout shift.
Why is my image stretched on mobile after adding width and height?
The attributes set a fixed height of their own. If your CSS shrinks the width with max-width:100% but does not set height:auto, the height stays at the attribute value. In our test an 800 by 450 image rendered at 358 by 450 on a 390-pixel screen, visibly squashed sideways.
Can I use CSS aspect-ratio instead of width and height?
Yes, if you know the ratio. A rule like aspect-ratio:16/9 with width:100% reserved the right box before the image loaded and recorded no shift in our test. The attributes are the easier habit because they travel with each image, and many tools write them for you.
Does the shift still count if it happens off screen?
In our test, layout shifts from an image that loaded before it scrolled into view were not reported. When the image was far enough below the fold that the browser waited until the visitor scrolled near it, the missing dimensions did produce a reported shift. Do not rely on the off-screen case; set the dimensions everywhere.