To display a video using HTML, employ the <video>
element:
<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> |
The controls attribute provides video controls such as play, pause, and volume.
Including width and height attributes is recommended to prevent page flickering during video loading if these dimensions are not set.
The <source> element allows specification of alternative video files that browsers can choose from, with the first recognized format being used.
Text between the <video> and </video> tags appears only in browsers that do not support the <video> element.
To initiate automatic playback of a video, utilize 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> |
Note that in Chromium browsers, autoplay is generally restricted. However, muted autoplay is consistently permitted. |
In Chromium browsers, autoplay is typically restricted, but muted autoplay is always allowed.
<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> |
The numbers in the table indicate the earliest browser version that fully supports the <video> element.
The three supported video formats are MP4, WebM, and Ogg. Browser support varies for each of these formats.
Browser |
MP4 |
WebM |
Ogg |
Edge |
YES |
YES |
YES |
Chrome |
YES |
YES |
YES |
Firefox |
YES |
YES |
YES |
Safari |
YES |
YES |
NO |
Opera |
YES |
YES |
YES |
File Format |
Media Type |
MP4 |
video/mp4 |
WebM |
video/webm |
Ogg |
video/ogg |
The HTML DOM provides methods, properties, and events specifically for the <video>
element.
These enable tasks such as loading, playing, and pausing videos, as well as adjusting settings like duration and volume.
Additionally, DOM events are available to inform you when a video starts playing, pauses, and more.
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 |