Horje
How to Add Autoplay Video in HTML?

Web design often has a feature of including a video that starts to play as soon as the web page loads. This effect can be achieved using HTML5 <video> element with the autoplay attribute. In this way, videos start playing without any user interaction hence improving the multimedia experience on websites.

Approach

  • HTML5 <video> element is utilized to insert video in your web page directly it supports different attributes that define how the video should be displayed and played.
  • This will enable videos to begin playing automatically as soon as the webpage opens by adding an autoplay attribute to the <video>.
  • use the <source> element within the <video> tag to specify your video file. The src attribute denotes where exactly your video files are located while the type attribute shows its MIME type.
  • With controls attributes included, various playback functions like play, pause, volume control among others will be available for users.

Syntax:

<video autoplay controls>         
<source src="video.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>

Example 1: The example below shows how to add an autoplay video in HTML. The <video> tag includes the autoplay and controls attributes, enabling the video to start playing automatically with user controls for playback.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
  <title>Autoplay Video With Control Example</title>
</head>
<body>
    <h2 style="color: green">GeeksforGeeks</h2>
    <h3> How to add Autoplay Video With Control </h3>
  
    <video autoplay controls>
          <source src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240517010406/Autoplay-Video-2.mp4"
                  type="video/mp4">
          Your browser does not support the video tag.
    </video>
</body>
</html>

Output:

Example 2: The example below shows how to add an autoplay video in HTML with the muted attribute. The <video> tag includes autoplay and muted attributes, enabling the video to start playing automatically without sound.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
  <title>Autoplay Video WithOut Control Example</title>
</head>
<body>
  <h2 style="color: green">Autoplay Video Example</h2>
  <h3> How to add Autoplay Video WithOut Control </h3>
  
  <video autoplay muted>
    <source src=
"https://media.geeksforgeeks.org/wp-content/uploads/20240517010406/Autoplay-Video-2.mp4" 
            type="video/mp4">
    Your browser does not support the video tag.
  </video>
</body>
</html>

Output:




Reffered: https://www.geeksforgeeks.org


HTML

Related
How to use SVG Images in CSS &amp; HTML? How to use SVG Images in CSS &amp; HTML?
HTML Boilerplate HTML Boilerplate
How to Add a Line Break with the br Tag in HTML? How to Add a Line Break with the br Tag in HTML?
How to Embed Video in Iframe in HTML? How to Embed Video in Iframe in HTML?
How to Add a Non-breaking Space using &amp;nbsp in HTML? How to Add a Non-breaking Space using &amp;nbsp in HTML?

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
17