The HTML srcset attribute
In short
The HTML srcset attribute provides a set of candidate images for the browser to choose from based on screen density or width. It is used on the <img> and <source> elements.
Overview
The srcset attribute provides a set of candidate images for the browser to choose from based on screen density or width. It applies to the <img> and <source> elements.
Pair width descriptors (w) with the sizes attribute so the browser can pick the best file before layout.
Syntax
<img src="m.jpg" srcset="s.jpg 480w, l.jpg 1080w" sizes="(max-width:600px) 480px, 1080px" alt="">
Values
| Value |
|---|
A comma-separated list of "URL descriptor" pairs, e.g. small.jpg 480w, large.jpg 1080w or 1x/2x. |
Best practices
- Make each
wvalue insrcsetthe file's real pixel width. If a 400-pixel file carries an 800w label and no CSS width is set, it shows at half size, because the browser divides w by the slot size to get a density. - Stick to one descriptor type within a srcset, and don't repeat a value. A URL with no descriptor counts as
1xand clashes with an explicit1xentry. - Add the src file to srcset when you use
wdescriptors, because the browser leavessrcout of the choice then. Withxdescriptors,srccounts as1xunless srcset has its own1xentry. - Percent-encode spaces in file names as
%20. Whitespace ends a candidate's URL, which turnsmy photo.jpg 2xinto a broken candidate that the browser drops. - Inspect the image's
currentSrcproperty to see which candidate loaded. The choice is implementation-defined, so the same markup can fetch different files on different screens and browsers.
Frequently asked questions
What does the srcset attribute do?
Lists responsive image candidates.
How do I make images responsive?
How do I stop images causing layout shift?
Which elements use the srcset attribute?
It is an element-specific attribute, used on the
<img> and <source> elements.






















