<source>: The Media or Image Source element
BaselineWidely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.
The<source>
HTML element specifies one or more media resources for the<picture>
,<audio>
, and<video>
elements. It is avoid 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 forimage file formats andmedia 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 allglobal attributes. In addition, the following attributes can be used with it:
type
Specifies theMIME media type of the image orother media type, optionally including a
codecs
parameter.src
Specifies the URL of the media resource. Required if the parent of
<source>
is<audio>
or<video>
. Not allowed if the parent is<picture>
.srcset
Specifies a comma-separated list of one or more image URLs and their descriptors. Required if the parent of
<source>
is<picture>
. Not allowed if the parent is<audio>
or<video>
.The list consists of strings separated by commas, indicating a set of possible images for the browser to use. Each string is composed of:
- A URL specifying an image location.
- An optional width descriptor—a positive integer directly followed by
"w"
, such as300w
. - An optional pixel density descriptor—a positive floating number directly followed by
"x"
, such as2x
.
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 thesizes
attribute is also present, then each string must include a width descriptor. If the browser does not supportsrcset
, thensrc
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<picture>
. Not allowed if the parent is<audio>
or<video>
.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 thatsizes
will take effect only if width descriptors are provided withsrcset
, not pixel density descriptors (i.e.,200w
should be used instead of2x
).media
Specifies themedia 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<picture>
. Not allowed if the parent is<audio>
or<video>
.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<picture>
. Not allowed if the parent is<audio>
or<video>
.The width value must be an integer without any units.
Usage notes
The<source>
element is avoid element, which means that it not only has no content but also has no closing tag. That is, younever 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 the
type
attribute isn't specified, the browser retrieves the media's type from the server and determines if it can be displayed. If the media can't be rendered, the browser checks the next<source>
in the list. - If the
type
attribute is specified, the browser immediately compares it with the media types it can display. If the type is not supported, the browser skips querying the server and directly checks the next<source>
element.
If none of the<source>
elements provide a usable source:
- In the case of a
<picture>
element, the browser will fall back to using the image specified in the<picture>
element's<img>
child. - In the case of an
<audio>
or<video>
element, the browser will fall back to displaying the content included between the element's opening and closing tags.
For information about image formats supported by web browsers and guidance on selecting appropriate formats to use, see ourImage file type and format guide. For details on the video and audio media types you can use, see theMedia type and format guide.
Examples
Using thetype
attribute with<video>
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, anerror
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 ourMedia 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 themedia
attribute with<video>
This example demonstrates how to offer an alternate source file for viewports above a certain width. When a user's browsing environment meets the specifiedmedia
condition, the associated<source>
element is chosen. The contents of itssrc
attribute are then requested and rendered. If themedia
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 nomedia
condition, so it will be selected for all other browsing contexts.
<video controls> <source src="foo-large.webm" media="(width >= 800px)" /> <source src="foo.webm" /> I'm sorry; your browser doesn't support HTML video.</video>
For more examples, theHTML video and audio article in the Learn area is a great resource.
Using themedia
attribute with<picture>
In this example, two<source>
elements are included within<picture>
, 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<img>
element.
<picture> <source srcset="mdn-logo-wide.png" media="(width >= 800px)" /> <source srcset="mdn-logo-medium.png" media="(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 analt
attribute for accessibility, unless the image is purely decorative and irrelevant to the content.
Usingheight
andwidth
attributes with<picture>
In this example, three<source>
elements withheight
andwidth
attributes are included in a<picture>
element.Amedia query allows the browser to select an image to display with theheight
andwidth
attributes based on theviewport size.
<picture> <source srcset="landscape.png" media="(width >= 1000px)" width="1000" height="400" /> <source srcset="square.png" media="(width >= 800px)" width="800" height="800" /> <source srcset="portrait.png" media="(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 avoid element. |
Tag omission | Must have a start tag and must not have an end tag. |
Permitted parents | |
Implicit ARIA role | No corresponding role |
Permitted ARIA roles | Norole permitted |
DOM interface | HTMLSourceElement |
Specifications
Specification |
---|
HTML # the-source-element |