Open In App

How To Autoplay Youtube Video in HTML

Last Updated : 04 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

To embed and play YouTube videos in HTML, you can use the <iframe> tag with the YouTube embed URL.

Furthermore, to autoplay a YouTube video in HTML, you need to add some specific parameters in the URL to control autoplay and muting.

  • Autoplay: (autoplay=1) in the URL automatically starts playing the video.
  • Mute: (mute=1 ) is essential since most browsers block autoplay with sound.

Here’s the code to Autoplay Youtube Video in HTML.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Autoplay YouTube Video</title>
</head>
<body>

    <h1>Autoplay YouTube Video Example</h1>
    
    <iframe width="560" height="315" 
        src="https://www.youtube.com/embed/YOUR_VIDEO_ID?autoplay=1&mute=1">
    </iframe>

</body>
</html>

Explanation :

  • <iframe> is an HTML element that lets you embed another webpage, like a YouTube video, within your page.
  • Width and Height: The width and height attribute of iframe provide a way to set the video dimensions (ex: width="560" and height="315") which can be adjusted as needed.

URL Parameters:

Additional URL parameters for autoplay and mute.

  • Autoplay: (autoplay=1) is a URL parameter that tells the video to start playing automatically when the page loads. By adding (autoplay=1) to the URL of the YouTube embed link, you can enable the autoplay feature for that video.
  • Mute: (mute=1) silences the video, which is often necessary since many browsers block autoplay if the sound is on. It is a URL parameter that tells the video to start playing without sound. Adding (mute=1) to the URL mutes the audio when the video begins playing.

Note: The '?' symbol in the URL indicates the beginning of the query string, which is where parameters are added to pass information to the server or control aspects of embedded content and the '&' symbol is used to separate two different parameters.


Article Tags :

Similar Reads