source

The source element specifies multiple media resources for picture, video, and audio elements, enabling format fallbacks and responsive media selection.

Description

The <source> element is an empty element that specifies media resources for <picture>, <video>, and <audio> elements. It allows browsers to choose the most appropriate media file based on format support, media queries, or other criteria.

The browser evaluates <source> elements in order and uses the first one that matches. This enables progressive enhancement with modern formats while maintaining compatibility with older browsers.

When to Use

  • Format fallback - Serve modern formats (WebP, AVIF, VP9) with older format fallbacks
  • Art direction - Different images for different viewport sizes (with picture)
  • Resolution switching - Different resolutions for different pixel densities
  • Codec support - Alternative video/audio codecs for browser compatibility

Usage with Picture

In <picture>, use srcset (not src) to specify image sources.

Format Switching

Serve modern image formats to supporting browsers.

Art Direction

Different image crops for different viewport sizes.

Combined: Format + Art Direction

Resolution Switching

Serve high-resolution images to high-DPI displays.

Dark Mode Images

Usage with Video

In <video>, use src to specify video sources.

Format Fallback

Video Codecs

Format Type Notes
MP4 (H.264) video/mp4 Universal support, good quality
WebM (VP9) video/webm Better compression, modern browsers
WebM (AV1) video/webm; codecs=av01 Best compression, newest browsers
Ogg (Theora) video/ogg Open format, older

Responsive Video

Different video files for different screen sizes (via media query).

Usage with Audio

Provide multiple audio formats for broad browser support.

Audio Codecs

Format Type Notes
MP3 audio/mpeg Universal support
AAC audio/aac Good quality, wide support
Ogg Vorbis audio/ogg Open format
Opus audio/opus Best quality/size, modern browsers
WAV audio/wav Uncompressed, large files
FLAC audio/flac Lossless compression

Attributes

Attribute Context Description
src video, audio URL of the media file
srcset picture URL(s) of image files with optional descriptors
type all MIME type of the media file
media picture, video Media query for when this source applies
sizes picture Image sizes for responsive images
width picture Intrinsic width of the image
height picture Intrinsic height of the image

Best Practices

Order Matters

Browsers use the first matching source, so order from most preferred to least preferred.

Always Include Type

The type attribute lets browsers skip unsupported formats without downloading.

Include Codec Information

For video, specify codec in the type for more accurate format selection.

Accessibility

The <source> element itself has no accessibility implications - it's purely a mechanism for resource selection. Accessibility considerations apply to the parent elements:

  • Picture: Ensure the <img> has appropriate alt text
  • Video: Provide <track> elements for captions
  • Audio: Provide transcripts for audio content

Related

  • <picture> - Container for responsive images using source
  • <img> - Required fallback within picture
  • <video> - Video player that accepts source elements
  • <audio> - Audio player that accepts source elements
  • <track> - Timed text tracks for video/audio

CSS Reference

The <source> element is not rendered and has no visual appearance. Vanilla Breeze sets display: none as a precaution.