--- title: Source slug: Web/HTML/Element/source translation_of: Web/HTML/Element/source ---
HTML <source>
元素为 {{HTMLElement("picture")}}, {{HTMLElement("audio")}} 或者 {{HTMLElement("video")}} 元素指定多个媒体资源。这是一个空元素。它通常用于以不同浏览器支持的多种格式提供相同的媒体内容。
Usage Context |
A media element ({{HTMLElement("audio")}} or {{HTMLelement("video")}}, and it should be placed before any flow content or {{HTMLElement("track")}} element. |
---|---|
Permitted parent elements | A {{HTMLElement("picture")}} element, and it should be placed before the {{HTMLElement("img")}} element. |
Content categories | None. |
Permitted content | None, it is an {{Glossary("empty element")}}. |
Tag omission | It must have start tag, but must not have an end tag. |
DOM interface | {{domxref("HTMLSourceElement")}} |
该元素包含 全局属性.
sizes
attribute has an effect only when the {{HTMLElement("source")}} element is the direct child of a {{HTMLElement("picture")}} element.<source>
element is placed inside a {{HTMLElement("picture")}} element.'w'
. The default value, if missing, is the infinity.'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 {{HTMLElement("source")}} element is the direct child of a {{HTMLElement("picture")}} element.
codecs
parameter. See RFC 4281 for information about how to specify codecs.If the type attribute isn't specified, the media's type is retrieved from the server and checked to see if Gecko 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 Gecko can play, and if it's not recognized, the server doesn't even get queried; instead, the next source element is checked at once.
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 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. See also the list of media formats supported by the audio and video elements in various 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, see Using audio and video in Firefox.
Specification | Status | Comment |
---|---|---|
{{SpecName('HTML WHATWG', 'edits.html#the-source-element-when-used-with-the-picture-element', '<source>')}} | {{Spec2('HTML WHATWG')}} | Initial definition of <source> used inside a {{HTMLElement("picture")}} element. |
{{SpecName('HTML WHATWG', 'the-video-element.html#the-source-element', '<source>')}} | {{Spec2('HTML WHATWG')}} | Initial definition of <source> used inside a media element, {{HTMLElement("audio")}} or {{HTMLElement("video")}}. |
{{HTMLRef}}