×

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


Related Topics

HTML Reset Button

The HTML reset button is used to reset all values in the form. It clears all input fields. Reset is the value of the type attribute of the button element. The...

6 minutes read.

HTML <style> tag

The section of information that we want to style is contained in html style tag. It includes CSS, which is used to design the text and images in the web page. Syntax: <head> <title>…………....

3 minutes read.

Difference between HTML and DHTML

Introduction to HTML HTML stands for Hypertext Markup Language. It is a standard web language used to create web pages and applications. HTML is used to define the structure and content...

4 minutes read.

HTML Form Action

The <form> tag has an attribute named action that contains a URL. The HTML action attribute defines the webpage URL on which you want to send the data after submitting...

4 minutes read.

HTML Anchor

HTML Anchor The HTML anchor tags define a hyperlink that links to other pages on one page. It can create hyperlinks to any web page and files, locations or any other...

3 minutes read.

HTML <u> tag

Underline Element/Tag: The u tag in html is used to specify the important text or phrase by underline. In earlier HTML versions, this element was known as the "Underline" element, and...

5 minutes read.

HTML Text Format

Introduction HTML may format text in two different ways. The HTML tags define a text property to make it appear one way or another. A text element may also be formatted...

7 minutes read.

HTML Audio

HTML AUDIO: HTML5 provides a method to embed the audio file into a webpage by using HTML5. Before HTML5 audio files is used to play on the browser with the help of...

1 minute read.

HTML Hide Element

The HTML hide element is used to hide the element. We place the hidden attribute inside those elements which we want to hide. The hidden attribute is a Boolean attribute. The...

3 minutes read.

HTML <body> tag

HTML means Hyper Text Markup Language, which is most preferred for making Web pages. It explains how a Web page is put together. There are many different elements in HTML,...

3 minutes read.

HTML <br> tag

HTML is referred to as Hyper Text Markup Language, which is the most popular language for creating Web pages. HTML explains the construction of a Web page. It contains a...

4 minutes read.

HTML5 Button Tag

HTML Button Tag The < button > tag is used on the webpage to construct a clickable button inside the HTML. Within the < button>....</button > tag, we can place content...

2 minutes read.

HTML Form Input Types

HTML Form Input Types An important element of the HTML form is < input type= "> in HTML. The type attribute of the input element can be of different types, which...

8 minutes read.

HTML Links

It consist of anchor element which is used to create a link between a source anchor and destination anchor. The anchor tag can be embedded between text, image, video or...

2 minutes read.

HTML5 Attribute

HTML Attribute HTML attributes are special words that provide additional information about the HTML element or attributes.The element or tags have attributes, which define the element's behavior.It also adds attributes with a start...

3 minutes read.

HTML Youtube

<object> element: It embed the object within the HTML documents and embed plug-ins such as Java applets, PDF readers, Flash Players etc in web pages. Example: <object width="100" height="100" data="bookmark.swf"></object> <embed> element: It defines an embedded...

1 minute read.

How to change text color in HTML

Changing the text colour of a web page is essential because it enhances readability. There are several ways to change the colour of the text in HTML, which are as follows: Text...

7 minutes read.

HTML Layouts

HTML Layouts HTML templates provide a way for well-managed, well-structured responsive web pages to be organized. We could say that the HTML layout specifies how web pages can be set up....

4 minutes read.

HTML &lt;body> alink Attribute

HTML <body> alink Attribute What is HTML? The HTML or HyperText Markup Language is used to design web pages. The file extension name for HTML file is ".html". It was released in...

2 minutes read.

Caption Tag in HTML

Caption Tag in HTML: The < caption > tag in HTML describes the heading of a table in the HTML text. Browsers typically make the text found above the table...

4 minutes read.