Skip to content

<source> - The Source Element

Media Element HTML5

The source element specifies multiple media resources for <picture>, <audio>, and <video> elements. It enables format fallbacks, responsive images, and cross-browser compatibility by letting browsers choose the most appropriate resource.

Result
<source src="media.mp4" type="video/mp4">
<source srcset="image.webp" type="image/webp">

The <source> element is void (self-closing) and must be a child of <picture>, <audio>, or <video>.

When used in <picture>, source uses srcset instead of src:

AttributeDescriptionExample
srcsetImage URL(s) and descriptorssrcset="image.webp" or srcset="img-400.jpg 400w, img-800.jpg 800w"
typeMIME type of the imagetype="image/webp", type="image/avif"
mediaMedia query for art directionmedia="(min-width: 800px)"
sizesImage sizes for different viewportssizes="(max-width: 600px) 100vw, 50vw"
widthIntrinsic width (new)width="800"
heightIntrinsic height (new)height="600"

When used in <audio> or <video>, source uses src:

AttributeDescriptionExample
srcURL of the media filesrc="video.mp4"
typeMIME type of the mediatype="video/mp4", type="audio/mpeg"

Serve modern image formats with automatic fallbacks:

Result

Different images for different viewport sizes:

Result

Provide multiple resolutions for different pixel densities:

Result

Let the browser choose based on viewport width:

Result

Use both media queries and format fallbacks:

Result

Provide multiple video formats for browser compatibility:

Result

Specify exact codec information for better browser selection:

Result

Provide multiple audio formats:

Result
Result
FormatMIME TypeBrowser Support
AVIFimage/avifChrome 85+, Firefox 93+, Safari 16+
WebPimage/webpChrome 23+, Firefox 65+, Safari 14+
JPEGimage/jpegUniversal
PNGimage/pngUniversal
GIFimage/gifUniversal
SVGimage/svg+xmlUniversal (modern browsers)
FormatMIME TypeCodecsSupport
MP4 (H.264)video/mp4avc1.42E01E, mp4a.40.2Universal
WebM (VP9)video/webmvp9, opusChrome, Firefox, Edge
WebM (VP8)video/webmvp8, vorbisChrome, Firefox, Edge
Ogg (Theora)video/oggtheora, vorbisFirefox, Chrome
FormatMIME TypeCodecsSupport
MP3audio/mpeg-Universal
AACaudio/mp4mp4a.40.2Universal
Opusaudio/opus-Chrome, Firefox, Edge
Ogg Vorbisaudio/oggvorbisFirefox, Chrome
WAVaudio/wav-Universal
<picture>
<source media="(min-width: 1200px)" srcset="xlarge.jpg">
<source media="(min-width: 800px)" srcset="large.jpg">
<source media="(min-width: 400px)" srcset="medium.jpg">
<img src="small.jpg" alt="Responsive image">
</picture>
Result

The browser processes <source> elements in this order:

  1. Check media query - If media attribute exists, must match
  2. Check format support - If type attribute exists, must be supported
  3. Check srcset - Evaluate srcset and pick best resolution
  4. Use first match - Stop processing after first match
Result
<!-- Order: specific to general -->
<picture>
<source media="(min-width: 1200px)" srcset="large.avif" type="image/avif">
<source media="(min-width: 1200px)" srcset="large.webp" type="image/webp">
<source media="(min-width: 800px)" srcset="medium.avif" type="image/avif">
<source media="(min-width: 800px)" srcset="medium.webp" type="image/webp">
<img src="small.jpg" alt="Description" width="400" height="300">
</picture>
<!-- Modern formats first -->
<picture>
<source srcset="photo.avif" type="image/avif">
<source srcset="photo.webp" type="image/webp">
<img src="photo.jpg" alt="Photo">
</picture>
<!-- Best formats first -->
<video controls>
<source src="video.mp4" type='video/mp4; codecs="avc1.42E01E"'>
<source src="video.webm" type='video/webm; codecs="vp9"'>
Fallback text for unsupported browsers.
</video>
<!-- Include codecs -->
<audio controls>
<source src="audio.opus" type="audio/opus">
<source src="audio.mp3" type="audio/mpeg">
Audio not supported.
</audio>
Result
Result

The <source> element itself has no accessibility attributes. All accessibility features come from the parent <img>, <audio>, or <video> element:

<!-- Alt text on img, not source -->
<picture>
<source srcset="image.webp" type="image/webp">
<img src="image.jpg" alt="Descriptive alt text">
</picture>
<!-- Captions on video, not source -->
<video controls>
<source src="video.mp4" type="video/mp4">
<track kind="captions" src="captions.vtt" srclang="en" label="English">
</video>
FeatureChromeFirefoxSafariEdge
<source> in <picture>38+38+9.1+13+
<source> in <video>3+3.5+4+12+
<source> in <audio>3+3.5+4+12+
media attribute38+38+9.1+13+
type attribute3+3.5+4+12+
srcset attribute38+38+9.1+13+
sizes attribute38+38+9.1+13+

Container for source elements providing image alternatives. Learn more →