HTML5 <source> Tag

The HTML <source> tag is used to specify multiple media resources for either the <picture>, the <audio> or the <video> element. It is commonly used to serve the same media content in multiple formats supported by different browsers.

No content permitted inside the <source> tag as it is a void element. The start tag is mandatory for this element but there must be no closing tag.

The parent of the <source> tag can be:

  • A media element—<audio> or <video>—and it must be placed before any flow content or <track> element.
  • A <picture> element, and it must be placed before the <img> element.

Here is an example of using this tag:

<video controls>
  <source src="foo.webm" type="video/webm">
  <source src="foo.ogg" type="video/ogg"> 
  <source src="foo.mov" type="video/quicktime">
  I'm sorry; your browser doesn't support HTML5 video.
</video>

Attributes

sizes

Is a list of source sizes that describes the final rendered width of the image represented by the source. Each source size consists of a comma-separated list of media condition-length pairs. This information is used by the browser to determine, before laying the page out, which image defined in srcset to use.
The sizes attribute has an effect only when the <source> element is the direct child of a <picture> element.

src

Required for <audio> and <video>, address of the media resource. The value of this attribute is ignored when the <source> element is placed inside a <picture> element.

srcset

A list of one or more strings separated by commas indicating a set of possible images represented by the source for the browser to use. Each string is composed of:

  1. one URL to an image,
  2. a width descriptor, that is a positive integer directly followed by ‘w’. The default value, if missing, is the infinity.
  3. a pixel density descriptor, that is a positive floating number directly followed by ‘x’. The default value, if missing, is 1x.

Each string in the list must have at least a width descriptor or a pixel density descriptor to be valid. Among the list, there must be only one string containing the same tuple of width descriptor and pixel density descriptor.
The browser chooses the most adequate image to display at a given point of time. The srcset attribute has an effect only when the <source> element is the direct child of a <picture> element.

type

The MIME-type of the resource, optionally with a codecs parameter. See RFC 4281 for information about how to specify codecs.

media

Media query of the resource’s intended media. This should be used only in a <picture> element.

Further Reading

  1. Read more about the HTML <source> element on MDN.
  2. You can also read the HTML <source> tag reference on W3C.

Reader Comments

1. You can register or login to post a comment. Asking readers to register improves the quality of discussion.

2. As a registered user, you will also get to access features like choosing between a light and dark theme or anything else that we might implement in future.

Follow Us For Updates

FacebookTwitterGoogle+
Go To Top