Responsive Images — Performance

srcset lists the files with their widths (480w, 800w, 1200w, 1600w), and sizes says how wide the image shows ((max-width: 600px) 100vw, 600px). The browser…

One hero file for every screen isn't fair. A laptop with a sharp screen does need a wide image. But a phone 375 px wide downloads the same file too, hero-1600.jpg at about 320 KB, while the 800-pixel file at about 80 KB is already sharp enough on its screen.

The way out is responsive images: you offer several versions of the same image and let the browser pick the best one for the visitor's screen. A small phone gets a small file, a laptop with a sharp screen gets a big one. It's like buying a T-shirt from a size chart with S, M, L and XL: you know how wide you are, so you take the smallest size that still fits. Too small doesn't fit, too big wastes fabric.

srcset and sizes

Three terms first:

To look sharp, an image 375 CSS px wide on a DPR 2 screen needs a file 375 × 2 = 750 pixels wide. Here's the image from the Lab:

<img
  src="hero-800.jpg"
  srcset="hero-480.jpg 480w,
          hero-800.jpg 800w,
          hero-1200.jpg 1200w,
          hero-1600.jpg 1600w"
  sizes="(max-width: 600px) 100vw, 600px"
  width="1200" height="900"
  alt="Rice fields in the morning">

How the browser picks

The browser works out the display width from sizes, multiplies it by the DPR, then takes the smallest file that's big enough:

Screen DPR Display width (from sizes) Needs Picked
375 px 2 375 px 750 pixels hero-800.jpg, about 80 KB
375 px 3 375 px 1,125 pixels hero-1200.jpg, about 180 KB
1280 px 1 600 px 600 pixels hero-800.jpg, about 80 KB
1280 px 2 600 px 1,200 pixels hero-1200.jpg, about 180 KB

The phone in the first row needs 750 pixels, so the smallest file that's big enough is 800w.

Two small things Chrome does (you can see them in the Lab):

Without sizes, the browser assumes the image is as wide as the screen (100vw). On a 1280 px laptop with DPR 1, it needs 1,280 pixels and downloads hero-1600.jpg (about 320 KB) for a box that's only 600 px. Four times heavier than needed.

Other options for sizes:

Way When to pick it
A list of sizes, (max-width: 600px) 100vw, 600px the default in this lesson: it works in every browser, and it's the one for images on the first screen
sizes="auto, (max-width: 600px) 100vw, 600px" plus loading="lazy" lazy images below the first screen. The browser uses the image's width after layout, so sizes doesn't have to match your CSS. In Chrome 126, Firefox 150, Safari 27. Older browsers use the list after auto,

auto is only for loading="lazy" images. In Chromium 156 (a 1280 px screen, DPR 1), the same image in a 300 px box downloaded hero-480.jpg when lazy, but hero-1600.jpg when not lazy.

For an image with a fixed size, say a logo 200 px wide, the x descriptor is enough: srcset="logo.png 1x, logo-2x.png 2x".

<picture>: a different image or a different format

srcset only picks a size of the same image. When the image itself changes, use <picture>. The browser reads each <source> from the top, takes the first one that fits, and if none fits it uses the <img> at the end.

Art direction, a different crop per screen, for example a wide photo on desktop and a portrait crop on phones:

<picture>
  <source media="(max-width: 600px)" srcset="hero-portrait.jpg">
  <img src="hero-wide.jpg" alt="A farmer in the rice field">
</picture>

Format choice, the same file in a leaner format, for browsers that support it:

<picture>
  <source srcset="hero.avif" type="image/avif">
  <source srcset="hero.webp" type="image/webp">
  <img src="hero.jpg" width="1200" height="900" alt="Cover photo">
</picture>

⚠️ Why does the browser pick the wrong file?

TL;DR: srcset lists the files with their widths (480w, 800w, 1200w, 1600w), and sizes says how wide the image shows ((max-width: 600px) 100vw, 600px). The browser multiplies the display width by the DPR and picks the smallest file that's big enough: a 375 px screen at DPR 2 needs 750, so 800w. Without sizes, the browser assumes 100vw and downloads too much. srcset is for different sizes of the same image. <picture> is for a different image (art direction, with media) or a different format (with type, AVIF first, then WebP), always with an <img> at the end as the fallback.

⚡ Several sizes, the browser picks

Write:

<img
  src="hero-800.jpg"
  srcset="hero-480.jpg 480w,
          hero-800.jpg 800w,
          hero-1200.jpg 1200w,
          hero-1600.jpg 1600w"
  sizes="(max-width: 600px) 100vw, 600px"
  width="1200" height="900"
  alt="Rice fields in the morning">

Meaning: srcset offers several versions of the same image at different resolutions, 800w meaning the file is 800 pixels wide. sizes says how wide the image shows, and the browser picks the one that fits.

⚡ Display width × DPR

Guess first: a 375 px phone screen with DPR 2. Which file gets picked?

hero-800.jpg. It needs 375 × 2 = 750 pixels, and the smallest file that's big enough is 800w.

Meaning: Without sizes, the browser assumes the image is as wide as the screen (100vw). On a 1280 px laptop it downloads hero-1600.jpg (about 320 KB) for a box that's only 600 px.

⚡ A different image or a different format

Write:

<picture>
  <source srcset="hero.avif" type="image/avif">
  <source srcset="hero.webp" type="image/webp">
  <img src="hero.jpg" width="1200" height="900" alt="Cover photo">
</picture>

Meaning: Use <picture> when the image itself changes: a different crop with media, or a different format with type, AVIF at the top. The <img> at the end is required as the fallback.

What you will learn