HTML Academy · Lesson

The source Element for Multiple Formats

Provide fallback formats with the source element.

Lesson 3 of 413 steps

The source Element for Multiple Formats is a free HTML Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the HTML Academy learning path, and your progress syncs across the web and the CoddyKit app. The HTML Academy course includes 4 lessons in total.

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:

  1. Reads the first source's type
  2. If it can play that type, downloads and uses it
  3. If not, moves to the next source
  4. 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 browsers

Fallback 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 media on source for viewport-based video selection
Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
40
Lessons
159

Frequently Asked Questions

Is the “The source Element for Multiple Formats” lesson free?

Yes — any 3 lessons of the HTML Academy learning path, including “The source Element for Multiple Formats”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The HTML Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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

  1. The audio Element src controls loop autoplay
  2. The video Element and Aspect Ratio
  3. The source Element for Multiple Formats
  4. Subtitles with track and WebVTT
← Back to HTML Academy