Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 37 ยท Images, Fonts and Media

Responsive Images and Lazy Loading

Serve the right image to each screen with srcset, sizes and the picture element, add format fallbacks, and defer offscreen images with loading=lazy without hurting LCP.

A single image file cannot be right for every visitor. A phone needs a small file; a large desktop with a high-density display needs a bigger one. Responsive images let you offer several versions and let the browser pick the best for the screen, the layout and the connection. Lazy loading then avoids downloading images the visitor may never scroll to.

srcset with width descriptors

srcset lists candidates with their real pixel width (w). sizes tells the browser how wide the image will be in the layout. The browser combines that with the screen's density and chooses.

HTML
<img
  src="/img/cat-800.jpg"
  srcset="/img/cat-400.jpg 400w,
          /img/cat-800.jpg 800w,
          /img/cat-1600.jpg 1600w"
  sizes="(min-width: 900px) 50vw, 100vw"
  width="1600" height="1000"
  alt="A tabby cat asleep on a sofa">
  • sizes reads: "from 900 px viewport width up the image takes half of it; otherwise it fills the viewport".
  • src is the fallback for browsers without srcset.
  • width and height still describe the intrinsic ratio, so no layout shift occurs (CLS).

The browser needs sizes before layout, because it picks an image while parsing HTML. If you leave sizes out, it assumes 100vw, which can make it download a larger file than needed.

JS How the browser picks a candidate

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Real browsers also consider data-saver settings and cache contents, so they may pick differently. The principle holds: they pick the smallest file that still looks sharp.

srcset with density descriptors

For images with a fixed CSS size (logos, avatars), use x descriptors instead of sizes:

HTML
<img src="/img/avatar-48.png"
     srcset="/img/avatar-48.png 1x, /img/avatar-96.png 2x, /img/avatar-144.png 3x"
     width="48" height="48" alt="Maya">

The picture element

<picture> is for two other jobs: format fallbacks and art direction.

Format fallbacks

The browser uses the first <source> whose type it supports:

HTML
<picture>
  <source type="image/avif" srcset="/img/hero.avif">
  <source type="image/webp" srcset="/img/hero.webp">
  <img src="/img/hero.jpg" width="1200" height="600" alt="Sunrise over a lake">
</picture>

Art direction

Serve a different crop on a small screen, not just a smaller version:

HTML
<picture>
  <source media="(max-width: 600px)" srcset="/img/hero-square.avif" type="image/avif">
  <source media="(min-width: 601px)" srcset="/img/hero-wide.avif" type="image/avif">
  <img src="/img/hero-wide.jpg" width="1200" height="500" alt="Sunrise over a lake">
</picture>

The <img> inside is required: it carries alt, size attributes and CSS.

Which source does a picture use?

img.currentSrc tells you which candidate the browser actually chose. It is handy when debugging a srcset.

Lazy loading

Setting loading="lazy" makes the browser delay the download until the image is near the viewport:

HTML
<img src="/img/review-12.jpg" width="600" height="400" loading="lazy" decoding="async" alt="...">
  • Use it for below-the-fold images: galleries, long articles, product lists.
  • Never use it on the LCP image or anything visible at the start. That delays the most important image.
  • Always set width and height so the space is reserved.
  • decoding="async" lets decoding happen off the main thread, reducing jank. Neither attribute changes quality.

The same attribute works on <iframe>, which is ideal for embedded videos and maps.

HTML
<iframe src="https://example.com/embed" loading="lazy" width="560" height="315" title="Demo"></iframe>

Native lazy loading versus JavaScript

Native lazy loading handles almost all cases. If you need custom behaviour (placeholders, different thresholds), use an IntersectionObserver to swap data-src into src.

Lazy load with IntersectionObserver

The rootMargin starts loading 200 px before the element appears, so users rarely see a blank slot.

flowchart TD
  A["Image on the page"] --> B{"Visible at load?"}
  B -->|Yes, LCP| C["Eager + fetchpriority=high"]
  B -->|Below the fold| D["loading=lazy + width/height"]

Eager for what users see first; lazy for the rest.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the sizes attribute describe?

    The browser uses sizes with srcset to select a candidate before layout is known.
  2. 2. Which element do you use for AVIF with a JPEG fallback?

    picture lets the browser choose the first supported format.
  3. 3. Which image should NOT be lazy-loaded?

    Lazy-loading the LCP image delays the most important content.
  4. 4. What does img.currentSrc return?

    It reflects the resolved choice from srcset or picture.

Summary

  • Use srcset with w descriptors and sizes for fluid images; x descriptors for fixed sizes.
  • Use <picture> for format fallbacks and art direction.
  • Lazy-load only below-the-fold images and always set width and height.
  • Keep the LCP image eager with fetchpriority="high".

Next: Web Fonts and Performance