CKTIS / HTML
srcset — image candidate list
The `srcset` attribute supplies alternative image resources to the browser. The `x` descriptors describe pixel density, while `w` descriptors describe resource widths used together with `sizes` for image selection.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
srcset="image.png 1x, image@2x.png 2x" | srcset="image-400.png 400w, image-800.png 800w" sizes="100vw"srcset is a comma-separated list of image URLs with optional descriptors. It applies to img and to source inside picture; on the latter it is required, and the following img displays the selected image. It does not replace src on source in audio or video.
The descriptor 2x denotes density, while 800w denotes the resource's natural width. Values must be positive and must not repeat. An omitted descriptor means 1x. Do not mix w with x or descriptorless candidates.
With w candidates, img requires sizes; it describes width for selection, not CSS layout sizing. A source also requires sizes in this case unless the following img allows auto-sizes. With w, the browser ignores src for selection; with x, it can supply a 1x candidate if the list lacks one.
The browser selects a resource according to viewing conditions; do not assume a fixed mapping to screen density. The example embeds two images and keeps the same appearance regardless of selection.
Examples
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>srcset example</title>
<img
src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='40'%20height='40'%3E%3Cpath%20fill='royalblue'%20d='M0,0H40V40H0Z'/%3E%3C/svg%3E"
srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='40'%20height='40'%3E%3Cpath%20fill='royalblue'%20d='M0,0H40V40H0Z'/%3E%3C/svg%3E 1x, data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='80'%20height='80'%3E%3Cpath%20fill='royalblue'%20d='M0,0H80V80H0Z'/%3E%3C/svg%3E 2x"
width="40" height="40" alt="Blue square">
</html>Result
A royal-blue square displayed at 40 × 40 CSS pixels.
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.