HTML `` Element: Enhancing Media Playback Compatibility
Learn how to use the HTML `` element to provide multiple media sources for `` and `` tags. This tutorial explains how to improve browser compatibility and optimize the user experience by offering various media formats. Master the `` element for robust and versatile media playback.
Previous
Next
Using the HTML `` Element for Media Playback
Understanding the `` Element
The HTML `` element is used to specify multiple media resources for media elements like `` and ``. This is crucial for providing support for different media formats and optimizing the user experience. The browser will select the first source that it supports, improving compatibility across various browsers and devices. This means that you can provide several different versions of your audio or video, and the browser will automatically select the most suitable version.
How the `` Element Works
The `` element is always placed *inside* a media element (`` or ``). Each `` element specifies a media file using the `src` attribute and its MIME type using the `type` attribute. The browser attempts to play the first suitable media source. If none of the sources are compatible, the content between the opening and closing media tags is displayed. The `srcset` attribute can be used to provide various sizes for images; it specifies URLs and sizes for different display contexts. The browser selects the most appropriate image based on the screen size and resolution.
Example: `` Element in an `` Tag
<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>
Copy Code
Browser Support for ``
The `` element is widely supported by modern browsers. Check the W3Schools Browser Support page for detailed compatibility information.
Browser
Version
Chrome
4.0
Edge
9.0
Firefox
3.5
Opera
4.0
Safari
10.5
`` Attributes
Attribute
Value
Description
media
Media query
Specifies the media type (screen size, etc.).
sizes
Size description
Specifies image sizes for different layouts (used with `srcset`).
src
URL
Specifies the URL of the media resource.
srcset
URL list
Specifies multiple image sources for different resolutions (used with ` `).
type
MIME type
Specifies the MIME type of the media resource.
The `` element also supports standard HTML global and event attributes.
Previous
Next