![]() |
VOOZH | about |
The <video> element in HTML is used to show video content on web pages. It supports various video formats, including MP4, WebM, and Ogg. It is introduced in HTML5.
Syntax:
<video src="" controls> </video>
or
<video controls="controls">
<source src="video_filename" type="video_type">
</video>
Generally, we prefer the syntax with the <source> tag wrapped between the video tag because:
The following attributes can be used with the <video> tag to enhance video playback:
| Attribute | Description |
|---|---|
| controls | Adds playback controls such as play, pause, volume, etc. |
| width | Specifies the width of the video player. |
| height | Specifies the height of the video player. |
| autoplay | Automatically starts playing the video when it's loaded. |
| muted | Mutes the video by default. |
| src | Specifies the video file’s path. |
| type | Defines the format of the video (e.g., video/mp4, video/webm). |
Below is an example of using different attributes of video tag:
Three different formats are commonly supported by web browsers - mp4, Ogg, and WebM. The table below lists the formats supported by different browsers:
Browser | MP4 | WebM | OGG |
|---|---|---|---|
| Google Chrome | Yes | Yes | Yes |
| Internet Explorer | Yes | No | No |
| Firefox | Yes | Yes | Yes |
| Opera | Yes | Yes | Yes |
| Safari | Yes | Yes | No |
Example 1: Responsive Video
Example 2: Styled Video with Controls & Autoplay
To ensure your videos are displayed correctly across all browsers, follow these best practices:
Since not all browsers support the same video formats, it's a good idea to provide multiple video formats (MP4, WebM, Ogg) in the <video> element. This will ensure that the video plays regardless of the browser the user is using.
<video controls>
<source src="example.mp4" type="video/mp4">
<source src="example.ogg" type="video/ogg">
<source src="example.webm" type="video/webm">
Your browser does not support the video tag.
</video>
To enhance user experience, make sure your videos are optimized for the web. Compress your video files without sacrificing too much quality. Use appropriate file sizes for faster loading times.
Add captions, subtitles, or other accessibility features to make your videos more inclusive. You can use the <track> element to provide captions in various languages.
<video controls>
<source src="example.mp4" type="video/mp4">
<track src="subtitles_en.vtt" kind="subtitles" srclang="en" label="English">
Your browser does not support the video tag.
</video>