Sub topic 15
Responsive images with srcset and picture
This chapter assumes you can export images at 1x and 2x, know the differences between JPEG, PNG, WebP, and AVIF, and can read basic HTML. You do not write the markup in most teams. You do need to know what it does, because it decides which files you must export and how they must be named and sized.
The problem responsive images solve
A single exported file is a compromise. A 1600-pixel-wide hero image looks sharp on a large laptop but wastes bandwidth on a phone with a 390-pixel-wide screen. A 400-pixel file loads fast on the phone but looks soft on a large display.
HTML lets you offer several files and lets the browser choose. There are two separate jobs:
- Resolution switching: the same image at different pixel sizes. The browser picks the one that fits the screen and its density. This uses
srcsetandsizes. - Art direction and format switching: different crops or different file formats. This uses the
pictureelement.
Keep the two jobs apart. Most mistakes come from using one tool for the other's job.
Density descriptors: fixed-size images
Use a density descriptor (1x, 2x) when an image always displays at the same CSS size, such as a logo or an avatar.
<img
src="logo.png"
srcset="logo.png 1x, [email protected] 2x"
width="120"
height="40"
alt="Acme logo">The width and height attributes give the displayed size in CSS pixels. The browser picks logo.png or the double-size [email protected] based on screen density. So logo.png is 120 × 40 pixels and [email protected] is 240 × 80 pixels. The src attribute is the fallback for browsers that do not understand srcset.
Width descriptors: fluid images
When an image changes size with the layout, a density descriptor is not enough, because the browser cannot know how wide the image will be. Use width descriptors (w) together with sizes.
<img
src="hero-800.jpg"
srcset="hero-400.jpg 400w, hero-800.jpg 800w, hero-1600.jpg 1600w"
sizes="(min-width: 960px) 800px, 100vw"
width="800"
height="450"
alt="Team working at a shared desk">The w descriptors state each file's pixel width. The sizes attribute states how wide the image is displayed, so the browser can choose the smallest file that still looks sharp.
Read sizes from left to right. The browser uses the first condition that is true:
- If the viewport is at least 960 pixels wide, the image displays at 800 CSS pixels.
- Otherwise, it displays at
100vw, the full viewport width.
How the browser chooses
The browser does this arithmetic:
- Read the display width from
sizes. - Multiply by the device pixel ratio (DPR).
- Pick the smallest file whose
wvalue is at least that number.
A phone with a 390 px viewport and DPR 3 needs 390 × 3 = 1170 pixels. The smallest file that is wide enough is hero-1600.jpg. A laptop with a 1440 px viewport and DPR 1 needs 800 pixels, so it takes hero-800.jpg. The same laptop with a DPR 2 display needs 1600 pixels.
This shows why more files do not always mean smaller downloads: high-density phones often need the large file.
Format switching with picture
The picture element lets the browser choose by format. It contains one or more source elements and a final img.
<picture>
<source type="image/avif" srcset="hero.avif">
<source type="image/webp" srcset="hero.webp">
<img src="hero.jpg" width="800" height="450" alt="Team working at a shared desk">
</picture>The browser uses the first source whose format it supports and falls back to the JPEG in the img element. Order matters: put the format you most want to serve first, and keep the img last because it is both the fallback and the element that carries alt, width, and height.
You can combine both techniques. Each source can have its own srcset and sizes, so you get several widths in each format. That is the most complete setup, and also the one that asks the most of your export work: 3 widths × 3 formats is 9 files for one image.
Art direction
The media attribute switches to a different crop at a given viewport width. A wide banner can become a tight square crop on phones, so the subject stays visible.
<picture>
<source media="(max-width: 599px)" srcset="banner-square.jpg">
<img src="banner-wide.jpg" width="1200" height="400" alt="Runners at the start line">
</picture>This is a design decision, not a compression one. Resizing alone cannot fix a crop where the subject becomes tiny.
Choosing the right tool
| Situation | Use | Files to export |
|---|---|---|
| Logo or icon at one fixed size | `srcset` with `1x, 2x` | 2 |
| Image that scales with the layout | `srcset` with `w` and `sizes` | 3 to 5 widths |
| Want AVIF or WebP with a fallback | `picture` with `type` | 1 per format, or per width and format |
| Different crop on small screens | `picture` with `media` | 1 per crop |
| Vector icon or logo | SVG, no `srcset` | 1 |
Common errors
- Missing
sizes. Without it, the browser assumes the image is100vwwide and may download a file far larger than needed. - Wrong
wvalues. Thewnumber must equal the file's real pixel width. If it does not, the browser's choice is based on false data. - Missing
widthandheight. The browser cannot reserve space, and the page jumps when the image loads (layout shift). - Using
picturefor resolution switching. It works, but it forces the browser to follow your rules instead of choosing by bandwidth and density. Prefersrcset. - Putting
alton asource. Only theimgtakesalt. - Serving a 2x file to a 1x screen on purpose. It wastes bytes with no visible gain.
What this means for your export
You do not need to write the markup, but your handoff should state:
- The displayed size in CSS pixels at each layout width.
- The set of widths and formats you exported.
- Which images need a separate crop.
Name files so the width is visible, for example hero-800.jpg and hero-1600.webp. A developer can then paste them into srcset without opening each one.
Exercise
A card image displays at 300 CSS pixels wide on every screen. It is exported as card-300.jpg (300 px wide) and card-600.jpg (600 px wide). Write the img attributes that serve card-300.jpg to standard screens and card-600.jpg to 2x screens. Then explain why width descriptors and sizes are unnecessary here.
Show answer
Use src="card-300.jpg" srcset="card-300.jpg 1x, card-600.jpg 2x" width="300" height="200" (the height depends on the crop; any matching ratio works) plus an alt. The displayed width is fixed at 300 CSS pixels, so the browser only needs screen density to choose. Width descriptors and sizes are for images whose displayed width changes with the layout.
Exercise
An image uses srcset="a-400.jpg 400w, a-800.jpg 800w, a-1600.jpg 1600w" and sizes="100vw". Which file does a phone with a 400 px viewport and DPR 2 pick, and which file does a 1200 px desktop at DPR 1 pick?
Show answer
The phone needs 400 × 2 = 800 pixels, so it picks a-800.jpg. The desktop displays the image at 100vw, which is 1200 pixels, with DPR 1, so it needs 1200 pixels. The smallest file at least that wide is a-1600.jpg.
Exercise
You export hero.avif, hero.webp, and hero.jpg. In a picture element, in what order should the source elements and the img appear, and why?
Show answer
Put the AVIF source first, the WebP source second, and the img with the JPEG last. The browser takes the first source whose type it supports, so the preferred (smallest) format must come first. The img is last because it is the fallback and carries alt, width, and height.