The Wayback Machine - https://web.archive.org/web/20241217204255/https://www.geeksforgeeks.org/html5-video/
Open In App

HTML Video

Last Updated : 27 Aug, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The <video> element in HTML allows you to embed video content directly into web pages. It supports various video formats, including MP4, WebM, and Ogg. In this guide, we’ll learn about the key features of HTML5 video. video and audio tags are introduced in HTML5.

Basic Syntax

To include a video on your webpage, use the following syntax:

<video src="" controls>   </video>
  • The src attribute specifies the URL of the video file.
  • The controls attribute adds default video controls (play, pause, volume, etc.).

Supported Formats

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 ChromeYesYesYes
Internet ExplorerYesNoNo
FirefoxYesYesYes
OperaYesYesYes
SafariYesYesNo

Additional Attributes

Attributes

Description

Autoplay

Starts playing the video automatically.

Preload

Provides a hint to the browser about the best user experience.

Loop

Automatically loops the video.

height

It sets the height of the video in CSS pixels.

width

It determines the width of the video display area on the web page.

Controls

It shows the default video controls like play, pause, volume, etc.

Muted

Mutes the audio.

Poster

Displays an image preview before video loading.

Example Usage of HTML Video

1. Adding Video with Preload

Example: We have embedded the HTML video along with the source link and preload functionality.

HTML
<!DOCTYPE html>
<html>

<body>
  <center>
    <h1 style="color:green;">GeeksforGeeks</h1>
    <h3>HTML video tag</h3>
    <p>Adding video on the webpage
    <p>
      <video width="450" height="250" 
      controls preload="auto">
        <source src="https://media.geeksforgeeks.org/wp-content
/uploads/20190616234019/Canvas.move_.mp4" 
                type="video/mp4">
        <source src="https://media.geeksforgeeks.org/wp-content
/uploads/20190616234019/Canvas.move_.ogg" 
                type="video/ogg">
      </video>
  </center>
</body>

</html>

Output:

2. Adding Video using HTML5

Example: This simple example illustrates the use of the <video> tag in HTML. Here, the controls attribute is used to add controls like play, pause, volume, etc, & the “source” element is used to specify the video that the browser will choose to play.

HTML
<!DOCTYPE html>
<html>

<body>
    <p>
      Adding Video on my webpage 
    </p>

    <video width="400" height="350" 
           controls>
        <source src="myvid.mp4" 
                type="video/mp4">
        <source src="myvid.ogg" 
                type="video/ogg">
    </video>
</body>
</html>

Output:

Video addition to the HTML.

3. Autoplaying a Video using HTML5

Example: This example illustrates the use of the autoplay attribute in the HTML <video> tag.

HTML
<!DOCTYPE html>
<html>

<body>
    <p>Adding Video on my webpage</p>

    <video width="400" 
           height="350" autoplay>
        <source src="myvid.mp4" 
        type="video/mp4">
        <source src="myvid.ogg" 
        type="video/ogg">
    </video>
</body>
</html>

Output:

Autoplay attribute

Please refer to the How to display video controls in HTML5? article for knowing the various available controls in detail.

4. HTML Video using JavaScript 

Many properties and events can be set for a video like load, play and pause videos, as well as setting duration and volume.

Example: In this example, we have used Javascript in order to play, pause & set the volume & duration of the video in HTML.

HTML
<!DOCTYPE html>
<html>

<body>
  <div style="text-align:center">
    <button onclick="Pauseplay()">
      Pause/Play
    </button>
    <button onclick="Big()">
      Big
    </button>
    <button onclick="Small()">
      Small
    </button>
    <button onclick="Normal()">
      Normal
    </button>
    <br>
    <video id="myvideo" width="450">
      <source src="myvid.MP4" 
              type="video/mp4">
      <source src="myvid.ogg" 
              type="video/ogg">
    </video>
  </div>
  <script>
    var testvideo = 
    document.getElementById("myvideo");

    function Pauseplay() {
      if (testvideo.paused) testvideo.play();
      else testvideo.pause();
    }

    function Big() {
      testvideo.width = 600;
    }

    function Small() {
      testvideo.width = 300;
    }

    function Normal() {
      testvideo.width = 450;
    }
  </script>
</body>

</html>

Output:

Conclusion

HTML5 video is a powerful tool for enhancing user experience on websites. By understanding its features and using it effectively, you can create engaging multimedia content for your audience.

Supported browsers: 

  • Google Chrome 3
  • Edge 12
  • Firefox 3.5
  • Opera 10
  • Safari 3.1

HTML Video – FAQs

How do you embed a video in HTML?

Use the <video> element with the src attribute or nested <source> elements to specify video files and their formats.

What attributes are commonly used with the <video> element?

Common attributes include controls (for playback controls), autoplay (starts playback automatically), loop (repeats video), and muted (silences audio).

How do you specify multiple video formats?

Use multiple <source> elements within the <video> tag to provide different formats for compatibility, such as MP4, WebM, and Ogg.

Can you provide fallback content for browsers that don’t support HTML5 video?

Yes, include fallback content between the opening and closing <video> tags, like a message or link to download the video.

How do you ensure a video is responsive?

Use CSS to make the video responsive, setting the width to 100% and height to auto, so it adjusts to different screen sizes.



Next Article

Similar Reads

three90RightbarBannerImg