Whether it’s a demo of any product, a tutorial, or a background clip, video helps to deliver content in a more interactive way. HTML has the <video> tag, which makes it simple to add videos to your websites. In this blog, you will learn all about the <video> tag in HTML and best practices to follow while using the video tag in your code.
Table of Contents:
The <video> Tag in HTML
The <video> tag was introduced in HTML5 and is used to add videos directly into the webpage. If you are using this <video> tag for adding video inside the webpage, then you don’t need to install any additional plugins. It can give you built-in playback control, also like play, pause, and volume adjustment. Here is the syntax of writing a video tag in HTML:
Syntax:
<video src=" " controls></video>
The video tag in HTML tells the browser to display a video player that plays the video available at the link given in the src attribute.
What is the <source> Tag?
All browsers don’t support the same video format. To make sure that the video works across different browsers, you can use multiple <source> elements inside your <video> tag.
Syntax:
<video controls>
<source src="video.mp4" type="video/mp4">
<source src="video.webm" type="video/webm">
<source src="video.ogg" type="video/ogg"
Your browser does not support the video tag.
</video>
Explanation:
- <video>: This is the main tag. When the browser encounters the video tag then it creates a video player.
- controls: This is responsible for adding default video controls like play, pause, volume, and many more.
- <source>: This allows you to add multiple video file formats.
- Fallback Text: This message will be shown only if the user’s browser doesn’t support the <video> tag.
Launch Your Dream Career with Web Development Skills
Learn Web Dev Today
Making a Responsive Video Element
Making a responsive video element helps you to improve user experience, when users see the video, it looks good on all screens (Mobile, Laptop, Tablet).
Example:
Explanation: In this example, a responsive video element is created using the <video> tag in HTML. The controls attribute enables built-in playback controls like play, pause, and volume. The poster attribute is used to display the poster.jpg image before the video starts playing. The loading=”lazy” attribute is used to loading of the video until the video element does not lie in the current screen/viewport of the user.
Tip:
- Now, modern browsers support lots of video formats like MP4, WebM, and many more, but MP4 is the most recommended for developers. Always try to upload videos in MP4 format.
- It is good to add captions and subtitles in the video by using the <track> tag in HTML.
Syntax:
<track src="subtitles.vtt" kind="subtitles" srclang="en" label="English">
Here, subtitles.vtt is the path to the subtitle file.
Common Attributes
Attributes are used to provide extra information about elements in HTML. It is always placed in the opening tag, never put inside the closing tag. Here is a list of some common attributes that are used with the <video> tag:
Attribute |
Description |
src |
It is used to give the URL of the video file. |
controls |
It is used to display the built-in video controls (play, pause, volume). |
autoplay |
It automatically starts the video when the page loads. |
loop |
It is used to repeat the video again and again. |
muted |
It is used to mute the audio of the video that you are playing. |
poster |
It helps you to show an image before the video starts playing. |
preload |
It tells the browser when and how to load the video. |
width/height |
It is used to set the width and height of the video player. |
Important Note:
Modern browsers that are used now require videos to be muted in order for autoplay to work. So if you want that autoplay to work in your browser, then you have to add the muted attribute also.
Browser Compatibility
The HTML <video> tag and common video formats are supported by all modern browsers. However, some video formats are not supported by some common browsers. Let’s understand this with a table:
Browser |
<video> Tag Support |
Video Formats Support |
Google Chrome |
Full Support |
MP4, WebM, Ogg |
Mozilla Firefox |
Full Support |
MP4, WebM, Ogg |
Safari |
Full Support |
MP4 |
Microsoft Edge |
Full Support |
MP4, WebM, Ogg |
Opera |
Full Support |
MP4, WebM, Ogg |
Internet Explorer 9+ |
Partial Support |
MP4 |
Best Practices
If you are using the video element for placing video inside the webpage, then you need to follow these best practices to avoid errors:
- Some browsers don’t support all types of video formats, then it is good to use multiple formats by using the <source> attribute.
- It is good practice to compress your video to reduce loading time.
- Set a poster image to make the preview more attractive.
- Autoplay only works when the video is muted. Thus, always use autoplay with muted.
- Don’t autoplay videos that have sounds, it can be annoying.
Common Mistakes to Avoid
- Using only one video format on websites.
- Don’t forget to add fallback text (just like alt for <img> tag).
- Don’t use autoplay without the muted.
Get 100% Hike!
Master Most in Demand Skills Now!
Conclusion
The HTML <video> tag is a powerful way to add videos to your website. It will reduce the dependency on third-party libraries. The only thing that you do is use the <video> tag and play with controls. It is easy to use and widely supported, and comes with some built-in features.
HTML <video> tag – FAQs
Q1. What is the video element in HTML?
The <video> element in HTML is used to add videos inside a web page. It provides various built-in features like play, pause, volume control, etc.
Q2. What is the video src in HTML?
The src attribute in a <video> tag is used to define the path of the video file that you want to play on your website.
Q3. Is the video tag semantic?
Semantic tags in HTML mean the tag that clearly tells its meaning or purpose. The <video> tag is a semantic tag because it tells the browsers that this is the video part of the webpage.
Q4. What is the full form of HTML?
HTML stands for Hypertext Markup Language. It is the markup language that is used to create the structure for websites.
Q5. What is CSS?
CSS stands for Cascading Style Sheets. It is used to style the HTML documents and make them responsive.