Tag <source> In Html

In this post, we will discuss about Tag source through examples in the expectation that it will satisfy our requirements.

The <source> tag in HTML is used to specify multiple media resources for an HTML media element, such as <video> or <audio>.

A player for audio files with two sources. Browsers will select the first source they support:

Example: 

<!DOCTYPE html> <html> <body> <audio controls> <source src="https://mrexamples.com/wp-content/uploads/2023/04/fat-kick-drumloop-99bpm-141016.mp3" type="audio/ogg"> <source src="https://mrexamples.com/wp-content/uploads/2023/04/fat-kick-drumloop-99bpm-141016.mp3" type="audio/mpeg"> Your browser does not support the audio element. </source></source></audio> </body> </html>


Tag <source> Usage

A Tag <source> is employed to describe a variety of media sources for various media types, for example <video>, <audio> and <pictures>.

By choosing an alternate video/audio/image file in <source> tag, you can determine which file the browser will choose depending on the browser’s support or viewport width. It will select the first source it supports.

Use <source> within <video> to play a video:

Example: 

<!DOCTYPE html> <html> <body> <video width="320" height="240" controls> <source src="https://mrexamples.com/wp-content/uploads/2023/03/Tesla-Factory-Tour-with-Elon-Musk.mp4" type="video/mp4"> <source src="https://mrexamples.com/wp-content/uploads/2023/03/Tesla-Factory-Tour-with-Elon-Musk.mp4" type="video/ogg"> Your browser is not compatible with the video tag. </source></source></video> </body> </html>

The can be used within an image to define different images based on the viewport size, as follows:

Example: 

<!DOCTYPE html> <html> <body> <picture> <source media="(min-width:650px)" srcset="https://mrexamples.com/wp-content/uploads/2023/03/79f12b03b5c281678959d0019cc90405.jpg"> <source media="(min-width:465px)" srcset="https://mrexamples.com/wp-content/uploads/2023/03/outerspace-58.gif"> <img src="https://mrexamples.com/wp-content/uploads/2023/03/outerspace-58.gif" alt="elonmusk" style="width:auto;"> </source></source></picture> </body> </html>

The following code will display a video player with different sources for different video quality based on the user’s internet speed:

Example: 

<!DOCTYPE html> <html> <body> <video controls> <source src="https://mrexamples.com/wp-content/uploads/2023/03/Tesla-Factory-Tour-with-Elon-Musk.mp4" type="video/mp4" data-quality="high" media="(min-width: 800px)"> <source src="https://mrexamples.com/wp-content/uploads/2023/03/Tesla-Factory-Tour-with-Elon-Musk.mp4" type="video/mp4" data-quality="low" media="(max-width: 799px)"> <p>Your browser is not compatible with the video tag.</p> </source></source></video> </body> </html>

Key Features of Source Tag

Here are some key features of the <source> tag:

  • The <source> tag allows developers to specify multiple sources for a media element, such as different file formats or different resolutions. This ensures that the media can be played on a wide range of devices and platforms.
  • The <source> tag also allows developers to specify an order of preference for the media sources. This means that the media element will attempt to play the first source first, and if that is not supported, it will move on to the next source in the list.
  • The <source> tag requires a “type” attribute that specifies the MIME type of the media resource. This ensures that the browser can properly interpret and play the media.
  • In case none of the sources specified in the <source> tag are supported by the browser, developers can include fallback content within the media element using the <video> or <audio> tags.
  • By providing multiple sources for a media element, developers can ensure that the content is accessible to users with different devices and network capabilities. This can help to improve the overall user experience and ensure that the content is accessible to a wider audience.

Attributes

Global

The Tag source <source> also accepts the Global Attributes in HTML.

Event

Tag source <source> also accommodates the Event Attributes in HTML.


Attributes List

AttributesValueOverview
mediamedia_queryIt supports any kind of media query that is typically specified in CSS.
sizesSets the size of images for different page layouts.
srcURLIt is essential when <source> is used in <audio> and <video>. Provide the URL of the media file.
srcsetURLThis is needed whenever <source> is used in <picture>. Provide the URL for a specific image depending on the situation.
typeMIME-typeIndicates the MIME-type of the resource.

Browser Compatibility

Numbers in the table indicate the earliest browser version fully compatible with the element.

Element
<source>4.09.03.54.010.5

HTML Source Tag Benefits

The <source> tag in HTML provides several benefits, some of them are given below :

  • The <source> tag allows developers to provide multiple versions of a media file, such as different video file formats or audio codecs, to ensure that the media can be played on a wide range of devices and platforms.
  • Developers can use the <source> tag to specify an order of preference for the media files, allowing the browser to select the best option for the user’s device and network conditions.
  • By providing multiple versions of a media file, developers can ensure that the content is accessible to users with different devices, screen sizes, and network speeds.
  • The <source> tag also provides a way to provide fallback content, such as a message or an image, in case none of the media files specified in the tag are supported by the browser.
  • Using the <source> tag can help improve the performance of media-heavy web pages by ensuring that the correct version of the media file is loaded for the user’s device and network conditions, reducing the load time and improving the overall user experience.
Do leave your feedback regarding this article by leaving a reaction below in order to encourage our efforts or to suggest us with some ideas to improve this site.
We value your feedback.
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0
+1
0

Subscribe To Our Newsletter
Enter your email to receive a weekly round-up of our best posts. Learn more!
icon

Leave a Reply

Your email address will not be published. Required fields are marked *