HTML Tags – Video Tag


What is <video> tag ?

<video> tag is an inline element used to embed video content into a web page.

<video> tag is used to add any videos, for example music video or news video, to our web pages.

What are the supported formats for <video> tag ?

Only three certain formats are supported by HTML5 <video> tag, listed below

1. mp4

2. ogg

3. webM

Note : All three formats are not supported in all browsers.

Simple <video> tag

Example :

<!DOCTYPE html>
<html>
        <head>
                <title>Video Tag</title>
        </head>
        <body>
                <p>MP4 Format</p>
                <video width="300" height="200" autoplay>
                        <source src="example1.mp4" type="video/mp4">
                </video>
                <p>Ogg Format</p>
                <video width="300" height="200" autoplay>
                        <source src="example2.ogv" type="video/ogg">
                </video>
                <p>WebM Format</p>
                <video width="300" height="200" autoplay>
                        <source src="example3.webm" type="video/webm">
                </video>
        </body>
</html>

<video> – Defines the start and end of <video> and </video> tags.

width, height  – Defines the width and height of the video section.

If not set the webpage will flicker while loading the video file.

source – Defines the source location of the video file.


type – Defines the supported format type of the video file.

Output :

HTML beginners tutorial !!!

Click to Learn More about – HTML Tutorial for beginners

What are the attributes for <video> tag ?

Some of the attributes can be used with <video> tag listed below,

1. height and width

2. src

3. autoplay

4. controls

5. loop

6. muted

7. poster

8. preload

1. height and width

Defines the width and height of the video player.

Example :

<!DOCTYPE html>
<html>
        <head>
                <title>Video Tag</title>
        </head>
        <body>
                <p>MP4 Format</p>
                <video width="300" height="200">
                        <source src="example1.mp4" type="video/mp4">
                </video>
        </body>
</html>

Output :

2. src

Defines the source URL of the video.

Example :

<!DOCTYPE html>
<html>
        <head>
                <title>Video Tag</title>
        </head>
        <body>
                <p>MP4 Format</p>
                <video width="300" height="200">
                        <source src="example1.mp4" type="video/mp4">
                </video>
        </body>
</html>

Output :

3. autoplay

Defines the video will play immediately after it loads.

Example :

<!DOCTYPE html>
<html>
        <head>
                <title>Video Tag</title>
        </head>
        <body>
                <p>MP4 Format</p>
                <video width="300" height="200" autoplay>
                        <source src="example1.mp4" type="video/mp4">
                </video>
        </body>
</html>

Output :

4. controls

Defines the controls to display with the video player.

This is a Boolean value( New attribute in HTML5).

The video control should include:

1. Play

2. Pause

3. Volume

4. Full-screen Mode

5. Seeking

6. Captions/Subtitles(if available)

7. Track(if available)

Example :

<!DOCTYPE html>
<html>
        <head>
                <title>Video Tag</title>
        </head>
        <body>
                <p>MP4 Format</p>
                <video controls width="300" height="200">
                        <source src="example1.mp4" type="video/mp4">
                </video>
        </body>
</html>

Output :


5. loop

Defines the video should continuously repeat.

Example :

<!DOCTYPE html>
<html>
        <head>
                <title>Video Tag</title>
                <script>
                        var id = document.getElementById("egVideo");
                        function enableLoop() {
                                id.loop = true;
                                id.load();
                        }
                </script>
        </head>
        <body>
                <p>MP4 Format</p>
                <video controls id="egVideo" width="300" height="200">
                        <source src="example1.mp4" type="video/mp4">
                </video><br/>
                <button onclick="enableLoop()" type="button"><b>Enable loop</b></button>
        </body>
</html>

Output :


6. muted

Defines the audio output should be muted.

Example :

<!DOCTYPE html>
<html>
        <head>
                <title>Video Tag</title>
                <script>
                        var id = document.getElementById("egVideo");
                        function enableLoop() {
                                id.muted = true;
                        }
                </script>
        </head>
        <body>
                <p>MP4 Format</p>
                <video controls id="egVideo" width="300" height="200">
                        <source src="example1.mp4" type="video/mp4">
                </video><br/>
                <button onclick="enableLoop()" type="button"><b>Enable Muted</b></button>
        </body>
</html>

Output :

7. poster

Defines an image to be shown while the video is downloading, or first loads.


Example :

<!DOCTYPE html>
<html>
        <head>
                <title>Video Tag</title>
                <script>
                        var id = document.getElementById("egVideo");
                        function enableLoop() {
                                id.muted = true;
                        }
                </script>
        </head>
        <body>
                <p>MP4 Format</p>
                <video controls id="egVideo" width="300" height="200" poster="https://secureservercdn.net/192.169.221.188/s30.e0b.myftpupload.com/wp-content/uploads/2020/10/html5-title-cc-1.png">
                        <source src="example1.mp4" type="video/mp4">
                </video><br/>
        </body>
</html>

Output :

8. preload

Defines how and when the video should load.

Example :

<!DOCTYPE html>
<html>
        <head>
                <title>Video Tag</title>
                <script>
                        var id = document.getElementById("egVideo");
                        function enableLoop() {
                                id.muted = true;
                        }
                </script>
        </head>
        <body>
                <p>MP4 Format</p>
                <video controls id="egVideo" width="300" height="200" preload="auto">
                        <source src="example1.mp4" type="video/mp4">
                </video><br/>
        </body>
</html>

Output :

<video> Tag Attributes

HTML <video> tag support following specific attributes.

SnoAttributesValueDescription
1autoplayautoplayAttached video will play automatically when it loads.
2controlscontrolsSpecifies that controls should display with the audio player such as play, stop, pause button.
3heightpixelsSpecifies the height, to display the video.
4looploopSpecifies that video will automatically start over again, when reaching the end of the video.
5mutedmutedSpecifies that the audio will be initially silenced, when the video is played.
6posterURLSpecifies the image to be shown while the video is downloading or until user start to play video.
7preloadnone
auto
metadata
Specifies how the author thinks the video file should be loaded as per best user experience.
8srcURLSpecifies the URL of video file.
9widthpixelsSpecifies the width, to display the video.

Global Attributes

HTML <video> tag support following global attributes.


SnoAttributesValueDescription
1idunique_nameDeclared unique id for an element.
2classclass_nameDeclared one or more classnames for an element.
3stylestylesCSS inline styles specify an element.
4titletitleSpecify extra details of element contain, this will display as a “tooltip” for an elements.

Event Attributes

HTML <video> tag support following event attributes.

SnoAttributesValueDescription
1onfocusscriptelement gets focus on object when script tobe run.
2onblurscriptelement lose the focus on object when scrip tobe run.
3onabortscriptelement gets aborted on object when script tobe run.
4onchangescriptelement gets anytime change on object when script tobe run.
5onbeforeunloadscriptelement gets unloaded on object when scrip tobe run.
6onclickscriptclicked on object when script tobe run.
7ondblclickscriptdouble click on object when script tobe run.
8onkeydownscriptkey is pressed when script tobe run.
9onkeypressscriptkey is pressed over element then released when script tobe run.
10onkeyupscriptkey is released over element when script tobe run.
11onmousedownscriptmouse button was pressed over an element when script tobe run.
12onmouseoutscriptmouse pointer release over an element when script tobe run.
13onmousemovescriptrun mouse pointer moved when script tobe run.
14onmouseoverscriptrun mouse pointer move over when script tobe run.
15onmouseupscriptmouse button is released when script tobe run.
16onresetscriptform has been reset when script tobe run.
17onselectscriptSelect some content when script tobe run.
18onsubmitscriptform has been submitted when script tobe run.
19onloadscriptobject has load when script tobe run.
20onchangescriptallow to change the object when script tobe run.
21onunloadscriptunload to the browser window when script tobe run.
22ondragscriptelement being dragged when script tobe run.
23ondragendscriptelement being stop dragged when script tobe run.
24ondragenterscriptelement being go target dragged when script tobe run.
25ondragleavescriptelement being leave to target dragged when script tobe run.
26ondragoverscriptelement being over to target dragged when script tobe run.
27ondragstartscriptelement being start dragged when script tobe run.
28ondropscriptelement being dropped when script tobe run.
29onerrorscriptelement error occurs when script tobe run.
30onmessagescriptelement message display when script tobe run.
31onerrorscriptelement error occurs when script tobe run.
32onmousewheelscriptmouse wheel will be rotate when script tobe run.
33onscrollscriptscrollbar is scroll when script tobe run.
34onresizescriptelement should be resize when script tobe run.
35onselectscriptall element content selected when script tobe run.
36onstoragescriptelement should be store in target when script tobe run.

Browser Compatibility

SnoBrowserSupport
1ChromeYes
2FirefoxYes
3EdgeYes
4OperaYes
5SafariYes
6Internet ExplorerYes