The HTML <video>
element is used to show a video on a web page.
The HTML <video> Element
To show a video in HTML, use the <video>
element:
Example
<video width="320" height="240" controls>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
How it Works
The controls
attribute adds video controls, like play, pause, and volume.
It is a good idea to always include width
and height
attributes. If height and width are not set, the page might flicker while the video loads.
The <source>
element allows you to specify alternative video files which the browser may choose from. The browser will use the first recognized format.
The text between the <video>
and </video>
tags will only be displayed in browsers that do not support the <video>
element.
HTML <video> Autoplay
To start a video automatically, use the autoplay
attribute:
<video width="320" height="240" autoplay> <source src="movie.mp4" type="video/mp4"> <source src="movie.ogg" type="video/ogg"> Your browser does not support the video tag. </video>
Add muted
after
to let your video start playing automatically (but muted):
autoplay
<video width="320" height="240" autoplay muted> <source src="movie.mp4" type="video/mp4"> <source src="movie.ogg" type="video/ogg"> Your browser does not support the video tag. </video>
HTML Video Formats
There are three supported video formats: MP4, WebM, and Ogg. The browser support for the different formats is:
Browser | MP4 | WebM | Ogg |
---|---|---|---|
Edge | YES | YES | YES |
Chrome | YES | YES | YES |
Firefox | YES | YES | YES |
Safari | YES | YES | NO |
Opera | YES | YES | YES |
HTML Video – Media Types
File Format | Media Type |
---|---|
MP4 | video/mp4 |
WebM | video/webm |
Ogg | video/ogg |
HTML Video – Methods, Properties, and Events
The HTML DOM defines methods, properties, and events for the <video>
element.
This allows you to load, play, and pause videos, as well as setting duration and volume.
There are also DOM events that can notify you when a video begins to play, is paused, etc.
HTML Audio/Video Methods
Method | Description |
---|---|
addTextTrack() | Adds a new text track to the audio/video |
canPlayType() | Checks if the browser can play the specified audio/video type |
load() | Re-loads the audio/video element |
play() | Starts playing the audio/video |
pause() | Pauses the currently playing audio/video |
HTML Audio/Video Properties
Property | Description |
---|---|
audioTracks | Returns an AudioTrackList object representing available audio tracks |
autoplay | Sets or returns whether the audio/video should start playing as soon as it is loaded |
buffered | Returns a TimeRanges object representing the buffered parts of the audio/video |
controller | Returns the MediaController object representing the current media controller of the audio/video |
controls | Sets or returns whether the audio/video should display controls (like play/pause etc.) |
crossOrigin | Sets or returns the CORS settings of the audio/video |
currentSrc | Returns the URL of the current audio/video |
currentTime | Sets or returns the current playback position in the audio/video (in seconds) |
defaultMuted | Sets or returns whether the audio/video should be muted by default |
defaultPlaybackRate | Sets or returns the default speed of the audio/video playback |
duration | Returns the length of the current audio/video (in seconds) |
ended | Returns whether the playback of the audio/video has ended or not |
error | Returns a MediaError object representing the error state of the audio/video |
loop | Sets or returns whether the audio/video should start over again when finished |
mediaGroup | Sets or returns the group the audio/video belongs to (used to link multiple audio/video elements) |
muted | Sets or returns whether the audio/video is muted or not |
networkState | Returns the current network state of the audio/video |
paused | Returns whether the audio/video is paused or not |
playbackRate | Sets or returns the speed of the audio/video playback |
played | Returns a TimeRanges object representing the played parts of the audio/video |
preload | Sets or returns whether the audio/video should be loaded when the page loads |
readyState | Returns the current ready state of the audio/video |
seekable | Returns a TimeRanges object representing the seekable parts of the audio/video |
seeking | Returns whether the user is currently seeking in the audio/video |
src | Sets or returns the current source of the audio/video element |
startDate | Returns a Date object representing the current time offset |
textTracks | Returns a TextTrackList object representing the available text tracks |
videoTracks | Deprecated. Do not use it. |
volume | Sets or returns the volume of the audio/video |
HTML Audio/Video Events
Event | Description |
---|---|
abort | Fires when the loading of an audio/video is aborted |
canplay | Fires when the browser can start playing the audio/video |
canplaythrough | Fires when the browser can play through the audio/video without stopping for buffering |
durationchange | Fires when the duration of the audio/video is changed |
emptied | Fires when the current playlist is empty |
ended | Fires when the current playlist is ended |
error | Fires when an error occurred during the loading of an audio/video |
loadeddata | Fires when the browser has loaded the current frame of the audio/video |
loadedmetadata | Fires when the browser has loaded meta data for the audio/video |
loadstart | Fires when the browser starts looking for the audio/video |
pause | Fires when the audio/video has been paused |
play | Fires when the audio/video has been started or is no longer paused |
playing | Fires when the audio/video is playing after having been paused or stopped for buffering |
progress | Fires when the browser is downloading the audio/video |
ratechange | Fires when the playing speed of the audio/video is changed |
seeked | Fires when the user is finished moving/skipping to a new position in the audio/video |
seeking | Fires when the user starts moving/skipping to a new position in the audio/video |
stalled | Fires when the browser is trying to get media data, but data is not available |
suspend | Fires when the browser is intentionally not getting media data |
timeupdate | Fires when the current playback position has changed |
volumechange | Fires when the volume has been changed |
waiting | Fires when the video stops because it needs to buffer the next frame |
HTML Video Tags
Tag | Description |
---|---|
<video> | Defines a video or movie |
<source> | Defines multiple media resources for media elements, such as <video> and <audio> |
<track> | Defines text tracks in media players |