Full answer
A single img src sends the same file to every device. With srcset, you list several versions and describe them. The browser then picks one.
There are two ways to describe candidates. Density descriptors (1x, 2x) suit fixed-size images such as a logo. Width descriptors (480w, 800w) suit images whose displayed size changes. With width descriptors, you also write sizes, which says how wide the image will be on the page at different viewport widths.
The browser works like this: read sizes and take the first condition that matches, to get the slot width in CSS pixels. Multiply by the device pixel ratio to get the pixels needed. Then choose a candidate with enough width, usually the smallest that is large enough. The browser may deviate, for example by reusing a file it already cached, so the choice is a hint, not a promise.
Measured in Chrome with test files: at a 400px viewport and 1x screen, the slot is 400px, so campus-480.jpg is chosen. At 400px and 2x, 800px are needed, so campus-800.jpg is chosen. At a 1000px viewport, the slot is 400px and the 480w file is chosen again.
Use picture when you need control rather than a hint: art direction (a tight crop on small screens), or different formats with a fallback. The img inside picture is required, and it carries the alt text and size attributes.
Always set width and height (or CSS aspect-ratio) so the browser reserves space before the image arrives and the page does not jump.
<style>
body { margin: 0; }
img { display: block; max-width: 100%; height: auto; }
</style>
<img id="photo"
src="campus-800.jpg"
srcset="campus-480.jpg 480w, campus-800.jpg 800w, campus-1200.jpg 1200w"
sizes="(min-width: 900px) 400px, 100vw"
width="1200" height="800"
alt="Students walking across the campus lawn">
<picture>
<source media="(min-width: 700px)" srcset="banner-wide.webp" type="image/webp">
<img id="banner" src="banner-narrow.jpg" width="600" height="300" alt="Admissions open">
</picture>The image files are placeholders. For the test, a local server returned a small image for every file name, and img.currentSrc was read at 400px (1x and 2x) and 1000px viewports. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- x descriptors suit fixed-size images; w descriptors with sizes suit fluid images.
- The browser reads sizes to get the slot width, then multiplies by the device pixel ratio.
- The choice is a hint; the browser may reuse a cached file.
- picture with source elements gives art direction and format fallbacks.
- Set width and height on img so the browser reserves space before loading.
Common mistakes
- mixing x and w descriptors in the same srcset. The list must use one kind only.
- using picture just for responsive sizing. srcset and sizes on a plain img are simpler, and the browser still picks well.
Follow-ups
- why set width and height on an img when CSS sets the size?
- When would you choose picture over srcset?
- How do width and height attributes help Core Web Vitals?
Tips
- Describe the real displayed width in sizes. If sizes is missing, the browser assumes 100vw, and it may download a file larger than needed.
- Work through one numeric example, such as a 400px slot on a 2x screen needing 800 pixels. It proves you know the algorithm.
- Mention that a missing sizes attribute is treated as 100vw.