Believemy logo purple

<source>

Defines an audio track in an HTML page.
Believemy logo

The <source> tag is used as a complement to the <audio>, <video>, and <picture> tags to define alternative multimedia files.

It allows offering multiple formats or resolutions to ensure compatibility with various browsers and devices.

It cannot be used alone and must always be embedded in an appropriate parent tag. 😉

 

Structure and Syntax of the <source> Tag

Structure

Let's take the example of a soundtrack we want to add to our HTML page.

Here’s the code using the <source> tag:

HTML
<audio controls>
  <source src="audio.mp3" type="audio/mpeg">
  <source src="audio.ogg" type="audio/ogg">
  Your browser does not support the audio element.
</audio>

A few explanations:

  • src: Defines the path to the multimedia file;
  • type: Indicates the file's MIME type (e.g., audio/mpeg or video/mp4);
  • "Your browser...": This is the text displayed in case the browser does not support the parent tag or its sources.

 

Usage Examples

Example for <video>

Here’s how it would look when combining the <source> tag with the <video> tag:

HTML
<video controls>
  <source src="video.mp4" type="video/mp4">
  <source src="video.ogg" type="video/ogg">
  Your browser does not support the video element.
</video>

As you can see, nothing is really different from what we saw earlier with the <audio> tag.

 

Example for <picture>

With the <picture> tag, here’s how it would look:

HTML
<picture>
  <source srcset="image-large.jpg" media="(min-width: 800px)">
  <source srcset="image-small.jpg" media="(max-width: 799px)">
  <img src="fallback.jpg" alt="Image description">
</picture>

 

Attributes

Specific Attributes for <source>

Some attributes are specific to the <source> tag:

AttributeDescriptionPossible Value
srcPath to the multimedia file.Valid URL
typeThe file's MIME type (important for browsers).e.g., audio/mpeg, video/mp4, image/jpeg
srcsetList of image URLs (used with <picture>).e.g., image1.jpg, image2.jpg 2x
mediaConditional expression to target specific devices (CSS media query).e.g., (min-width: 600px)
sizesImage size (used with srcset).e.g., 100vw, 50vw

 

General Attributes

The <source> tag also accepts general attributes such as id, class, style, etc.

 

Recommendations

Several things should be considered when using the <source> tag: here are our recommendations!

Always Define the MIME Type

The MIME type helps browsers select the most suitable multimedia file. Forgetting to specify it is like navigating blindfolded for your user’s browser: they might end up seeing your fallback text.

That would be a shame, right? 😋

 

Use Universal Formats

Always prioritize universal formats such as:

  • mp4;
  • mp3;
  • jpeg;
  • png.

 

Add a Fallback Text

As we’ve seen, the fallback text is displayed when none of the formats is compatible with the user’s browser.

If you don’t add one, your user will simply see nothing.

 

Browser Compatibility

The <source> tag is compatible with all browsers.

ElementGoogle ChromeSafariMozilla FirefoxEdge
<source>YesYesYesYes

Discover our hTML and CSS glossary

Browse the terms and definitions most commonly used in HTML and CSS development.