I volontari di MDN non hanno ancora tradotto questo articolo in Italiano. Unisciti a noi e traducilo tu stesso.
Puoi anche consultare l’articolo in English (US).
The HTML Video element (<video>
) embeds a media player which supports video playback into the document. You can use <video>
for audio content as well, but the <audio>
element may provide a more appropriate user experience.
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.
The above example shows simple usage of the <video>
element. In a similar manner to the <img>
element, we include a path to the media we want to display inside the src
attribute; we can include other attributes to specify information such as video width and height, whether we want it to autoplay and loop, whether we want to show the browser's default video controls, etc.
The content inside the opening and closing <video></video>
tags is shown as a fallback in browsers that don't support the element.
Browsers don't all support the same video formats; you can provide multiple sources inside nested <source>
elements, and the browser will then use the first one it understands:
<video controls> <source src="myVideo.mp4" type="video/mp4"> <source src="myVideo.webm" type="video/webm"> <p>Your browser doesn't support HTML5 video. Here is a <a href="myVideo.mp4">link to the video</a> instead.</p> </video>
Other usage notes:
- If you don't specify the
controls
attribute, the video won't include the browser's default controls; you can create your own custom controls using JavaScript and theHTMLMediaElement
API. See Creating a cross-browser video player for more details. - To allow precise control over your video (and audio) content,
HTMLMediaElement
s fire many different events. - You can use the
object-position
property to adjust the positioning of the video within the element's frame, and theobject-fit
property to control how the video's size is adjusted to fit within the frame. - To show subtitles/captions along with your video, you can use some JavaScript along with the
<track>
element and the WebVTT format. See Adding captions and subtitles to HTML5 video for more information.
A good general source of information on using HTML <video>
is the Video and audio content beginner's tutorial.
Attributes
Like all other HTML elements, this element supports the global attributes.
autoplay
- A Boolean attribute; if specified, the video automatically begins to play back as soon as it can do so without stopping to finish loading the data.
-
Note: Sites that automatically play audio (or video with an audio track) can be an unpleasant experience for users, so it should be avoided when possible. If you must offer autoplay functionality, you should make it opt-in (requiring a user to specifically enable it). However, this can be useful when creating media elements whose source will be set at a later time, under user control.
To disable video autoplay,
autoplay="false"
will not work; the video will autoplay if the attribute is there in the<video>
tag at all. To remove autoplay the attribute needs to be removed altogether.In some browsers (e.g. Chrome 70.0) autoplay is not working if no
muted
attribute is present.
buffered
- An attribute you can read to determine the time ranges of the buffered media. This attribute contains a
TimeRanges
object. controls
- If this attribute is present, the browser will offer controls to allow the user to control video playback, including volume, seeking, and pause/resume playback.
crossorigin
- This enumerated attribute indicates whether to use CORS to fetch the related image. CORS-enabled resources can be reused in the
<canvas>
element without being tainted. The allowed values are:anonymous
- Sends a cross-origin request without a credential. In other words, it sends the
Origin:
HTTP header without a cookie, X.509 certificate, or performing HTTP Basic authentication. If the server does not give credentials to the origin site (by not setting theAccess-Control-Allow-Origin:
HTTP header), the image will be tainted, and its usage restricted. use-credentials
- Sends a cross-origin request with a credential. In other words, it sends the
Origin:
HTTP header with a cookie, a certificate, or performing HTTP Basic authentication. If the server does not give credentials to the origin site (throughAccess-Control-Allow-Credentials:
HTTP header), the image will be tainted and its usage restricted.
Origin:
HTTP header), preventing its non-tainted used in<canvas>
elements. If invalid, it is handled as if the enumerated keywordanonymous
was used. See CORS settings attributes for additional information. height
- The height of the video's display area, in CSS pixels (absolute values only; no percentages.)
loop
- A Boolean attribute; if specified, the browser will automatically seek back to the start upon reaching the end of the video.
muted
- A Boolean attribute that indicates the default setting of the audio contained in the video. If set, the audio will be initially silenced. Its default value is
false
, meaning that the audio will be played when the video is played. preload
- This enumerated attribute is intended to provide a hint to the browser about what the author thinks will lead to the best user experience with regards to what content is loaded before the video is played. It may have one of the following values:
none
: Indicates that the video should not be preloaded.metadata
: Indicates that only video metadata (e.g. length) is fetched.auto
: Indicates that the whole video file can be downloaded, even if the user is not expected to use it.- empty string: Synonym of the
auto
value.
The default value is different for each browser. The spec advises it to be set to
metadata
.Notes:- The
autoplay
attribute has precedence overpreload
. Ifautoplay
is specified, the browser would obviously need to start downloading the video for playback. - The specification does not force the browser to follow the value of this attribute; it is a mere hint.
intrinsicsize
- This attribute tells the browser to ignore the actual intrinsic size of the image and pretend it’s the size specified in the attribute. Specifically, the image would raster at these dimensions and
naturalWidth
/naturalHeight
on images would return the values specified in this attribute. Explainer, examples poster
- A URL for an image to be shown while the video is downloading. If this attribute isn't specified, nothing is displayed until the first frame is available, then the first frame is shown as the poster frame.
src
- The URL of the video to embed. This is optional; you may instead use the
<source>
element within the video block to specify the video to embed. width
- The width of the video's display area, in CSS pixels (absolute values only; no percentages).
playsinline
- A Boolean attribute indicating that the video is to be played "inline", that is within the element's playback area. Note that the absence of this attribute does not imply that the video will always be played in fullscreen.
Usage notes
Styling with CSS
The <video>
element is a replaced element — its display
value is inline
by default, but its default width and height in the viewport is defined by the video being embedded.
There are no special considerations for styling <video>
; a common strategy is to give it a display
value of block
to make it easier to position, size, etc., and then provide styling and layout information as required. Video player styling basics provides some useful styling techniques.
Detecting track addition and removal
You can detect when tracks are added to and removed from a <video>
element using the addtrack
and removetrack
events. However, these events aren't sent directly to the <video>
element itself. Instead, they're sent to the track list object within the <video>
element's HTMLMediaElement
that corresponds to the type of track that was added to the element:
HTMLMediaElement.audioTracks
- An
AudioTrackList
containing all of the media element's audio tracks. You can add a listener foraddtrack
to this object to be alerted when new audio tracks are added to the element. HTMLMediaElement.videoTracks
- Add an
addtrack
listener to thisVideoTrackList
object to be informed when video tracks are added to the element. HTMLElement.textTracks
- Add an
addtrack
event listener to thisTextTrackList
to be notified when new text tracks are added to the element.
For example, to detect when audio tracks are added to or removed from a <video>
element, you can use code like this:
var elem = document.querySelector("video"); elem.audioTrackList.onaddtrack = function(event) { trackEditor.addTrack(event.track); }; elem.audioTrackList.onremovetrack = function(event) { trackEditor.removeTrack(event.track); };
This code watches for audio tracks to be added to and removed from the element, and calls a hypothetical function on a track editor to register and remove the track from the editor's list of available tracks.
You can also use addEventListener()
to listen for the addtrack
and removetrack
events.
Examples
Simple video example
This example plays a video when activated, providing the user with the browser's default video controls to control playback.
<!-- Simple video example --> <!-- 'Big Buck Bunny' licensed under CC 3.0 by the Blender foundation. Hosted by archive.org --> <!-- Poster from peach.blender.org --> <video controls src="https://archive.org/download/BigBuckBunny_124/Content/big_buck_bunny_720p_surround.mp4" poster="https://peach.blender.org/wp-content/uploads/title_anouncement.jpg?x11217" width="620"> Sorry, your browser doesn't support embedded videos, but don't worry, you can <a href="https://archive.org/details/BigBuckBunny_124">download it</a> and watch it with your favorite video player! </video>
Until the video starts playing, the image provided in the poster
attribute is displayed in its place. If the browser doesn't support video playback, the fallback text is displayed.
Multiple sources example
This example builds on the last one, offering three different sources for the media; this allows the video to be watched regardless of which video codecs are supported by the browser.
<!-- Using multiple sources as fallbacks for a video tag --> <!-- 'Elephants Dream' by Orange Open Movie Project Studio, licensed under CC-3.0, hosted by archive.org --> <!-- Poster hosted by Wikimedia --> <video width="620" controls poster="https://upload.wikimedia.org/wikipedia/commons/e/e8/Elephants_Dream_s5_both.jpg" > <source src="https://archive.org/download/ElephantsDream/ed_1024_512kb.mp4" type="video/mp4"> <source src="https://archive.org/download/ElephantsDream/ed_hd.ogv" type="video/ogg"> <source src="https://archive.org/download/ElephantsDream/ed_hd.avi" type="video/avi"> Your browser doesn't support HTML5 video tag. </video>
First WebM is tried. If that can't be played, then MP4 is tried. Finally, OGG is tried. A fallback message is displayed if the video tag isn't supported, but not if all sources fail.
Server support for video
If the MIME type for the video is not set correctly on the server, the video may not show or show a gray box containing an X (if JavaScript is enabled).
If you use Apache Web Server to serve Ogg Theora videos, you can fix this problem by adding the video file type extensions to "video/ogg" MIME type. The most common video file type extensions are ".ogm", ".ogv", or ".ogg". To do this, edit the "mime.types" file in "/etc/apache" or use the "AddType"
configuration directive in httpd.conf
.
AddType video/ogg .ogm AddType video/ogg .ogv AddType video/ogg .ogg
If you serve your videos as WebM, you can fix this problem for the Apache Web Server by adding the extension used by your video files (".webm" is the most common one) to the MIME type "video/webm" via the "mime.types" file in "/etc/apache" or via the "AddType" configuration directive in httpd.conf
.
AddType video/webm .webm
Your web host may provide an easy interface to MIME type configuration changes for new technologies until a global update naturally occurs.
Accessibility concerns
Videos should provide both captions and transcripts that accurately describe its content (see Adding captions and subtitles to HTML5 video for more information on how to implement these). Captions allow people who are experiencing hearing loss to understand a video's audio content as the video is being played, while transcripts allow people who need additional time to be able to review audio content at a pace and format that is comfortable for them.
If automatic captioning services are used, it is important to review the generated content to ensure it accurately represents the source video.
In addition to spoken dialog, subtitles and transcripts should also identify music and sound effects that communicate important information. This includes emotion and tone:
14 00:03:14 --> 00:03:18 [Dramatic rock music] 15 00:03:19 --> 00:03:21 [whispering] What's that off in the distance? 16 00:03:22 --> 00:03:24 It's… it's a… 16 00:03:25 --> 00:03:32 [Loud thumping] [Dishes clattering]
Captions should not obstruct the main subject of the video. They can be positioned using the align
VTT cue setting.
- MDN Subtitles and closed caption — Plugins
- Web Video Text Tracks Format (WebVTT)
- WebAIM: Captions, Transcripts, and Audio Descriptions
- MDN Understanding WCAG, Guideline 1.2 explanations
- Understanding Success Criterion 1.2.1 | W3C Understanding WCAG 2.0
- Understanding Success Criterion 1.2.2 | W3C Understanding WCAG 2.0
Technical summary
Content categories | Flow content, phrasing content, embedded content. If it has a controls attribute: interactive content and palpable content. |
---|---|
Permitted content |
If the element has a Else: zero or more |
Tag omission | None, both the starting and ending tag are mandatory. |
Permitted parents | Any element that accepts embedded content. |
Permitted ARIA roles | application |
DOM interface | HTMLVideoElement |
Specifications
Specification | Status | Comment |
---|---|---|
HTML Living Standard The definition of '<video>' in that specification. |
Living Standard |
Browser compatibility
Desktop | Mobile | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Basic support | Chrome Full support 3 | Edge Full support Yes | Firefox Full support 3.5 | IE Full support 9 | Opera Full support 10.5 | Safari Full support 3.1 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
autoplay | Chrome Full support 3 | Edge Full support Yes | Firefox Full support 3.5 | IE Full support 9 | Opera Full support 10.5 | Safari Full support 3.1 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS
Full support
10
| Samsung Internet Android Full support Yes |
buffered | Chrome ? | Edge Full support Yes | Firefox Full support 4 | IE ? | Opera Full support Yes | Safari ? | WebView Android ? | Chrome Android ? | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS ? | Samsung Internet Android ? |
controls | Chrome Full support 3 | Edge Full support Yes | Firefox Full support 3.5 | IE Full support 9 | Opera Full support 10.5 | Safari Full support 3.1 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
crossorigin | Chrome ? | Edge Full support Yes | Firefox Full support 12 | IE ? | Opera ? | Safari ? | WebView Android ? | Chrome Android ? | Edge Mobile Full support Yes | Firefox Android Full support 14 | Opera Android ? | Safari iOS ? | Samsung Internet Android ? |
height | Chrome Full support 3 | Edge Full support Yes | Firefox Full support 3.5 | IE Full support 9 | Opera Full support 10.5 | Safari Full support 3.1 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
intrinsicsize | Chrome
Full support
71
| Edge ? | Firefox ? | IE No support No | Opera Full support 58 | Safari No support No | WebView Android
Full support
71
| Chrome Android
Full support
71
| Edge Mobile ? | Firefox Android ? | Opera Android Full support 58 | Safari iOS No support No | Samsung Internet Android ? |
loop | Chrome Full support 3 | Edge Full support Yes | Firefox Full support 11 | IE Full support 9 | Opera Full support 10.5 | Safari Full support 3.1 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 14 | Opera Android Full support Yes | Safari iOS Full support 6 | Samsung Internet Android Full support Yes |
muted | Chrome Full support 30 | Edge Full support Yes | Firefox Full support 11 | IE Full support 10 | Opera Full support Yes | Safari Full support 5 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 14 | Opera Android Full support Yes | Safari iOS ? | Samsung Internet Android Full support Yes |
played | Chrome ? | Edge Full support Yes | Firefox Full support 15 | IE ? | Opera Full support Yes | Safari ? | WebView Android ? | Chrome Android ? | Edge Mobile Full support Yes | Firefox Android Full support 15 | Opera Android Full support Yes | Safari iOS ? | Samsung Internet Android ? |
poster | Chrome Full support 3 | Edge Full support Yes | Firefox Full support 3.6 | IE Full support 9 | Opera Full support 10.5 | Safari Full support 3.1 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
preload | Chrome
Full support
3
| Edge Full support Yes | Firefox Full support 4 | IE Full support 9 | Opera
Full support
Yes
| Safari Full support 3.1 | WebView Android
Full support
Yes
| Chrome Android
Full support
Yes
| Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android
Full support
Yes
| Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
src | Chrome Full support 3 | Edge Full support Yes | Firefox Full support 3.5 | IE Full support 9 | Opera Full support 10.5 | Safari Full support 3.1 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
width | Chrome Full support 3 | Edge Full support Yes | Firefox Full support 3.5 | IE Full support 9 | Opera Full support 10.5 | Safari Full support 3.1 | WebView Android Full support Yes | Chrome Android Full support Yes | Edge Mobile Full support Yes | Firefox Android Full support 4 | Opera Android Full support Yes | Safari iOS Full support Yes | Samsung Internet Android Full support Yes |
Legend
- Full support
- Full support
- No support
- No support
- Compatibility unknown
- Compatibility unknown
- Experimental. Expect behavior to change in the future.
- Experimental. Expect behavior to change in the future.
- Non-standard. Expect poor cross-browser support.
- Non-standard. Expect poor cross-browser support.
- See implementation notes.
- See implementation notes.
- User must explicitly enable this feature.
- User must explicitly enable this feature.
See also
- Media formats supported by the audio and video elements
- Positioning and sizing the picture within its frame:
object-position
andobject-fit
<audio>
- Using HTML5 audio and video
- Manipulating video using canvas
- Configuring servers for Ogg medi