HTML <source> Tag: Usage, Attributes, and Real-World Examples

By Cristian G. Guasch •  Updated: 09/25/23 •  9 min read

I’m diving into a much-overlooked HTML element today: the <source> tag. It’s an incredibly useful tool in your web development arsenal, allowing you to specify multiple media resources for media elements like <picture>, <audio>, and <video>. This means you can cater to different screen sizes, resolutions, and browser capabilities – all with a single line of code!

The power of the <source> tag truly comes alive when you start using its attributes. For example, the ‘srcset’ attribute lets you define different image sources based on viewport width. And then there’s ‘media’, which allows conditional loading based on media queries. I’ll be providing detailed explanations and examples of these attributes further along.

But that’s just scratching the surface; there are many other ways to leverage this versatile HTML element. So stick around as we delve deeper into using the HTML <source> tag effectively in your projects.

Understanding the HTML <source> Tag

Diving into the depths of HTML coding, you’ll frequently encounter a number of tags. One such tag that often pops up is the <source> tag. So what exactly does it do? It’s an important tool in any web developer’s kit, providing multiple media resources for media elements like <picture>, <audio>, and <video>.

The true value of this tag lies in its versatility. The magic happens when different types of files need to be loaded depending on various conditions. For example:

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

In this snippet, two versions of a video are provided: an MP4 and an OGG file. Now here’s where it gets interesting: if your browser can’t play MP4 files, it’ll automatically switch to using the OGG one instead.

But hang on — there’s more! Did you know that the <source> attributes extend beyond simply src and type? You’ve also got media and sizes. With these at your disposal, you can specify certain parameters for your source files to meet.

Let me illustrate with another code example:

  <source media="(min-width:650px)" srcset="img_pink_flowers.jpg">
  <source media="(min-width:465px)" srcset="img_white_flower.jpg">
  <img src="img_orange_flowers.jpg" alt="Flowers" style="width:auto;">

Here we have a picture element with three sources available. Depending on the width of your viewport (the visible area in a webpage), different images will load accordingly – pink flowers for 650 pixels and above, white ones for 465-649 pixels, and orange flowers otherwise.

Be careful not to mix up src and srcset though! They’re used in different contexts: src for <audio> or <video>, and srcset specifically for the <picture> element.

As you can see, the HTML <source> tag is no mere bystander in the world of web development. It’s a flexible friend that helps ensure your media elements are as accessible and adaptable as possible to all kinds of browsing conditions. Don’t underestimate its power!

Attributes of the HTML <source> Tag

Diving headfirst into the world of HTML, let’s clarify some key facts about the <source> tag. This little piece of code is a real powerhouse when it comes to multimedia content.

First off, there are two primary attributes most commonly used with this tag: srcset and media. The srcset attribute helps you specify multiple images or video resolutions. It’s an absolute gem for responsive web design! Here’s how you can use it:

  <source srcset="img_panda.jpg" media="(min-width: 650px)">
  <img src="img_panda.jpg" alt="Panda">

In this example, if the viewport is at least 650 pixels wide, the browser will load ‘img_panda.jpg’. Otherwise, it’ll fall back to whatever image specified in the <img> tag.

The second attribute is media, which works hand-in-hand with srcset. It specifies what media or device the linked resource is optimized for. So paired together, they make sure your site looks fantastic on any screen size!

Another attribute worth mentioning is type. While not as frequently used as its counterparts, it still plays a vital role. The type attribute indicates what kind of content we’re working with – whether that be image/jpeg or video/mp4. Let me show you how:

<source src="video.mp4" type="video/mp4">

However, there’s a caveat I need to mention here. Sometimes developers mistakenly use wrong MIME types causing browsers to ignore their resources altogether. For instance:

<source src="video.mp4" type="audio/mpeg">

In this faulty code snippet above, our video file won’t play because we’ve incorrectly labeled it as an audio file! So, remember to double-check your code and avoid such common mistakes.

HTML tags are like building blocks. And just like with real construction projects, you need the right tools for the job. Understanding how to correctly use attributes of the <source> tag can greatly enhance your web development skills.

How to Use the HTML <source> Tag

So you’re wondering how to use the HTML <source> tag? Let me walk you through it. It’s used within media elements like <audio> and <video>. When a browser can’t play a certain file format, this little guy offers alternative versions of content.

Here’s an example where we use the <source> tag inside a <video> element:

<video controls>
  <source src="myVideo.mp4" type="video/mp4">
  <source src="myVideo.ogg" type="video/ogg">
  Your browser does not support HTML5 video.

In this case, if the MP4 file can’t be played by your viewer’s browser, it’ll try to play the OGG file instead. If both fail? They’ll receive our friendly error message: “Your browser does not support HTML5 video.”

Now, let’s talk about its attributes. The two most commonly used are src and type. The former is pretty straightforward—it sets the path to your source file. The latter specifies what media type that source is.

There are some common pitfalls when using this tag though. For one, always make sure your paths in the src attribute are correct! A wrong path means no content being displayed—it’s that simple. Another common mistake is forgetting to close your tags properly or not using them within their parent media elements.

Remember though, while the <source> tag is useful for providing multiple versions of audio or visual content, it doesn’t work on its own—it needs those parent elements! So don’t just randomly throw it around in your code; think of it as part of a team working together to deliver great multimedia experiences on your website.

Alright, let’s dive right into the practical side of things by taking a look at some real-world examples of HTML <source> tag usage. I’m here to guide you through this journey and hopefully make things a little clearer for you.

Consider a scenario where we’re putting together a multimedia website that needs to support multiple devices and browsers. The HTML <source> tag becomes our best friend in such situations as it allows us to specify multiple media resources for different situations.

Here’s an example:

<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.

In this code snippet, we’ve used two <source> tags within the <video> element each specifying different video formats: MP4 and OGG. If the visitor’s browser doesn’t support MP4, it’ll try loading the OGG video instead. Thus, we’ve ensured broader compatibility with minimal effort.

But hey, mistakes are part of every learning process! One common mistake is forgetting to include the type attribute in your <source> tag. This attribute is crucial because it specifies the media MIME type, assisting the browser in determining if it can play this file or not without having to download any part of it first.

So remember folks – always specify your type!

The versatility of HTML <source> tag isn’t just limited to videos; they’re commonly used with audio elements too! Here’s how:

<audio controls>
  <source src="audio.ogg" type="audio/ogg">
  <source src="audio.mp3" type="audio/mpeg">
  Your browser does not support this audio format.

Just like before, providing alternative sources with different audio formats ensures that our website remains accessible to as many users as possible, regardless of their browser or device capabilities.

There you have it – a few practical examples to illustrate the power and flexibility of the HTML <source> tag. Keep experimenting, keep learning!

Conclusion: Mastering the HTML <source> Tag

I’ve walked you through the various aspects of using the HTML <source> tag. It’s a powerful tool for web developers, enabling us to provide multiple media resources for different devices or screen resolutions.

Let’s take a look back at what we’ve covered:

<video controls>
  <source src="myVideo.mp4" type="video/mp4">

In this example, myVideo.mp4 will play if the browser supports MP4 format.

Where people often trip up is in understanding how browsers select between multiple source tags. They’ll go down the list from top to bottom and pick the first one that matches their capabilities.

One common mistake I see all too often? Failing to provide a fallback option for older browsers that don’t support <source>. An easy fix is adding a text message within your video or audio tag like so:

<video controls>
  <source src="myVideo.mp4" type="video/mp4">
  Your browser does not support HTML5 video.

By now, you should be comfortable getting started with <source>. But remember, mastering any aspect of coding takes practice! So why not start experimenting with this versatile HTML tag today? With time, you’ll be able to leverage its full potential in enhancing your website’s usability across diverse platforms and devices.

Cristian G. Guasch

Hey! I'm Cristian Gonzalez, I created HTML Easy to help you learn HTML easily and fast.

Related articles