The HTML srcset attribute
Quick answer
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
Frequently asked questions
What does the srcset attribute do?
Lists responsive image candidates.