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 address tag in HTML shows contact details. The <address> tag helps show who owns the page or article. Understand…
The HTML time Tag marks time values in a document. Use it to show dates or times that machines and…
Forms need instructions so users know what to enter. You use the HTML label tag to give names to input…
The br tag adds a single line break in HTML. It is an empty tag that does not need a…
The heading elements define a content structure in HTML. Use <h1> to <h6> to show levels. <h1> means the main…
The HTML a tag allows you to create links that move users from one page or resource to another. Understand…
Use the HTML b tag to make text bold without giving it extra meaning. It adds visual weight only. Understand…
The HTML title attribute adds extra info to any element. You can use it to show tooltips on hover. What…
The HTML textarea tag gives users space to enter longer messages. This element helps build forms that accept feedback, comments,…
You use the <aside> tag to mark extra content in HTML. It does not replace the main text and shows…