CKTIS / HTML
picture — conditional image selection
The `picture` element supplies an `img` element with alternative image sources based on display conditions or format support. It also enables different crops, rather than merely different resolutions.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
<picture>
<source media="…" type="…" srcset="…">
<img src="…" alt="…">
</picture>picture is a container: it does not display an image itself. It contains zero or more source elements followed by exactly one img; script-supporting elements are also permitted. Both picture tags are required.
Use srcset, not src, on source. The browser considers sources in order and selects the first suitable one: media checks conditions, while type declares the format. It then selects a candidate from srcset. If no source matches, it uses the sources on img. This is not a download-error retry chain.
Use picture for art direction or format selection. For the same image at different resolutions, img with srcset and appropriate sizes is often sufficient. The w descriptors describe file widths, while x descriptors describe densities; do not mix them in one srcset.
Put alternative text in the img element's alt, not on picture or source. The example is decorative, so it uses alt="". Below a viewport width of 600 CSS pixels it displays an orange rectangle, and from 600 upward a blue one; both measure 120 × 80 CSS pixels.
Examples
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Picture selection</title>
<picture>
<source media="(min-width: 600px)" srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='120'%20height='80'%3E%3Crect%20width='120'%20height='80'%20fill='blue'/%3E%3C/svg%3E">
<img src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='120'%20height='80'%3E%3Crect%20width='120'%20height='80'%20fill='orange'/%3E%3C/svg%3E" alt="" width="120" height="80">
</picture>
</html>Result
Viewport width < 600 CSS px: orange rectangle, 120 × 80 CSS px. Viewport width >= 600 CSS px: blue rectangle, 120 × 80 CSS px.
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.