HTML <source> Tag

Beginner
6 min read
Updated: Nov 2026
4 examples
Live preview

Definition and Usage

The <source> element lists one alternate media or image resource for <video>, <audio>, or <picture>. The browser walks the list and uses the first option it can play or display.

Inside media elements, put sources before any <track> or fallback text. Inside <picture>, put sources before the required <img> (which also carries alt).

Baseline · Widely available.

Not deprecated. Stable across modern browsers since about July 2015.

Void element.

Do not write a closing </source>. Attributes also differ by parent: src for audio/video; srcset for picture.

HTML Syntax

syntax.html
<source src=" url" type="mime">

Start tag only. For <picture>, use srcset (not src) on each <source>.

Attributes

Element-specific attributes (allowed set depends on the parent):

AttributeDescriptionTry it
srcResource URL. Required for audio/video. Not allowed inside pictureTry it
typeMIME type (optional codecs) so unsupported formats are skippedTry it
srcsetImage candidates with w or x descriptors. Required inside pictureTry it
sizesLayout width hints with width-based srcset (picture only)With w descriptors
mediaMedia query for when this source applies (art direction or large/small files)Try it
width / heightIntrinsic image size in pixels (picture only)Try it

Global Attributes

The <source> tag also supports the Global Attributes in HTML.

Live Preview

Switch between video, audio, and picture patterns that use nested <source> elements.

Real nested <source> markup. The browser picks the first usable resource.

Preview

Video — WebM first, then MP4.

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Multiple Video Formats

Offer WebM and MP4 so more browsers can play the clip.

HTML
<video controls width="360" muted>
  <source src="/video/count.webm" type="video/webm">
  <source src="/video/count.mp4" type="video/mp4">
  Download the
  <a href="/video/count.webm">WEBM</a>
  or
  <a href="/video/count.mp4">MP4</a>
  video.
</video>
Try It Yourself

How It Works

  1. Open <video> with controls (and optional size).
  2. List <source> children with src + type, preferred format first.
  3. Browser skips unsupported types without downloading when type is set.
  4. Add fallback text/links for environments that cannot play video at all.

Example 2 — Multiple Audio Formats

Same pattern for sound: MP3 plus Ogg for broader support.

HTML
<audio controls>
  <source src="/audio/count.mp3" type="audio/mpeg">
  <source src="/audio/count.ogg" type="audio/ogg">
  Your browser does not support the audio element.
  <a href="/audio/count.mp3">Download MP3</a>.
</audio>
Try It Yourself

How It Works

  1. Nest sources inside <audio> before fallback text.
  2. Set MIME type (audio/mpeg, audio/ogg, …).
  3. Order matters — first playable source wins.
  4. Keep a download link in the fallback for older clients.

Example 3 — Art Direction in <picture>

Use srcset + media to swap images by viewport width.

HTML
<picture>
  <source srcset="/images/nature-pattern.jpg" media="(width >= 600px)">
  <img src="/images/circle_image.jpg" alt="Demo responsive image" width="240" height="160">
</picture>
Try It Yourself

How It Works

  1. Inside picture, use srcset on <source> (not src).
  2. Add media for when that image should apply.
  3. Always end with <img> — required fallback and alt.
  4. If no source matches, the browser uses the img src.

Example 4 — Format Fallback with type

Offer WebP first; unsupported browsers skip to the JPEG/PNG <img>.

HTML
<picture>
  <source srcset="/images/html-picture-tag.webp" type="image/webp" width="320" height="168">
  <img src="/images/javascript.png" alt="HTML picture tag illustration" width="320" height="168">
</picture>
Try It Yourself

How It Works

  1. Put modern formats first with matching type (e.g. image/webp).
  2. Optional width/height on picture sources help reserve layout space.
  3. Keep a widely supported fallback on <img src>.
  4. Write alt once on that <img>, not on <source>.

Edge Cases & Pitfalls

Most bugs mix audio/video attributes with picture rules, or forget fallbacks.

parent

src vs srcset

Audio/video need src. Picture needs srcset. Mixing them is invalid for that parent.

order

Sources before tracks / img

In media elements, place <source> before <track> and fallback text. In picture, sources come before <img>.

type

Always set MIME when you can

Without type, browsers may download a file only to discover they cannot play it.

a11y

alt lives on img

<source> has no alt. For captions on video, use <track>.

void

No closing tag

Writing </source> is incorrect HTML. Treat it like <img> or <br>.

Browser Support

The <source> element is core HTML5 media/image markup in every major browser. Playable formats still vary by codec.

✓ Baseline · Widely available

<source>

Widely available since about July 2015. Nesting source in video, audio, and picture is supported across Chrome, Firefox, Safari, Edge, and Opera. Offer multiple formats for widest playback.

98% Element support (approx.)
Google Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet All modern
Full support
Internet Explorer 9+ (media); picture later
Partial
<source> Widely supported

Bottom line: Safe to use everywhere. Always provide fallbacks (extra formats, nested img, or download links) because codecs differ.

Key Takeaways

  • Role: <source> lists alternate resources for video, audio, or picture.
  • Void: start tag only — never </source>.
  • Parent rules: media → src + type; picture → srcset (+ media/type).
  • Fallback: first usable source wins; always keep img or text fallbacks.

One line: nest ordered <source> alternatives so browsers can pick a format or image that works.

Default CSS Settings

source is not rendered on its own. Typical user-agent idea:

default.css
source {
  display: none;
}

Visible UI comes from the parent video, audio, or the nested img inside picture.

Frequently Asked Questions

The source element lists alternate media or image resources for video, audio, or picture. The browser picks the first option it can use, then falls back to later sources or nested fallback content.
Yes. source has no content and must not have a closing tag. Write <source …> (or self-close as <source … /> in XHTML-style markup).
Use src (required) and usually type (MIME). Optional media can choose a file by viewport. Do not use srcset or sizes on source inside audio or video.
Use srcset (required). Add media and/or type as needed, and sizes when using width descriptors. Do not put src on source inside picture — the fallback URL belongs on the nested img.
With type, the browser can skip unsupported formats without downloading the file. List preferred formats first (for example WebM then MP4).
For picture, the nested img is used. For audio/video, the browser shows the fallback content between the parent’s tags (or an error state in the controls).

Next: spacer

See the obsolete spacer element and modern CSS layout alternatives.

spacer tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful