HTML Responsive Images: srcset and sizes
Offer density candidates
Use srcset with 1x and 2x when an image occupies the same CSS size but higher-density displays may need more image pixels. The browser chooses a candidate using device density and other conditions; it is not a guaranteed download rule. Keep an ordinary src fallback.
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22160%22%20height%3D%2280%22%20viewBox%3D%220%200%20160%2080%22%3E%3Crect%20width%3D%22160%22%20height%3D%2280%22%20fill%3D%22%23dbeafe%22%2F%3E%3Cpath%20d%3D%22M0%2080%20L80%2016%20L160%2080Z%22%20fill%3D%22%23166534%22%2F%3E%3C%2Fsvg%3E" alt="Green hill" srcset="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22160%22%20height%3D%2280%22%20viewBox%3D%220%200%20160%2080%22%3E%3Crect%20width%3D%22160%22%20height%3D%2280%22%20fill%3D%22%23dbeafe%22%2F%3E%3Cpath%20d%3D%22M0%2080%20L80%2016%20L160%2080Z%22%20fill%3D%22%23166534%22%2F%3E%3C%2Fsvg%3E 1x, data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22320%22%20height%3D%22160%22%20viewBox%3D%220%200%20320%20160%22%3E%3Crect%20width%3D%22320%22%20height%3D%22160%22%20fill%3D%22%23dbeafe%22%2F%3E%3Cpath%20d%3D%22M0%20160%20L160%2032%20L320%20160Z%22%20fill%3D%22%23166534%22%2F%3E%3C%2Fsvg%3E 2x" width="160" height="80">Both candidates depict the same subject and occupy a 160 by 80 CSS-pixel slot.
Use width descriptors and sizes
A w descriptor states the actual intrinsic width of the candidate, not the intended display width. sizes tells the browser the expected slot width: the first matching condition wins, with the last unconditional length as a fallback. It does not itself set CSS layout. Do not mix w and x descriptors in the same list.
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22160%22%20height%3D%2280%22%20viewBox%3D%220%200%20160%2080%22%3E%3Crect%20width%3D%22160%22%20height%3D%2280%22%20fill%3D%22%23dbeafe%22%2F%3E%3Cpath%20d%3D%22M0%2080%20L80%2016%20L160%2080Z%22%20fill%3D%22%23166534%22%2F%3E%3C%2Fsvg%3E" alt="Green hill" srcset="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22160%22%20height%3D%2280%22%20viewBox%3D%220%200%20160%2080%22%3E%3Crect%20width%3D%22160%22%20height%3D%2280%22%20fill%3D%22%23dbeafe%22%2F%3E%3Cpath%20d%3D%22M0%2080%20L80%2016%20L160%2080Z%22%20fill%3D%22%23166534%22%2F%3E%3C%2Fsvg%3E 160w, data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22320%22%20height%3D%22160%22%20viewBox%3D%220%200%20320%20160%22%3E%3Crect%20width%3D%22320%22%20height%3D%22160%22%20fill%3D%22%23dbeafe%22%2F%3E%3Cpath%20d%3D%22M0%20160%20L160%2032%20L320%20160Z%22%20fill%3D%22%23166534%22%2F%3E%3C%2Fsvg%3E 320w, data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22480%22%20height%3D%22240%22%20viewBox%3D%220%200%20480%20240%22%3E%3Crect%20width%3D%22480%22%20height%3D%22240%22%20fill%3D%22%23dbeafe%22%2F%3E%3Cpath%20d%3D%22M0%20240%20L240%2048%20L480%20240Z%22%20fill%3D%22%23166534%22%2F%3E%3C%2Fsvg%3E 480w" sizes="(max-width: 400px) 160px, 320px" width="320" height="160" style="width: 320px; max-width: 100%; height: auto"><style> @media (max-width: 400px) { img { width: 160px !important; } }</style>At a narrow viewport the described slot is 160 pixels; otherwise it is 320 pixels. The CSS rule matches that slot.
Keep the source choices equivalent
Use size switching for equivalent images. A change of crop or composition is art direction, covered next. When testing, inspect the chosen resource in browser developer tools and use a fresh page at each viewport; cached larger resources may be reused.
<img src="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22160%22%20height%3D%2280%22%20viewBox%3D%220%200%20160%2080%22%3E%3Crect%20width%3D%22160%22%20height%3D%2280%22%20fill%3D%22%23dbeafe%22%2F%3E%3Cpath%20d%3D%22M0%2080%20L80%2016%20L160%2080Z%22%20fill%3D%22%23166534%22%2F%3E%3C%2Fsvg%3E" alt="Green hill" srcset="data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22160%22%20height%3D%2280%22%20viewBox%3D%220%200%20160%2080%22%3E%3Crect%20width%3D%22160%22%20height%3D%2280%22%20fill%3D%22%23dbeafe%22%2F%3E%3Cpath%20d%3D%22M0%2080%20L80%2016%20L160%2080Z%22%20fill%3D%22%23166534%22%2F%3E%3C%2Fsvg%3E 160w, data:image/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20width%3D%22320%22%20height%3D%22160%22%20viewBox%3D%220%200%20320%20160%22%3E%3Crect%20width%3D%22320%22%20height%3D%22160%22%20fill%3D%22%23dbeafe%22%2F%3E%3Cpath%20d%3D%22M0%20160%20L160%2032%20L320%20160Z%22%20fill%3D%22%23166534%22%2F%3E%3C%2Fsvg%3E 320w" sizes="160px" width="160" height="80">The browser has two width candidates for the same slot.
Practice and next steps
Try a different viewport in the browser and compare the selected resource in developer tools.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Responsive Images: srcset and sizes — Knowledge Check
Check the core concepts from this lesson.
What does 320w describe?
What does sizes communicate?
Which srcset list is valid in principle?
A srcset uses 1x and 2x descriptors. What distinguishes the candidates?
Can you guarantee that a browser chooses the smallest candidate matching a calculated slot?