Lazy Loading Images: Faster Pages With One Attribute
loading=lazy defers an offscreen image. Learn the attribute, the LCP trap, the placeholder, and the sizes attribute.
By Velloreth
The attribute
<img src="photo.jpg" loading="lazy" alt="A description" width="800" height="600">
It defers the load until the image is near the viewport.
The browser decides the distance, which is a few hundred pixels.
It is supported everywhere, and it needs no JavaScript.
The LCP trap
The largest image above the fold is often the LCP element.
Deferring it delays the largest contentful paint, which is the opposite of the goal.
<!-- The hero image: eager, high priority -->
<img src="hero.jpg" fetchpriority="high" alt="..." width="1200" height="600">
<!-- Below the fold: lazy -->
<img src="photo.jpg" loading="lazy" alt="..." width="800" height="600">
The rule: lazy load below the fold, and load the hero eagerly.
The dimensions
Always set width and height.
<img src="photo.jpg" width="800" height="600" alt="...">
The browser reserves the space from the ratio, so the layout does not shift.
The CSS alternative:
img {
aspect-ratio: 4 / 3;
width: 100%;
height: auto;
}
Without the dimensions, the page shifts when the image arrives, which is a CLS problem.
The responsive source
<img
src="photo-800.jpg"
srcset="photo-400.jpg 400w, photo-800.jpg 800w, photo-1600.jpg 1600w"
sizes="(max-width: 600px) 100vw, 50vw"
loading="lazy"
alt="..."
width="800"
height="600"
>
srcset lists the sources with their widths.
sizes tells the browser how wide the image will be displayed.
Without sizes, the browser assumes the full viewport and may download a larger file.
The formats
| Format | Notes |
|---|---|
| AVIF | the smallest, a slower encode |
| WebP | a good balance, widely supported |
| JPEG | universal, larger |
| PNG | lossless, for a graphic |
| SVG | for an icon or a logo |
A <picture> element serves a modern format with a fallback.
<picture>
<source srcset="photo.avif" type="image/avif">
<source srcset="photo.webp" type="image/webp">
<img src="photo.jpg" alt="..." width="800" height="600" loading="lazy">
</picture>
The placeholder
A low-quality image, a solid colour, or a blurred preview.
A dominant-colour placeholder is cheap and effective.
img {
background-color: #d9d9d9;
}
A blurred preview is a tiny image scaled up, which is a larger payload.
The goal is to avoid an empty space and a shift.
The decoding
<img src="photo.jpg" decoding="async" alt="...">
decoding="async" lets the browser decode off the main thread.
fetchpriority="high" raises the priority of the hero image.
fetchpriority="low" lowers it for a decorative image.
The measurement
The LCP is the largest contentful paint, and an image is often the element.
The CLS is the cumulative layout shift, and a missing dimension is a common cause.
The tools:
- Lighthouse
- The Chrome DevTools performance panel
- The WebPageTest filmstrip
- The CrUX report
Measure before and after, because a change that helps one page may hurt another.
Common mistakes
The hero image lazy loaded. A delayed LCP.
No width and height. A layout shift.
No sizes. An oversized download.
A huge source image. A slow load.
No modern format. A larger file.
A placeholder that is a large image. A wasted payload.
Every image eager. A slow initial load.
A lazy image in a carousel. It may not load until it is visible.
No alt. An accessibility failure.
No measurement. The change is a guess.
A quotable summary
loading="lazy" defers an offscreen image until it is near the viewport, which reduces the initial bytes and improves the load time. Never lazy load the LCP image, because deferring it delays the largest paint, and always set width and height so the browser reserves the space and the layout does not shift. The sizes attribute tells the browser which source to pick, and a placeholder prevents an empty space.
Related reading
For the related topics, see improve-inp-core-web-vitals-static-site and code-splitting-guide. For the broader context, how-cdns-actually-work and javascript-performance-tips.