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.
Scroll down to the End, there is a Tutorial Video which is a live example of the Video Element displaying on this webpage.
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:
- It allows you to specify multiple
<source>
elements for different formats of the video (e.g., MP4, WebM, Ogg), improving cross-browser compatibility. - By including the
type
attribute, you can tell the browser the exact MIME type of the video file, helping it determine whether it can play the file.
Key Attributes of the <video>
Element
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:
HTML
<html>
<body>
<video controls="" height="240" width="320">
<source src="https://media.geeksforgeeks.org/wp-content/uploads/20190616234019/Canvas.move_.mp4" type="video/mp4"/>
Sample Video
</video>
</body>
</html>
- The <video> tag defines the video player, with width and height attributes setting its dimensions.
- The controls attribute adds playback controls like play, pause, and volume.
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 |
More Examples of HTML video
Example 1:
HTML
<html>
<head>
<style>
video {
max-width: 100%;
height: auto;
display: block;
margin: 0 auto;
}
</style>
</head>
<body>
<video controls="" poster="https://via.placeholder.com/640x360.png?text=Video+Loading">
<source src="https://media.geeksforgeeks.org/wp-content/uploads/20190616234019/Canvas.move_.mp4" type="video/mp4"/>
</video>
</body>
</html>
- The video is made responsive with CSS, adjusting its width to 100% of its container while maintaining the aspect ratio.
- The poster attribute displays a placeholder image before the video loads or plays, enhancing the user experience.
Example 2:
HTML
<html>
<head>
<style>
video {
width: 640px;
height: 360px;
border: 2px solid #4CAF50;
border-radius: 8px;
background-color: #000;
}
</style>
</head>
<body>
<video autoplay="" controls="" loop="" muted="">
<source src="https://media.geeksforgeeks.org/wp-content/uploads/20241202174008478068/sample-vedio.mp4" type="video/mp4"/>
</video>
</body>
</html>
- The video is styled with a green border, rounded corners, and a black background to enhance its appearance.
- The autoplay, loop, and muted attributes ensure the video plays automatically, repeats indefinitely, and starts without sound, respectively.
Best Practices for Video Implementation
To ensure your videos are displayed correctly across all browsers, follow these best practices:
1. Use Multiple Video Sources
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>
2. Optimize Video Size and Quality
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.
3. Implement Accessibility Features
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>
Similar Reads
HTML Tutorial HTML stands for HyperText Markup Language. It is the standard language used to create and structure content on the web. It tells the web browser how to display text, links, images, and other forms of multimedia on a webpage. HTML sets up the basic structure of a website, and then CSS and JavaScript
11 min read
HTML Introduction HTML stands for Hyper Text Markup Language, which is the core language used to structure content on the web. It organizes text, images, links, and media using tags and elements that browsers can interpret. As of 2025, over 95% of websites rely on HTML alongside CSS and JavaScript, making it a fundam
6 min read
HTML Editors An HTML Editor is a software application designed to help users create and modify HTML code. It often includes features like syntax highlighting, tag completion, and error detection, which facilitate the coding process. There are two main types of HTML editors: Text-Based Editors - Allow direct codi
5 min read
HTML Basics HTML (HyperText Markup Language) is the standard markup language used to create and structure web pages. It defines the layout of a webpage using elements and tags, allowing for the display of text, images, links, and multimedia content. As the foundation of nearly all websites, HTML is used in over
6 min read
HTML Comments HTML comments are used to add notes or explanations in the HTML code that are not displayed by the browser.They are useful for documenting the code, making it easier to understand and maintain.To add a comment, use the syntax <!-- your comment here -->. HTML<!-- This is a comment and will n
4 min read
HTML Elements An HTML Element consists of a start tag, content, and an end tag, which together define the element's structure and functionality. Elements are the basic building blocks of a webpage and can represent different types of content, such as text, links, images, or headings.For example, the <p> ele
5 min read
HTML Attributes HTML Attributes are special words used within the opening tag of an HTML element. They provide additional information about HTML elements. HTML attributes are used to configure and adjust the element's behavior, appearance, or functionality in a variety of ways. Each attribute has a name and a value
8 min read
HTML Headings HTML headings are used to define the titles and subtitles of sections on a webpage. They help organize the content and create a structure that is easy to navigate.Proper use of headings enhances readability by organizing content into clear sections.Search engines utilize headings to understand page
4 min read
HTML Paragraphs A paragraph in HTML is simply a block of text enclosed within the <p> tag. The <p> tag helps divide content into manageable, readable sections. Itâs the go-to element for wrapping text in a web page that is meant to be displayed as a distinct paragraph.Syntax:<p> Some Content...
5 min read
HTML Text Formatting HTML text formatting refers to the use of specific HTML tags to modify the appearance and structure of text on a webpage. It allows you to style text in different ways, such as making it bold, italic, underlined, highlighted, or struck-through. Table of ContentCategories of HTML Text FormattingLogic
4 min read