The source Element for Multiple Formats
Provide fallback formats with the source element.
The source Element for Multiple Formats is a free HTML Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the HTML Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why Multiple Formats?
Different browsers support different audio/video codecs:
- WebM (VP9/VP8) — Chrome, Firefox, Edge (not Safari)
- MP4 (H.264) — all browsers (universal)
- OGG — Firefox, Chrome (not Safari)
- OPUS — Chrome, Firefox, Edge (not Safari)
The <source> element lets you provide multiple formats for fallback.
source Inside audio
Provide multiple audio formats:
<audio controls>
<!-- Browser picks the first format it supports: -->
<source src="podcast.opus" type="audio/ogg; codecs=opus">
<source src="podcast.ogg" type="audio/ogg; codecs=vorbis">
<source src="podcast.mp3" type="audio/mpeg">
<!-- Fallback text if none supported: -->
<p>Download: <a href="podcast.mp3">podcast.mp3</a></p>
</audio>source Inside video
Provide multiple video formats:
<video controls width="1280" height="720" poster="thumbnail.jpg">
<source src="tutorial.webm" type="video/webm">
<source src="tutorial.mp4" type="video/mp4">
<p>Your browser doesn't support HTML5 video.
<a href="tutorial.mp4">Download the video</a></p>
</video>The type Attribute on source
The type attribute tells the browser the MIME type and codec:
<!-- Full MIME type with codec parameter: -->
<source src="video.webm" type="video/webm; codecs=vp9,vorbis">
<source src="video.mp4" type="video/mp4; codecs=avc1.42E01E,mp4a.40.2">
<source src="audio.ogg" type="audio/ogg; codecs=vorbis">
<!-- Simplified (browser may need to probe): -->
<source src="video.webm" type="video/webm">
<source src="video.mp4" type="video/mp4">How source Selection Works
The browser evaluates source elements in order:
- Reads the first source's
type - If it can play that type, downloads and uses it
- If not, moves to the next source
- If no source works, shows the fallback content
Put the most preferred format first (e.g., WebM for Chrome/Firefox performance).
media Attribute on source
The media attribute lets you serve different formats for different viewports:
<video controls>
<!-- Mobile: smaller file -->
<source
src="video-480.mp4"
type="video/mp4"
media="(max-width: 768px)"
>
<!-- Desktop: full quality -->
<source
src="video-1080.mp4"
type="video/mp4"
>
</video>Recommended Format Order
Optimal source order for maximum compatibility and performance:
<video controls>
<!-- AV1 in WebM: best compression, modern browsers -->
<source src="video.av1.mp4" type="video/mp4; codecs=av01">
<!-- VP9 in WebM: Chrome/Firefox/Edge -->
<source src="video.webm" type="video/webm">
<!-- H.264 in MP4: universal fallback -->
<source src="video.mp4" type="video/mp4">
</video>Audio Format Recommendations
Audio codec recommendations:
- OPUS in WebM — best quality/size ratio; Chrome, Firefox, Edge
- AAC in MP4 — universal; Safari, Chrome, Firefox, Edge
- MP3 — widest compatibility; use as last fallback
Responsive Video with srcset
For video, use media on source for bandwidth adaptation:
<video controls poster="thumbnail.jpg">
<source
src="video-720.webm"
type="video/webm"
media="(min-width: 1024px)"
>
<source
src="video-360.webm"
type="video/webm"
>
<source src="video-720.mp4" type="video/mp4" media="(min-width: 1024px)">
<source src="video-360.mp4" type="video/mp4">
</video>Checking canPlayType
Test browser codec support with JavaScript:
const video = document.createElement('video');
console.log(video.canPlayType('video/webm; codecs=vp9'));
// '' = no, 'maybe' = possibly, 'probably' = likely yes
console.log(video.canPlayType('video/mp4; codecs=avc1.42E01E'));
// 'probably' on most modern browsersFallback Content
Always include fallback content inside audio/video:
<video controls width="800" height="450">
<source src="demo.webm" type="video/webm">
<source src="demo.mp4" type="video/mp4">
<!-- Fallback for browsers without video support: -->
<p>
Your browser does not support HTML5 video.
<a href="demo.mp4">Download the video</a> instead.
</p>
</video>Quick Check
In what order should you list source elements?
Recap: source Element
source element essentials:
<source src type>inside audio/video provides format alternatives- Browser picks first matching source
- Most modern format first; MP4/AAC as universal fallback
- Include fallback text/link inside audio/video after all sources
- Use
mediaon source for viewport-based video selection
Frequently asked questions
Is the “The source Element for Multiple Formats” lesson free?
Yes — the full text of “The source Element for Multiple Formats” is free to read here on the web, and the HTML Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the HTML Academy course, upgrade to CoddyKit PRO.
What will I learn in “The source Element for Multiple Formats”?
Provide fallback formats with the source element. You practise HTML Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start HTML Academy?
No prior experience is required. HTML Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The source Element for Multiple Formats” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this HTML Academy lesson?
Yes. Every HTML Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The audio Element src controls loop autoplay
- The video Element and Aspect Ratio
- The source Element for Multiple Formats
- Subtitles with track and WebVTT