The HTML <source> tag is used to specify multiple media resources for elements such as <audio>, <video>, and <picture>. It allows browsers to choose the most suitable file format or media source to display.
- Provides alternative media files for audio, video, and images.
- Helps ensure compatibility across different browsers and devices.
- Commonly used inside <audio>, <video>, and <picture> elements.
Syntax:
<source src=" " type="">
// Statements
</source>
Attributes
- src : src specifies the path or URL of the media resource.
- media : Defines the media condition or type for which the resource is intended.
- srcset : srcset specifies one or more image URLs for different display situations.
- sizes : Defines the image sizes for different page layouts.
- type : Specifies the MIME type of the media resource.
- height : Sets the intrinsic height of the image in pixels (used with <picture>).
- width : Sets the intrinsic width of the image in pixels (used with <picture>).
Example 1: This example uses <source> tag with the video media file.
Example 2: This example uses <source> tag with the audio media files.