Responsive images: srcset and sizes
srcset lists a picture at several widths and sizes describes how wide it will render, so the browser can pick one file from the viewport and pixel density before CSS loads. A strong answer names who decides and from what.
What it is
Responsive images solve two different problems. The first is resolution switching: the same picture should arrive as a smaller file on a small or low-density screen and as a larger file where it renders wide or on a dense display. The second is art direction: a wide landscape shot that works on a laptop may need a tighter, portrait crop on a phone so the subject stays visible. srcset and sizes on an img element handle the first. The picture element handles the second.
srcset is a comma-separated list of candidates. Each is a URL, here followed by a width descriptor such as 800w, which states the file's intrinsic width in pixels (the w unit, not px). sizes is a comma-separated list of media conditions, each paired with the width the image slot will take when that condition holds, for example (width <= 600px) 480px. The browser uses the first condition that matches and stops reading; the last entry has no condition and acts as the default. A slot width can be any CSS length, including vw, em and calc(), but not a percentage, and relative units resolve against the document root rather than the image. When srcset uses width descriptors and sizes is missing, the browser assumes 100vw. You still write src: browsers without srcset support load it, and with width descriptors present it is otherwise ignored.
<img
src="photo-1200.jpg"
srcset="photo-600.jpg 600w,
photo-1200.jpg 1200w,
photo-2000.jpg 2000w"
sizes="(min-width: 1100px) calc(60vw - 3rem),
100vw"
width="2000" height="1333"
alt="Two kayaks on a still lake at dawn" />The reason this lives in HTML rather than CSS or script is timing. Browsers start requesting images while parsing the markup, often before the CSS has been fetched, and well before it has been applied. At that moment the browser knows the viewport width, the display's pixel density and user preferences, and nothing about the layout. sizes is how you tell it, in viewport terms, how wide the image will end up. If you instead loaded a default image and swapped it with JavaScript once you had measured the layout, the first file would already be on its way, and the swap would add a second download.
Here is the calculation the browser runs, with web.dev's worked numbers. Take sizes="80vw" and candidates of 600, 1200 and 2000 pixels. On a 1000 px viewport the slot is 800 px, so the candidates work out to 0.75, 1.5 and 2.5 times the slot: each file has an effective density for this viewport. The 600 px file is dropped because it would need upscaling; a 1x screen takes the 1200 px file as the closest match at or above 1x, and a 2x screen takes the 2000 px file. Shrink the viewport to 600 px and the slot is 480 px: the same files now rate 1.25, 2.5 and 4.17, so a 1x phone gets the 600 px file and a 2x phone the 1200 px one. A dense small screen can therefore download a larger file than a wide screen at 1x.
The HTML specification deliberately leaves the final pick to the browser. It may choose a lower density candidate on a slow connection or when the person has asked to save data, and in most browsers it won't request a smaller file when a larger one for the same image is already in its cache, since it can scale that one down. Treat sizes as a description and the result as the browser's decision, not a promise of a particular file.
When the image has a fixed CSS width on all screens, such as a 320 px avatar or a thumbnail in a grid of fixed cells, use density descriptors instead: srcset="a-320.jpg, a-480.jpg 1.5x, a-640.jpg 2x" with no sizes. The src file counts as the 1x candidate, and the browser matches the display's density directly. Width and density descriptors don't mix in one srcset, and sizes pairs with width descriptors, not density ones.
For lazily loaded images there is a shortcut. sizes="auto" with loading="lazy" asks the browser to use the image's laid-out width, which it knows by the time a lazy image loads, instead of a hand-written list. If auto can't be resolved (an older browser, or an image not yet laid out), the browser falls back to any sizes listed after it, then to the width and height attributes. So write sizes="auto, (max-width: 30em) 100vw, 50vw" and set width and height to the largest candidate's dimensions so the space is reserved at the right aspect ratio.
The picture element wraps one or more source elements and a final img, which is required and, in MDN's example, carries src and alt. A source with media="(width < 800px)" is a command rather than a hint: the browser takes the first source whose condition matches and uses its srcset. A source with type="image/avif" is skipped by browsers that can't decode that format, so you can list AVIF, then WebP, then a JPEG img as the fallback. Each source can carry its own srcset with descriptors, so a cropped source can still offer 1x and 2x files. The alt text is shared across all sources, so it has to describe both crops.
When to use it
Use width descriptors plus sizes for content photos whose rendered width follows the layout: a feed image that spans the viewport on a phone and a third of it in a three-column grid on a laptop. Write sizes from your CSS breakpoints, with calc() to subtract fixed gutters, and remember that the widest viewport may not get the largest file: a column in a wide layout can be narrower than a full-width phone slot.
How many files to keep per photo is a storage-versus-bytes trade-off. web.dev reports that 3 to 5 widths is common: more widths let each screen download closer to what it needs, at the cost of more stored files and longer markup. Generating the srcset is easy to automate in an upload pipeline or an image service; sizes is harder, because its correct value depends on the rendered layout, and tools exist to check a sizes attribute against the real page.
Reach for the picture element when the crop, aspect ratio or format changes, not just the size: a tall crop for phones, a wide one for desktops, or AVIF with a JPEG fallback. Give each source its own width and height when the crops have different shapes, so the browser reserves the right box. For the loading and decoding side of the same img, see Loading images.
When not to
A small icon or logo that renders at one fixed size rarely needs any of this; web.dev's design course singles out icons as the case where neither srcset nor picture may be needed. A single file, or an SVG, is simpler to keep correct. Our advice: add density descriptors to a fixed-size raster image when it visibly blurs on dense screens, and stop there.
Don't use picture with media conditions just to resize the same picture. Each matched source is a command, so you end up pinning the choice to your breakpoints instead of describing the slot and letting the browser weigh the whole list. MDN's guide reserves media for art direction, and warns against also putting media conditions in sizes when you do use it. Plain resizing belongs in srcset and sizes on the img element.
Don't try to choose the file in JavaScript after measuring the layout. The browser's preloader has usually requested the src image before your script runs, so you pay for two downloads instead of one. Likewise, a decorative image with no meaning belongs in CSS, where background-image with image-set() offers density candidates in the same spirit as srcset.
Compared
| Approach | What you write | Pick it when | Where it falls short |
|---|---|---|---|
Single | One file | An icon or logo at one small fixed size | All screens download the same bytes; a small file can blur on dense displays, a large one wastes data on small ones |
|
| The image has a fixed CSS width on all screens | Doesn't follow a slot that changes with the layout |
| Widths per file, slot width per breakpoint | The same picture fills a fluid slot |
|
|
| Below-the-fold images where hand-written | Not for eager, above-the-fold images; needs fallbacks for older browsers |
| A source per crop, | The crop or shape changes per viewport | A command, not a hint; one |
| A source per format | You want AVIF or WebP with a JPEG fallback | One encoded file per format per width, stored or generated on demand |
CSS | Density candidates in | The image is decorative | No |
In a picture
Step 1 of 4
The tag is parsed
The browser meets an img whose sizes says 80vw and whose srcset offers three files at 600w, 1200w and 2000w. The CSS may not have arrived yet, so it works from the viewport, the display and the sizes hint.