The Wayback Machine - https://web.archive.org/web/20250306221329/https://www.geeksforgeeks.org/html-autoplay-attribute/
Open In App

HTML autoplay Attribute

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

The HTML autoplay Attribute, a boolean attribute, enables audio or video elements to start playing automatically when the page loads, providing seamless playback without interruption.

Syntax: 

<element autoplay> 

Supported Elements

It can be used with <audio> and <video> elements. 

ElementDescription
<audio>Embeds audio content in a webpage.
<video>Embeds video content in a webpage.

HTML Video autoplay Code Example

In this example we demonstrates the use of the autoplay attribute for the <video> element, enabling automatic playback of the video when the page loads.

html
<!DOCTYPE html>
<html>

<head>
    <title>
        HTML video autoplay Attribute
    </title>
</head>

<body>
    <center>
        <h3>HTML video autoplay Attribute</h3>
        <video width="400" height="200" controls autoplay>
            <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: 

VideoautoPlay

HTML video autoplay Attribute example output


HTML Audio autoplay Code Example

In this example we demonstrates the use of the autoplay attribute for the <audio> element, enabling automatic playback of the audio when the page loads.

html
<!DOCTYPE html>
<html>

<head>
    <title>
        HTML audio autoplay Attribute
    </title>
</head>

<body>
    <h2>HTML audio autoplay Attribute</h2>
    <audio controls autoplay>
        <source src=
"https://media.geeksforgeeks.org/wp-content/uploads/20190531165842/Recording1514.ogg"
            type="audio/ogg" />
        <source src=
"https://media.geeksforgeeks.org/wp-content/uploads/20190531165842/Recording1514.mp3"
            type=" audio/mpeg " />
    </audio>
</body>

</html>

Output: 

audioautoPlay

Supported Browsers: The browser supported by HTML autoplay attributes are listed below: 

HTML autoplay Attribute – FAQs

Which HTML elements support the autoplay attribute?

The autoplay attribute is primarily used with the <audio> and <video> tags.

Does the autoplay attribute work on mobile devices?

On many mobile devices, autoplay is often disabled by default to save data and battery. Some browsers also block autoplay unless the media is muted.

Can multiple videos autoplay at the same time on a webpage?

Yes, multiple videos can autoplay simultaneously, but this can overwhelm users and significantly impact performance. It’s best to autoplay only one video at a time.

What happens if the media file is large and takes time to load?

If the media file is large, the autoplay will start as soon as enough data is loaded to begin playback. However, it may still result in a delay if the file is very large.

How does autoplay interact with lazy-loading media?

When using lazy-loading techniques (like loading=”lazy”), the autoplay attribute only triggers once the media enters the viewport and begins loading. This ensures resources are not wasted on content that isn’t immediately visible.


Next Article

Similar Reads

three90RightbarBannerImg