HTML <source> tag

For media attributes, such as <video> tag, <audio> tag, and <picture> tag, we use the <source> tag in html for adding such type of multiple media html elements in our web page.

A developer can use alternative video, audio, and picture files using the source> tag.

Syntax:

<audio controls>
<source src="# " type="# ">
<source src="# " type="#">
  …….
</audio>

Example

Here is an example of an audio player having two source files. The browser will select the first one that it supports:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="Free Web tutorials">
<meta name="keywords" content="HTML, CSS, JavaScript">
<meta name="author" content="deepanshu ">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> html source tag </title>
</head>
<body>
<audio controls>
<source src="horse.ogg" type="audio/ogg">
<source src="horse.mp3" type="audio/mpeg">
  Your browser does not support the audio element.
</audio>
</body>
</html>

Output:

HTML <source> Tag

Attributes of Source Tag

Type:

The MIME media type of the data and information, maybe used with a codecs parameter.

<audio controls>
<source src="horse.ogg" type="audio/ogg">
</audio>

Src:

This attribute is used when the source element is an audio or video element, but it is prohibited if the parent element of the origin resource is a picture element.

It is made up of the media resource's address.

<img src="https://cdn.pixabay.com/photo/2013/07/21/13/00/rose-165819__340.jpg" alt="Flowers" style="width:auto;">

Srcset:

It must be present if the source element is a picture element. It can not be used when the source element is an audio or video element.

We can specify the more than two string using commas.

<source media="(min-width:650px)" srcset="https://cdn.pixabay.com/photo/2013/07/21/13/00/rose-165819__340.jpg">

Sizes:

This attribute is used when the parent of source element is specified as picture.  This attribute cannot be used when the source is specified as video or audio.

Media:

If the source element is a picture element, then this can be used in source tag.  This attribute cannot be used when the source is specified as video or audio.

<source media="(min-width:650px)" srcset="https://cdn.pixabay.com/photo/2013/07/21/13/00/rose-165819__340.jpg">

Height:

If the source element is a picture element, then this can be used in source tag. This attribute cannot be used when the source is specified as video or audio.

<img src="https://cdn.pixabay.com/photo/2013/07/21/13/00/rose-165819__340.jpg" alt="Flowers" style="height:4500;">

Width:

If the source element is a picture element, then this can be used in source tag. This attribute cannot be used when the source is specified as video or audio.

<img src="https://cdn.pixabay.com/photo/2013/07/21/13/00/rose-165819__340.jpg" alt="Flowers" style="width:567;">

Example

In this example, we use video value in source tag to play a video:

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="description" content="Free Web tutorials">
<meta name="keywords" content="HTML, CSS, JavaScript">
<meta name="author" content="deepanshu ">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> html source tag </title>
</head>
<body>
<video width="320" height="240" controls>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
  Your browser does not support the video tag.
</video>
</body>
</html>

Output:

HTML <source> Tag

Example

We use picture value in source tag to define different images based on the viewport width:

<!DOCTYPE html>
<html>
<head>
<title> html source tag </title>
</head>
<body>
<picture>
<source media="(min-width:650px)" srcset="https://cdn.pixabay.com/photo/2013/07/21/13/00/rose-165819__340.jpg">
<source media="(min-width:465px)" srcset="https://cdn.pixabay.com/photo/2013/07/21/13/00/rose-165819__340.jpg">
<img src="https://cdn.pixabay.com/photo/2013/07/21/13/00/rose-165819__340.jpg" alt="Flowers" style="width:auto;">
</picture>
</body>
</html>

Output:

HTML Source Tag

Browser support:

List of browsers that support html <section> tag are mentioned below:

Chrome: Yes

Microsoft Edge: Yes – version 12 needed

Firefox: Yes – version 3.5 needed

Safari: Yes

Opera: Yes