Skip to content
Velloreth
Web Development 3 min read

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

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

FormatNotes
AVIFthe smallest, a slower encode
WebPa good balance, widely supported
JPEGuniversal, larger
PNGlossless, for a graphic
SVGfor 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.

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.

Frequently asked questions

What does loading=lazy do?
It tells the browser to defer the load of an image until it is near the viewport, rather than loading it with the page. It reduces the initial bytes and the main-thread work, which improves the load time. It is supported in every modern browser.
Should I lazy load every image?
No. The largest image above the fold, which is often the LCP element, must load eagerly, because deferring it delays the largest contentful paint. Lazy load the images below the fold, and load the hero image eagerly with a high priority.
Why does my page shift when an image loads?
Because the image had no reserved space, so the layout reflows when it arrives. Set the width and height attributes, or an aspect-ratio in CSS, so the browser reserves the space before the image loads. That removes the layout shift.
What is the sizes attribute?
It tells the browser how wide the image will be displayed at each viewport, so it can pick the right source from a srcset. Without it, the browser assumes the full viewport width and may download a larger file than needed.
What is a placeholder?
A low-quality image, a solid colour, or a blurred preview shown while the real image loads. It prevents an empty space and a layout shift. A dominant-colour placeholder is cheap and effective.
Does lazy loading help Core Web Vitals?
It helps the LCP when the deferred images are below the fold, because the initial bytes are smaller. It hurts the LCP if the LCP image itself is deferred. It also helps the CLS when the dimensions are set, because the space is reserved.