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:
- px in CSS is the unit of size on the page. A phone screen is usually 360 to 430 CSS px wide.
- DPR (device pixel ratio) is how many physical screen pixels are used for 1 CSS px. A normal screen has a DPR of 1, a sharp phone screen 2 or 3. In the console, check it with
window.devicePixelRatio. - vw (viewport width) is a percentage of the screen width.
100vwmeans as wide as the screen.
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">
srcsetlists the files with their real width in pixels.800wmeans that file is 800 pixels wide. The number must match the actual file.sizessays how wide the image will show: on screens up to 600 px, as wide as the screen (100vw); on wider screens, 600 px. The browser needs this before the CSS has loaded, which is why it's written in the HTML.srcis the fallback for browsers that don't knowsrcset.widthandheightare still written for the ratio, so nothing shifts.
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):
- If the file just under what's needed is nearly enough, Chrome sometimes takes it. A 412 px DPR 2 screen needs 824, and Chrome takes 800w, not 1200w. Firefox takes 1200w.
- If a bigger file was already downloaded on that page, Chrome reuses it. Making the window smaller doesn't start a new download.
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>
type="image/avif"lets a browser that doesn't know AVIF skip that source without downloading it. The newest format goes at the top.- The
<img>at the end is required. It's what actually shows, soalt,widthandheightgo on the<img>, not on a<source>. - Why AVIF and WebP are smaller is covered in the Modern Image Formats lesson.
⚠️ Why does the browser pick the wrong file?
- Forgetting
sizes. The browser assumes100vwand downloads a file that's too big on wide screens. sizesnot matching the CSS. If your CSS makes the image 600 px butsizessays100vw, the pick is wrong. Keep them the same.- Testing only on a DPR 1 laptop. Most phones are DPR 2 to 3, so they need a file bigger than their screen width.
- A
<source>withouttype. A browser that can't open AVIF still picks it, and the image is broken. - A
<picture>without an<img>. Nothing shows at all.
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.