👀 Live Preview
Muted video set to autoplay on page load:

The autoplay attribute is an essential feature in HTML that allows developers to control the automatic playback of audio and video elements. This attribute can enhance the user experience by providing seamless multimedia content without requiring manual intervention.
Presence = play.
Media elements.
Browser policy.
User pause/volume.
.autoplay = true
Use responsibly.
autoplayThe primary purpose of the autoplay attribute is to specify whether a media element should start playing automatically when the page loads. This can be particularly useful for background music, promotional videos, or any other scenario where immediate playback is desired.
Modern browsers often block autoplay with sound. Add muted (and playsinline on mobile) when silent autoplay is acceptable.
Add the boolean autoplay attribute to a <video> or <audio> element:
<video controls autoplay muted playsinline>
<source src="clip.mp4" type="video/mp4">
</video><video> and <audio> elements.controls so users can pause or adjust volume.The autoplay attribute accepts a boolean value:
autoplay attribute is not present.In HTML markup, boolean attributes are enabled by their presence. To disable autoplay in HTML, remove the attribute—do not rely on autoplay="false", which can still be treated as enabled in some cases.
<!-- HTML: presence enables autoplay -->
<video autoplay muted></video>
<!-- JavaScript property -->
<script>
videoElement.autoplay = true;
videoElement.autoplay = false;
</script>| Scenario | Recommended Markup | Notes |
|---|---|---|
| Silent hero video | autoplay muted playsinline | Works in most browsers |
| Video with sound | No autoplay; use controls | Requires user gesture |
| Background audio | autoplay loop muted | Provide mute toggle |
| Dynamic autoplay | element.autoplay = true | Call load() after change |
| User control | controls | Always recommended |
| Applicable elements | video, audio | HTML media elements |
| Element | Supported? | Notes |
|---|---|---|
<video> | Yes | Most common use |
<audio> | Yes | Background music, podcasts |
<iframe> | No | Use embed-specific APIs |
<img> | No | Not a media element |
Video autoplay example and dynamic JavaScript autoplay.
Muted video set to autoplay on page load:
Let’s look at a simple example of how to use the autoplay attribute in an HTML video element:
<video controls autoplay muted playsinline>
<source src="example.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>In this example, the autoplay attribute is included within the <video> element, indicating that the video should start playing automatically when the page loads. Adding muted helps browsers allow autoplay without a prior user click.
You can dynamically control the autoplay attribute using JavaScript. This is particularly useful when you want to adjust the autoplay behavior based on certain conditions or user interactions. Here’s an example:
<video id="dynamicVideo" controls muted>
<source src="example.mp4" type="video/mp4">
</video>
<script>
// Dynamically set autoplay for a video element
var videoElement = document.getElementById("dynamicVideo");
videoElement.autoplay = true;
videoElement.load();
</script>In this script, the autoplay property is dynamically set to true for a video element with the id dynamicVideo. Calling load() refreshes the media element so the new autoplay setting can take effect.
controls so users can pause, mute, or adjust volume.Mark a video or audio element with the boolean attribute.
The file downloads and the media element becomes ready.
Browser allows muted autoplay more readily than autoplay with sound.
Playback begins without the user pressing play first.
The autoplay attribute is supported in all modern browsers, but autoplay behavior may be restricted—especially for media with sound until the user interacts with the page.
All major browsers recognize autoplay on media elements.
Bottom line: The attribute is widely supported, but test muted autoplay and always provide user controls.
autoplay sparingly on intentional hero or promo mediacontrols so users can pause or adjust volumemuted and playsinline for reliable silent autoplayautoplay="false" in HTML to disable (remove the attribute instead)autoplay attribute responsibly, as automatic playback can be intrusive and affect the user experience negatively.controls attribute) alongside autoplay to allow users to pause or adjust the volume of the media.The autoplay attribute is a powerful tool for controlling the automatic playback of audio and video elements in HTML. By understanding how to use this attribute and considering best practices, you can create engaging multimedia content on your web pages.
Use muted autoplay when immediate silent playback is appropriate, always provide controls, and reach for JavaScript when autoplay depends on user actions or runtime conditions.
autoplayBookmark these before adding auto-playing media to your site.
Presence = play.
BasicsMedia only.
ScopePolicy friendly.
BrowserDynamic JS.
ScriptUser choice.
A11y<video> and <audio> elements.muted for silent autoplay or wait for a user click before calling play().autoplay attribute from the element. In JavaScript, set element.autoplay = false.videoElement.autoplay = true and call load() to refresh the media element.Practice the autoplay attribute with video and dynamic JavaScript examples in the Try It editor.
5 people found this page helpful