Sub topic 11
Putting picture, srcset, and sizes together
This chapter assumes you know basic HTML and CSS, and that you have read the earlier chapters on srcset with w descriptors, sizes, and the picture element. Here you combine them so one image can change its crop, its format, and its file width.
Why combine the techniques
Each technique answers a different question:
| Question | Tool |
|---|---|
| Which crop fits this viewport? | `media` on `source` |
| Which format can this browser decode? | `type` on `source` |
| Which width fits this layout slot and screen density? | `srcset` with `w` descriptors and `sizes` |
You can use all three on one image. The browser answers them in order, and the order explains most mistakes.
A complete example
<picture>
<source
media="(min-width: 50em)"
type="image/webp"
srcset="hero-wide-800.webp 800w, hero-wide-1600.webp 1600w"
sizes="100vw">
<source
type="image/webp"
srcset="hero-narrow-400.webp 400w, hero-narrow-800.webp 800w"
sizes="100vw">
<img
src="hero-narrow-800.jpg"
srcset="hero-narrow-400.jpg 400w, hero-narrow-800.jpg 800w"
sizes="100vw"
width="800"
height="800"
alt="A sailor looking out to sea">
</picture>The first source is the landscape crop in WebP, used on viewports of 50em and wider. The second source is the square crop in WebP, used on every other viewport. The img holds the square crop in JPEG for browsers that support neither.
How the browser decides
The browser works through the source elements from top to bottom and takes the first one whose media condition is true and whose type it supports. A source with no media always matches, so it must come after the ones that have a condition.
Only then does it look inside the chosen source. It uses that source's sizes to find the slot width, multiplies by the device pixel ratio, and picks a candidate from that source's srcset.
Suppose the viewport is 1000 CSS pixels wide on a 2x screen, and the browser supports WebP. The first source matches. sizes="100vw" gives a 1000px slot, so the browser needs about 2000 device pixels. It picks hero-wide-1600.webp, the largest available.
Common errors
- Reordered sources. If the unconditional
sourcecomes first, it always wins and the wide crop is never used. - Missing
sizes. Withoutsizes, the browser assumes100vw. This overestimates any image that fills less of the page. - Mixed crops in one
srcset. Every candidate in asrcsetmust show the same crop. Use a separatesourcefor each crop. - Mismatched
type. A wrong MIME type, such asimage/jpg, makes the browser skip thesource. - Styling the wrong element. The
imgis the rendered element. Put CSS on it, not onpictureorsource.
Exercise
Exercise
A picture has two source elements. The first has type="image/avif" and no media. The second has media="(min-width: 50em)" and type="image/webp". A browser that supports AVIF and WebP loads the page on a 60em-wide viewport. Which source is used, and what change would let the wide crop appear in WebP?
Show answer
The first source is used, because it has no media condition and AVIF is supported, so it matches first. To let the wide crop appear, put the source with media="(min-width: 50em)" first, then the unconditional one.