HTML Reference Β· Embedded Content
The <source> element
Reference for the HTML <source> element: type, src, srcset, sizes, media, width and height, how it works in <picture>, <video> and <audio>, with examples.
The <source> element offers one alternative file to its parent. It works in two places, with slightly different attributes:
- Inside
<video>or<audio>, it usessrcandtype. The browser plays the first source it can handle, which lets you offer several formats. - Inside
<picture>, it usessrcset,sizes,mediaandtype. The browser picks the first source whose conditions match and shows it through the<img>.
<source> is a void element and is never displayed itself.
HTML
<video controls> <source src="/media/clip.webm" type="video/webm"> <source src="/media/clip.mp4" type="video/mp4"> </video>
Try it
Resize the preview: the label in the picture shows which source matched.
Attributes
| Attribute | Values | Description |
|---|---|---|
type |
MIME type, optionally with codecs |
Both contexts. The file's format, such as video/webm, audio/mpeg or image/avif. The browser skips sources whose type it can't handle without downloading them. |
src |
URL | <video> and <audio> only. The media file. Required inside <video> and <audio>; not allowed inside <picture>. |
srcset |
Comma-separated URL width or URL density candidates |
<picture> only. The image file(s) for this source, with the same syntax as on <img>. Required inside <picture>. |
sizes |
Media conditions and lengths | <picture> only. The display width of the image, used with w descriptors in srcset. |
media |
Media query | <picture>, <video> and <audio>. The condition under which this source applies, such as (min-width: 600px) or (prefers-color-scheme: dark). Inside <video> and <audio> it was removed from HTML years ago and later restored; support there is recent. |
width |
Non-negative integer | <picture> only. The intrinsic width of this source's image, so the browser can reserve space when crops have different shapes. |
height |
Non-negative integer | <picture> only. The intrinsic height of this source's image. |
It also accepts the global attributes.
Usage notes
- Put sources before everything else. In media elements,
<source>elements come first, then<track>, then fallback content. In<picture>, they come before the<img>. - Always set
type. Without it, the browser may have to download part of each file to find out whether it can play it. - Don't combine with
srcon the parent. If<video>or<audio>has asrcattribute, its<source>children are ignored. - Order of preference. Browsers take the first usable source, so list newer, smaller formats (AV1, WebM, AVIF, WebP) before the universal fallback (MP4, MP3, JPEG).
- Changing sources with JavaScript. Editing a
<source>after the media element has loaded does nothing until you callload()on the media element. - Check the choice.
currentSrcon the media element or the<img>returns the URL that was actually chosen.
Technical summary
| Content categories | None |
| Permitted content | Nothing; it is a void element |
| Tag omission | Start tag required; must not have an end tag |
| Permitted parents | A <picture> element, before its <img>; or a <video> or <audio> element without a src attribute, before any <track> elements and other content |
| Implicit ARIA role | No corresponding role |
| DOM interface | HTMLSourceElement |
| Default display | Not rendered |
Browser support
Supported in every browser. width and height inside <picture> and media inside <video>/<audio> are newer and only honored by recent browser versions.
Related
<picture>β image alternatives<video>and<audio>β media alternatives<track>β text tracks that follow the sources<img>β the image that renders a chosen source- MIME types β values for the
typeattribute - Lesson: Responsive Images
- Lesson: Video
- Lesson: Audio