<source>: The Media or Image Source element #
::: section-content
The <source>
HTML element specifies multiple media
resources for the
<picture>
, the
<audio>
element, or the
<video>
element. It is a
void
element,
meaning that it has no content and does not have a closing tag. It 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 #
::: section-content ::: iframe ::: {.output-header .border-rounded-top}
HTML Demo: <source> #
Reset :::
::: {#warning-no-script .warning-container} ::: warning The interactive example cannot be shown because JavaScript is disabled. ::: :::
::: {#warning-mathml-not-supported .warning-container .hidden} ::: warning The interactive example cannot be shown because MathML is not supported by your browser. ::: :::
::: {#editor-container .editor-container .tabbed-standard .hidden .border-rounded-bottom editor-type=“tabbed”} ::: {#tab-container .section .tabs} ::: {#tablist .tab-list role=“tablist”} HTML
CSS
JavaScript :::
::: {#html-panel .section .hidden tabindex=“0” role=“tabpanel” aria-labelledby=“html” aria-hidden=“true”} ::: {#html-editor} ::: :::
::: {#css-panel .section .hidden tabindex=“0” role=“tabpanel” aria-labelledby=“css” aria-hidden=“true”} ::: {#css-editor} ::: :::
::: {#js-panel .section .hidden tabindex=“0” role=“tabpanel” aria-labelledby=“js” aria-hidden=“true”} ::: {#js-editor} ::: ::: :::
::: {#output .output-container}
Output #
::: :::
::: {.section .console-container .hidden aria-hidden=“true”}
Console Output #
![] clear console
::: {#console .console} ::: :::
::: {#html-output .output .editor-tabbed} %html-content% ::: :::
Content categories | None. |
---|---|
Permitted content | None; it is a void element. |
Tag omission | It must have a start tag, but must not have an end tag. |
Permitted parents | 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. |
Implicit ARIA role | No corresponding role |
Permitted ARIA roles | No role permitted |
DOM interface | HTMLSourceElement |
Attributes #
::: section-content This element includes the global attributes.
type
The MIME media type of the image or other media type, optionally with a
codecs
parameter.src
Required if the
source
element's parent is an<audio>
and<video>
element, but not allowed if thesource
element's parent is a<picture>
element.Address of the media resource.
srcset
Required if the
source
element's parent is a<picture>
element, but not allowed if thesource
element's parent is an<audio>
or<video>
element.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:
- One URL specifying an image.
- A width descriptor, which consists of a string containing a
positive integer directly followed by
"w"
, such as300w
. The default value, if missing, is the infinity. - A pixel density descriptor, that is a positive floating number
directly followed by
"x"
. The default value, if missing, is1x
.
Each string in the list must have at least a width descriptor or a pixel density descriptor to be valid. The two types of descriptors should not be mixed together and only one should be used consistently throughout the list. Among the list, the value of each descriptor must be unique. The browser chooses the most adequate image to display at a given point of time. If the
sizes
attribute is present, then a width descriptor must be specified for each string. If the browser does not supportsrcset
, thensrc
will be used for the default source.sizes
Allowed if the
source
element's parent is a<picture>
element, but not allowed if thesource
element's parent is an<audio>
or<video>
element.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. Before laying the page out, the browser uses this information to determine which image is defined in
srcset
to use. Please note thatsizes
will have its effect only if width dimension descriptors are provided withsrcset
instead of pixel ratio values (200w instead of 2x for example).media
Media query of the resource's intended media.
height
Allowed if the
source
element's parent is a<picture>
element, but not allowed if thesource
element's parent is an<audio>
or<video>
element.The intrinsic height of the image, in pixels. Must be an integer without a unit.
width
Allowed if the
source
element's parent is a<picture>
element, but not allowed if thesource
element's parent is an<audio>
or<video>
element.The intrinsic width of the image in pixels. Must be an integer without a unit.
If the type
attribute isn't specified, the media's type is retrieved
from the server and checked to see if the user agent can handle it; if
it can't be rendered, the next <source>
is checked. If the type
attribute is specified, it's compared against the types the user agent
can present, and if it's not recognized, the server doesn't even get
queried; instead, the next <source>
element is checked at once.
When used in the context of a <picture>
element, the browser will fall
back to using the image specified by the <picture>
element's
<img>
child if it is unable to find a suitable image to use
after examining every provided <source>
.
:::
Usage notes #
::: section-content
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.
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 on the web. For details on the video and audio media types you can use, see the Guide to media types formats used on the web. :::
Examples #
Video with type attribute example #
::: section-content
This example demonstrates how to offer a video in WebM format for users
whose browsers support WebM format, Ogg format for users whose browsers
support Ogg format, and a QuickTime format video for users whose
browsers support that. 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 and the default media controls (if enabled)
will indicate an error. Be sure to reference our
guide to media types
and formats on the
web for
details on what media file formats you can use and how well they're
supported by browsers.
::: code-example [html]{.language-name}
<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>
::: :::
Video with media attribute example #
::: section-content
This example demonstrates how to offer an alternate source file for
viewports at or above a certain width. When a user's browsing
environment meets the specified media
condition, the associated
<source>
element is chosen, and the contents of its src
attribute
are requested and rendered. If the media
condition does not match, the
user agent will move on to the next <source>
in the source order. The
second source option in the below code has no media
condition, so will
be selected for all other browsing contexts.
::: code-example [html]{.language-name}
<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 learning area article Video and audio content is a great resource. :::
Picture example #
::: section-content
In this example, two <source>
elements are included within the
<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 user agent will fall back to the
image given by the
<img>
element.
::: code-example [html]{.language-name}
<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, with an alt
attribute to ensure accessibility (unless
the image is an irrelevant background decorative image).
:::
Picture with height & width attributes example #
::: section-content
In this example, three <source>
elements with height
and width
attributes are included in a
<picture>
element. A
media
query
allows the browser to select an image to display with the height
and
width
attributes based on the
viewport
size.
::: code-example [html]{.language-name}
<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>
::: :::
Specifications #
::: _table #
Specification #
HTML Standard
[#
the-source-element]{.small}
:::
Browser compatibility #
::: _table #
Desktop Mobile
Chrome Edge Firefox Internet Opera Safari WebView Chrome Android Firefox for Android Opera Android Safari Samsung
Explorer Android on IOS Internet
source
3 12 3.5 9 15 3.1 4.4 18 4 14 2 1.0
Until Firefox 15, Firefox picked Until Firefox 15, Firefox picked
the first source element that has a the first source element that has a
type matching the MIME-type of a type matching the MIME-type of a
supported media format; see [bug supported media format; see [bug
449363](https://bugzil.la/449363) 449363](https://bugzil.la/449363)
for details. for details.
height
90 90 108 9 76 15 90 90 108 64 15 15.0
media
3 12 15 9 15 3.1 4.4 18 15 14 2 1.0
sizes
38 ≤18 38 No 25 9.1 38 38 38 25 9.3 3.0
34--38 21--25 37--38 34--38 21--25 2.0--3.0
Supports a Supports a Supports a Supports a Supports a Supports a
subset of the subset of the subset of the subset of the subset of the subset of the
syntax for syntax for syntax for syntax for syntax for syntax for
resolution resolution resolution resolution resolution resolution
switching switching switching switching switching switching
(using the `x` (using the `x` (using the `x` (using the `x` (using the `x` (using the `x`
descriptor), descriptor), descriptor), descriptor), descriptor), descriptor),
but not the but not the but not the but not the but not the but not the
full syntax full syntax full syntax full syntax full syntax full syntax
that can be that can be that can be that can be that can be that can be
used with used with used with used with used with used with
`sizes` (using `sizes` (using `sizes` (using `sizes` (using `sizes` (using `sizes` (using
the `w` the `w` the `w` the `w` the `w` the `w`
descriptor). descriptor). descriptor). descriptor). descriptor). descriptor).
src
3 12 3.5 9 15 3.1 4.4 18 4 14 2 1.0
srcset
38 ≤18 38 No 25 9.1 38 38 38 25 9.3 3.0
34--38 21--25 37--38 34--38 21--25 2.0--3.0
Supports a Supports a Supports a Supports a Supports a Supports a
subset of the subset of the subset of the subset of the subset of the subset of the
syntax for syntax for syntax for syntax for syntax for syntax for
resolution resolution resolution resolution resolution resolution
switching switching switching switching switching switching
(using the `x` (using the `x` (using the `x` (using the `x` (using the `x` (using the `x`
descriptor), descriptor), descriptor), descriptor), descriptor), descriptor),
but not the but not the but not the but not the but not the but not the
full syntax full syntax full syntax full syntax full syntax full syntax
that can be that can be that can be that can be that can be that can be
used with used with used with used with used with used with
`sizes` (using `sizes` (using `sizes` (using `sizes` (using `sizes` (using `sizes` (using
the `w` the `w` the `w` the `w` the `w` the `w`
descriptor). descriptor). descriptor). descriptor). descriptor). descriptor).
type
3 12 3.5 9 15 3.1 4.4 18 4 14 2 1.0
width
90 90 108 9 76 15 90 90 108 64 15 15.0
#
:::
See also #
::: section-content
- Guide to media types and formats on the web
- Image file type and format guide
<picture>
element<audio>
element<video>
element- Web Performance :::
::: _attribution
© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5
or later.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/source{._attribution-link}
:::