HTML <source> source tag
The <source>
tag in HTML is used to specify multiple media resources for elements that support media sources, such as the <audio>
and <video>
elements. It allows you to provide different formats of the same media file to ensure compatibility with various browsers and devices.
Key Features:
- Multiple Formats: Enables you to provide different file formats or sources for the same media, allowing the browser to choose the most suitable one.
- Fallback: Improves compatibility by providing alternative formats for browsers that may not support certain media types.
Basic Syntax:
<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>
In this example:
- Two
<source>
elements are used within the<audio>
tag to provide both MP3 and OGG formats of the audio file. - The browser will select the format it supports and ignore the others.
- The text "Your browser does not support the audio element." is a fallback message displayed if none of the provided formats are supported.
Example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Source Tag Example</title>
</head>
<body>
<h1>Video Example</h1>
<video width="640" height="360" controls>
<source src="video.mp4" type="video/mp4">
<source src="video.webm" type="video/webm">
Your browser does not support the video element.
</video>
</body>
</html>
In this example:
- The
<video>
element includes two<source>
tags for different video formats (MP4 and WebM). - The browser will use the format it supports and ignore the other.
- The fallback message is shown if neither of the provided video formats is supported.
Attributes:
src
: Specifies the URL of the media file.<source src="video.mp4">
type
: Defines the MIME type of the media file. This helps the browser understand the format of the media.<source src="video.mp4" type="video/mp4">
Use Cases:
- Compatibility: Ensures that media files are playable across different browsers and devices by providing multiple formats.
- Fallback: Provides a fallback mechanism to handle cases where the preferred media format is not supported.
Best Practices:
- Provide Multiple Formats: Include several formats of the media file to maximize compatibility.
- Use Appropriate MIME Types: Ensure that the
type
attribute is set correctly to avoid issues with media playback. - Fallback Content: Always provide fallback content within the media element to inform users if their browser cannot play any of the provided formats.
Key Points:
- Purpose: The
<source>
tag specifies multiple media sources for the<audio>
and<video>
elements, improving compatibility. - Usage: Allows browsers to choose the best format for the media.
- Fallback: Provides a fallback message for unsupported media formats.
In summary, the <source>
tag in HTML is used within <audio>
and <video>
elements to specify multiple media sources, allowing browsers to select the most compatible format. This enhances media compatibility and ensures a better user experience across different devices and browsers.