: The Media or Image Source element - HTML: HyperText Markup Language | MDN (original) (raw)

Baseline

Widely available *

The <source> HTML element specifies one or more media resources for the , , and elements. It is a void element, which means that it has no content and does not require a closing tag. This element is commonly used to offer the same media content in multiple file formats in order to provide compatibility with a broad range of browsers given their differing support for image file formats and media file formats.

Try it

<video controls width="250" height="200" muted>
  <source src="/shared-assets/videos/flower.webm" type="video/webm" />
  <source src="/shared-assets/videos/flower.mp4" type="video/mp4" />
  Download the
  <a href="/shared-assets/videos/flower.webm">WEBM</a>
  or
  <a href="/shared-assets/videos/flower.mp4">MP4</a>
  video.
</video>

Attributes

This element supports all global attributes. In addition, the following attributes can be used with it:

type

Specifies the MIME media type of the image or other media type, optionally including a codecs parameter.

src

Specifies the URL of the media resource. Required if the parent of <source> is or . Not allowed if the parent is .

srcset

Specifies a comma-separated list of one or more image URLs and their descriptors. Required if the parent of <source> is . Not allowed if the parent is or .

The list consists of strings separated by commas, indicating a set of possible images for the browser to use. Each string is composed of:

Each string in the list must have either a width descriptor or a pixel density descriptor to be valid. These two descriptors should not be used together; only one should be used consistently throughout the list. The value of each descriptor in the list must be unique. The browser chooses the most adequate image to display at a given point of time based on these descriptors. If the descriptors are not specified, the default value used is 1x. If the sizes attribute is also present, then each string must include a width descriptor. If the browser does not support srcset, then src will be used for the default image source.

sizes

Specifies a list of source sizes that describe the final rendered width of the image. Allowed if the parent of <source> is . Not allowed if the parent is or .

The list consists of source sizes separated by commas. Each source size is media condition-length pair. Before laying the page out, the browser uses this information to determine which image defined in srcset to display. Note that sizes will take effect only if width descriptors are provided with srcset, not pixel density descriptors (i.e., 200w should be used instead of 2x).

media

Specifies the media query for the resource's intended media.

height

Specifies the intrinsic height of the image in pixels. Allowed if the parent of <source> is a . Not allowed if the parent is or .

The height value must be an integer without any units.

width

Specifies the intrinsic width of the image in pixels. Allowed if the parent of <source> is a . Not allowed if the parent is or .

The width value must be an integer without any units.

Usage notes

The <source> element is a void element, which means that it not only has no content but also has no closing tag. That is, you never use </source> in your HTML.

The browser goes through a list of <source> elements to find a format it supports. It uses the first one it can display. For each <source> element:

If none of the <source> elements provide a usable source:

For information about image formats supported by web browsers and guidance on selecting appropriate formats to use, see our Image file type and format guide. For details on the video and audio media types you can use, see the Media type and format guide.

Examples

Using the type attribute with

This example demonstrates how to offer a video in different formats: WebM for browsers that support it, Ogg for those that support Ogg, and QuickTime for browsers that support QuickTime. If the <audio> or <video> element is not supported by the browser, a notice is displayed instead. If the browser supports the element but does not support any of the specified formats, an error event is raised on the <audio> or <video> element and the default media controls (if enabled) will indicate an error. For more details on which media file formats to use and their browser support, refer to our Media type and format guide.

<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 HTML video.
</video>

Using the media attribute with

This example demonstrates how to offer an alternate source file for viewports above a certain width. When a user's browsing environment meets the specified media condition, the associated <source> element is chosen. The contents of its src attribute are then requested and rendered. If the media condition does not match, the browser will move on to the next <source> in the list. The second <source> option in the code below has no media condition, so it will be selected for all other browsing contexts.

<video controls>
  <source src="foo-large.webm" media="(min-width: 800px)" />
  <source src="foo.webm" />
  I'm sorry; your browser doesn't support HTML video.
</video>

For more examples, the HTML video and audio article in the Learn area is a great resource.

Using the media attribute with

In this example, two <source> elements are included within , providing versions of an image to use when the available space exceeds certain widths. If the available width is less than the smallest of these widths, the browser will fall back to the image specified in the element.

<picture>
  <source srcset="mdn-logo-wide.png" media="(min-width: 800px)" />
  <source srcset="mdn-logo-medium.png" media="(min-width: 600px)" />
  <img src="mdn-logo-narrow.png" alt="MDN Web Docs" />
</picture>

With the <picture> element, you must always include an <img> with a fallback image. Also, make sure to add an alt attribute for accessibility, unless the image is purely decorative and irrelevant to the content.

Using height and width attributes with

In this example, three <source> elements with height and width attributes are included in a element. A media query allows the browser to select an image to display with the height and width attributes based on the viewport size.

<picture>
  <source
    srcset="landscape.png"
    media="(min-width: 1000px)"
    width="1000"
    height="400" />
  <source
    srcset="square.png"
    media="(min-width: 800px)"
    width="800"
    height="800" />
  <source
    srcset="portrait.png"
    media="(min-width: 600px)"
    width="600"
    height="800" />
  <img
    src="fallback.png"
    alt="Image used when the browser does not support the sources"
    width="500"
    height="400" />
</picture>

Technical summary

Content categories None.
Permitted content None; it is a void element.
Tag omission Must have a start tag and must not have an end tag.
Permitted parents A media element— or—and it must be placed before anyflow content or element. A element, and it must be placed before the element.
Implicit ARIA role No corresponding role
Permitted ARIA roles No role permitted
DOM interface HTMLSourceElement

Specifications

Specification
HTML # the-source-element

Browser compatibility

See also