Table of Content
The source tag in HTML helps load different media formats and screen sizes.
You can use it inside many other tags to improve compatibility, such as:
<video><audio><picture>
Understand the HTML <source> Tag
The <source> tag gives the browser different file options for one media element. It helps load the best file for a user’s device or browser. The browser checks each <source> in order and picks the first one it supports.
Here is the tag:
<source src="file-path" type="media-type" />You do not close it like a normal element. The tag goes inside a parent, like <video> or <audio>. Also, it can be placed inside <picture>. It does not work alone.
Here are the Common Use Cases for <source:
- Inside
<picture>for Responsive Images - Inside
<audio>or<video>for Format Support
Here are the required and optional attributes:
The src attribute:
- It gives the path to the media file.
- It is required in most cases.
Here is an example:
<source src="video.mp4" />The type attribute:
- Tells the browser the media type and format.
- Helps the browser decide whether it supports the file.
For example:
<source src="video.webm" type="video/webm" />The media (for <picture> only) attribute:
- Used for screen size or device rules.
- Helps switch images based on screen width.
Here is an example:
<source srcset="image-large.jpg" media="(min-width: 800px)" />The srcset and sizes (for responsive images) attribute:
srcsetdefines multiple image options.sizesgives rules to help the browser pick one.
For example:
<source srcset="img-480.jpg 480w, img-800.jpg 800w" sizes="(max-width: 600px) 480px, 800px" />You add the <source> tag:
- Inside
<video>and<audio>to load more than one format. - Inside
<picture>to show different images on different devices.
This tag must be a child of those elements. It cannot stand alone. It lets you offer .mp4 or .webm. Also, it allows you to add .ogg or other formats for different browsers.
Browsers Support <source> Tag in HTML
The <source> tag works on all modern browsers. That includes:
- Chrome
- Firefox
- Safari
- Edge
- Opera
Older browsers may skip unsupported formats. The browser moves to the next <source>.
The Difference Between <source>, <img>, and <track>
<source>
- Helps load media files inside
<video>,<audio>, or<picture>. - Offers multiple formats or resolutions.
- Does not render content by itself.
<img>
- Loads one image file.
- Does not need a container.
- Does not switch formats automatically.
<track>
- Adds subtitles or captions to
<video>or<audio>. - Does not affect file formats or images.
- Works with
kind,src,srclang, andlabel.
Here is a table that shows you key differences:
| Tag | Purpose | Works With | Can Use Multiple Formats | Adds Captions |
|---|---|---|---|---|
<source> | Choose file based on format or size | <video>, <audio>, <picture> | Yes | No |
<img> | Display a single image | Alone | No | No |
<track> | Add subtitles or captions | <video>, <audio> | No | Yes |
The use case:
- Use
<source>when you want fallback formats or responsive images. - Use
<img>when you only need one static image. - Use
<track>when you need captions or translated text for video or audio.
Examples of <source> Tag in HTML
Responsive Image with <picture>:
<picture>
<source srcset="image-large.jpg" media="(min-width: 800px)" />
<source srcset="image-medium.jpg" media="(min-width: 500px)" />
<img src="image-small.jpg" alt="Responsive example" />
</picture>This example shows how to use <source> inside <picture> to load different image sizes. The browser uses screen width to pick the right image.
Multiple Formats in a Video Tag:
<video controls>
<source src="video.webm" type="video/webm" />
<source src="video.mp4" type="video/mp4" />
Your browser does not support the video tag.
</video>This example uses <source> to offer .webm and .mp4 formats for a video. The browser plays the first format it supports.
Audio with Fallback Formats:
<audio controls>
<source src="sound.ogg" type="audio/ogg" />
<source src="sound.mp3" type="audio/mpeg" />
Your browser does not support the audio element.
</audio>This example gives two audio file formats. Some browsers do not support .ogg, so the .mp3 file works as a backup.
Advanced Responsive Image with srcset and sizes:
<picture>
<source
srcset="img-400.jpg 400w, img-800.jpg 800w"
sizes="(max-width: 600px) 400px, 800px"
type="image/jpeg" />
<img src="img-800.jpg" alt="Advanced responsive image" />
</picture>This example uses srcset and sizes to handle responsive image loading. It works better than basic media queries by giving size hints to the browser.
Wrapping Up
In this article, you learned what the <source> tag does and where to use it. You saw how it supports different media types, adds responsive images, and improves browser support.
Here is a quick recap:
- Use
<source>with<video>,<audio>, or<picture>. - It supports different file formats and screen rules.
- It does not work alone. You must use it inside a media element.
<source>differs from<img>and<track>in both use and purpose.- You can build flexible layouts and stronger fallbacks with it.
FAQs
How to use the <source> tag in HTML video?
<video controls>
<source src="video.mp4" type="video/mp4">
<source src="video.webm" type="video/webm">
Your browser does not support the video tag.
</video>
Use multiple <source> elements to ensure compatibility across all browsers.
What is the purpose of the <source> tag in <picture>?
<picture>
<source media="(min-width: 800px)" srcset="large.jpg">
<source media="(min-width: 400px)" srcset="medium.jpg">
<img src="small.jpg" alt="Responsive image">
</picture>
This technique helps load the most appropriate image based on the user’s device.
Can I use multiple <source> tags in HTML <audio>?
<audio controls>
<source src="audio.mp3" type="audio/mpeg">
<source src="audio.ogg" type="audio/ogg">
Your browser does not support the audio element.
</audio>
This ensures the audio plays across various browsers by offering supported formats.
Similar Reads
The HTML sup tag puts text above the normal line. Writers use it for math power and marks. So, readers…
The HTML <cite> tag is a small but important tool for marking up the title of a creative work. You…
The <output> tag in HTML shows results inside a form. It helps display values from user actions or scripts. You…
The HTML canvas tag draws shapes and images. It also helps you draw text with code. It gives control over…
In this article, you will cover how to use div tag in HTML layout. It offers beginner to advanced code…
The HTML span tag acts as a simple container for inline text or other inline elements. It groups text and…
HTML code does not always show the same result on every browser. Old browsers follow outdated rules while modern browsers…
The <wbr> tag in HTML helps the browser decide where it can break a word. It does not force a…
The HTML noscript tag tells the browser what to show when JavaScript does not run. This tag targets users who…
The HTML figure tag links media with a caption. Use it to group an image, diagram, code block, or video…