Open In App

HTML <source> Tag

Last Updated : 27 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The <source> tag in HTML is used to specify multimedia files like <audio>, <video>, and <picture>. It utilizes attributes like src and type for defining the source URL and also enables the inclusion of different file formats for compatibility across browsers.

It is a void element, which means that it has no content and does not require a closing tag. The <source> tag also supports the Global Attributes and Event Attributes in HTML.

Syntax:

<source src=" " type=""> 

// Statements

</source>

Attributes:

AttributeDescription
srcIt holds the path of media content.
mediaIt defines the type of the media content.
srcsetIt specifies the URL of the image used in different situations.
sizesIt specifies the sizes of images in different page layouts.
typeIt specifies the MIME-type resource.
heightIntrinsic image height in pixels (only for <picture>).
widthIntrinsic image width in pixels (only for <picture>).

Example 1: This example uses <source> tag with the video media file. 

html
<!DOCTYPE html>
<html>

<head>
    <title>
        HTML source Tag
    </title>
</head>

<body>
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>

    <h2>
        HTML &lt;source&gt; Tag
    </h2>

    <video width="400" height="350" controls>
        <source src="video.mp4" type="video/mp4">
        <source src="video.ogg" type="video/ogg">
    </video>
</body>

</html>

Output:

Example 2: This example uses <source> tag with the audio media files. 

html
<!DOCTYPE html>
<html>

<head>
    <title>
        HTML source Tag
    </title>
</head>

<body>
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>
    <h2>
        HTML &lt;source&gt; Tag
    </h2>
    <audio controls>
        <source src="audio.mp3" 
                type="audio/mp3">
    </audio>
</body>

</html>

Output:

Supported Browsers:


Next Article

Similar Reads