HTML <source> element 指定多媒体资源为 <picture> <audio> 元素,或 <video> 元素。 It is an empty 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 .

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

内容类别 None.
准许内容 None, it is an empty 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> 元素。

A <picture> element, and it must be placed before the <img> 元素。
Implicit ARIA role 无对应角色
Permitted ARIA roles No role permitted
DOM 接口 HTMLSourceElement

属性

此元素包括 全局属性 .

media
Media query of the resource's intended media; this should be used only in a <picture> 元素。
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. Please note that sizes will have its effect only if width dimension descriptors are provided with srcset instead of pixel ratio values (200w instead of 2x for example).

sizes attribute has an effect only when the <source> element is the direct child of a <picture> 元素。

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> 元素。
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 specifying an image.
  2. A width descriptor, which consists of a string containing a positive integer directly followed by "w" ,譬如 300w . 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.

srcset attribute has an effect only when the <source> element is the direct child of a <picture> 元素。

type
MIME media type of the resource , optionally with a codecs 参数 .

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> 元素的 <img> child if it is unable to find a suitable image to use after examing every provided <source> .

用法注意事项

<source> element is an empty element (or 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 .

范例

Video example

This example demonstrates how to offer a video in Ogg format for users whose browsers support Ogg format, and a QuickTime format video for users whose browsers support that. If the audio or 视频 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.

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

For more examples, the learning area article Video and audio content is a great resource.

Picture example

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 smaller of these widths, the user agent will fall back to the image given by the <img> 元素。

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

规范

规范 状态 注释
HTML 实时标准
The definition of '<source>' in that specification.
实时标准

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
桌面 移动
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet
source Chrome 完整支持 Yes Edge 完整支持 12 Firefox 完整支持 3.5 注意事项
完整支持 3.5 注意事项
注意事项 Until Firefox 15, Firefox picked the first source element that has a type matching the MIME-type of a supported media format; see bug 449363 了解细节。
IE 完整支持 9 Opera 完整支持 Yes Safari 完整支持 Yes WebView Android 完整支持 Yes Chrome Android 完整支持 Yes Firefox Android 完整支持 4 注意事项
完整支持 4 注意事项
注意事项 Until Firefox 15, Firefox picked the first source element that has a type matching the MIME-type of a supported media format; see bug 449363 了解细节。
Opera Android ? Safari iOS 完整支持 Yes Samsung Internet Android 完整支持 Yes
media Chrome 完整支持 Yes Edge 完整支持 12 Firefox 完整支持 15 IE 完整支持 9 Opera 完整支持 Yes Safari 完整支持 Yes WebView Android 完整支持 Yes Chrome Android 完整支持 Yes Firefox Android 完整支持 15 Opera Android ? Safari iOS 完整支持 Yes Samsung Internet Android 完整支持 Yes
sizes Chrome 完整支持 Yes Edge 完整支持 ≤18 Firefox 完整支持 38
完整支持 38
完整支持 33 Disabled
Disabled ). To change preferences in Firefox, visit
IE ? Opera 完整支持 Yes Safari 完整支持 9
完整支持 9
部分支持 7
WebView Android 完整支持 Yes Chrome Android 完整支持 Yes Firefox Android 完整支持 38
完整支持 38
完整支持 33 Disabled
Disabled ). To change preferences in Firefox, visit
Opera Android ? Safari iOS 完整支持 9
完整支持 9
部分支持 8
Samsung Internet Android 完整支持 Yes
src Chrome 完整支持 Yes Edge 完整支持 12 Firefox 完整支持 3.5 IE 完整支持 9 Opera 完整支持 Yes Safari 完整支持 Yes WebView Android 完整支持 Yes Chrome Android 完整支持 Yes Firefox Android 完整支持 4 Opera Android ? Safari iOS 完整支持 Yes Samsung Internet Android 完整支持 Yes
srcset Chrome 完整支持 Yes Edge 完整支持 ≤18 Firefox 完整支持 38
完整支持 38
完整支持 33 Disabled
Disabled ). To change preferences in Firefox, visit
IE ? Opera 完整支持 Yes Safari 完整支持 9
完整支持 9
部分支持 7
WebView Android 完整支持 Yes Chrome Android 完整支持 Yes Firefox Android 完整支持 38
完整支持 38
完整支持 33 Disabled
Disabled ). To change preferences in Firefox, visit
Opera Android ? Safari iOS 完整支持 9
完整支持 9
部分支持 8
Samsung Internet Android 完整支持 Yes
type Chrome 完整支持 Yes Edge 完整支持 12 Firefox 完整支持 3.5 IE 完整支持 9 Opera 完整支持 Yes Safari 完整支持 Yes WebView Android 完整支持 Yes Chrome Android 完整支持 Yes Firefox Android 完整支持 4 Opera Android ? Safari iOS 完整支持 Yes Samsung Internet Android 完整支持 Yes

图例

完整支持

完整支持

兼容性未知 ?

兼容性未知

见实现注意事项。

见实现注意事项。

用户必须明确启用此特征。

用户必须明确启用此特征。

另请参阅

元数据

  • 最后修改: