CKTIS / HTML
source — alternative image and media resources
The `source` element declares alternative resources for `picture`, `audio`, or `video`. It displays nothing itself. Inside `picture` it requires `srcset`; inside `audio` and `video` it requires `src`. These uses must not be confused.
Version: WHATWG HTML Living Standard; index snapshot date recorded separately
Syntax
<picture>
<source srcset="image.svg" type="image/svg+xml">
<img src="fallback.png" alt="Description">
</picture>source is a void element: it has no contents and no end tag. Inside picture, place it before the required img. Inside audio and video, source children are allowed only if the parent has no src attribute, and they must precede track elements and fallback content. If the parent has a src attribute, resource selection uses that attribute instead of the source children.
Inside picture, srcset is required and src is forbidden. The img displays the image and also provides alt text and a fallback source. sizes describes the image size for candidates with width descriptors; it is required unless img allows auto-sizes. width and height can establish the selected variant's dimensions and aspect ratio.
Inside audio and video, each source requires a nonempty src URL. Optional type supplies a MIME type and helps skip an unsupported format without fetching it. media contains media queries: selection in picture reacts to environmental changes, whereas for media elements the condition is evaluated only once during resource selection.
Merely changing src or type on an existing source inside audio or video does not change playback; set the parent's src. In the example, embedded images remove network dependencies: a viewport at least 600 CSS pixels wide selects the blue variant, while a narrower viewport selects orange.
Examples
<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>source demo</title>
<picture>
<source media="(min-width: 600px)" type="image/svg+xml"
srcset="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='120'%20height='80'%3E%3Cpath%20fill='blue'%20d='M0,0H120V80H0Z'/%3E%3C/svg%3E">
<img width="120" height="80" alt="Color sample"
src="data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='120'%20height='80'%3E%3Cpath%20fill='orange'%20d='M0,0H120V80H0Z'/%3E%3C/svg%3E">
</picture>
</html>Result
Viewport >= 600 CSS px: blue rectangle, 120 × 80 CSS px. Viewport < 600 CSS px: orange 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.