- Home
- /
- Tutorials
- /
- HTML Tutorial
- /
- HTML <source> Tag
HTML Tags
HTML <source> Tag
<source>offers the browser a choice. Inside <picture>, <video> or <audio>, you list several versions of the same image or media (different formats, sizes, or crops) and the browser takes the first one it can use. Order matters, and the fallback is not optional.
Try it
The same picture in a modern format first and a universal one last. Every browser shows an image; a browser that understands AVIF loads the smaller file. Resize the preview to see the wide/narrow crop switch.
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>picture with sources</title>
</head>
<body>
<picture>
<source media="(min-width: 600px)" srcset="https://placehold.co/800x300/0ea5e9/fff?text=Wide+crop" type="image/png">
<source srcset="https://placehold.co/400x400/f97316/fff?text=Square+crop" type="image/png">
<img src="https://placehold.co/400x400/64748b/fff?text=Fallback" alt="Placeholder illustration" style="max-width:100%">
</picture>
</body>
</html>How the browser chooses
It walks the <source> elements top to bottom and stops at the first one whose type it supports and whose media query (if any) matches. If none match, it uses the <img> (in a picture) or the element's own src (for video and audio). So: most specific and most modern first, universal last.
Attributes
| Attribute | Used in | What it does |
|---|---|---|
| srcset | | One or more image URLs, optionally with width (800w) or density (2x) descriptors. Required in a picture. |
| sizes | | How wide the image will be displayed at various viewport widths, so the browser can pick from the w candidates. |
| media | | A media query. The source is only considered if it matches. Ignored on video and audio sources. |
| type | All three | The MIME type: image/avif, image/webp, video/mp4, audio/ogg. Lets the browser skip formats it cannot play without downloading them. |
| src | , | The file URL. Not used in a picture: use srcset there. |
| width, height | | Intrinsic size of that source, so the browser can reserve space before it loads. |
A realistic example: modern formats with a safe fallback
The most valuable use of <source> today is format negotiation. AVIF is typically half the size of JPEG for the same quality, WebP about two-thirds, but neither is safe to serve alone. This pattern serves each browser the best it can decode:
html
<picture>
<source srcset="hero.avif" type="image/avif">
<source srcset="hero.webp" type="image/webp">
<img src="hero.jpg" alt="Students working at laptops in a bright classroom"
width="1200" height="600" loading="lazy">
</picture>And for video, the same idea with codecs: WebM/AV1 for browsers that have it, MP4/H.264 for everything:
html
<video controls width="640" poster="lesson.jpg">
<source src="lesson.webm" type="video/webm">
<source src="lesson.mp4" type="video/mp4">
Your browser does not support video. <a href="lesson.mp4">Download it instead.</a>
</video>Common mistakes
1. No <img> inside <picture>, or not last
The <img> is not a fallback for old browsers: it is the element that actually displays the chosen source, in every browser. Without it nothing renders at all. It must be the last child, and it carries the alt, width, height, loading and styling; the sources only supply URLs.
2. Universal format first
Put hero.jpg first and every browser takes it, because every browser supports JPEG: the AVIF below it is never reached. Order sources from most specific to least.
3. media on a video or audio source
Browsers dropped support for media on media-element sources years ago. It is silently ignored, so the first playable source wins regardless. To serve different videos by screen size, swap the src with JavaScript.
4. src instead of srcset in a picture
<source src="hero.webp"> inside a <picture> is invalid and ignored. It must be srcset, even for a single URL.
5. Wrong or missing type
Without type, the browser cannot tell whether it can decode a source without fetching it, so it may download a WebP just to discover it cannot show it. With the wrong type (image/jpg instead of image/jpeg, video/mov instead of video/quicktime) it may skip a source it could have played.
6. Expecting sources to be swapped after load
Adding or changing <source> elements with JavaScript after a video has started does nothing; the browser has already chosen. Call video.load() to make it re-evaluate.
Browser support
<source> in video and audio: universal. In <picture>: every browser since 2015–2016 (Chrome 38, Firefox 38, Safari 9.1, Edge 13). Older browsers ignore the sources and show the <img>, which is exactly why the <img> is required.
Related chapters
HTML <source> Tag exercises
Add a source
Make a video player with controls. Give it this file through an element inside it, with the type video/mp4: https://ik.imagekit.io/html5andcss3/lesson-samples/sample_video.mp4
Result
