Sub topic 6
Controlling playback with attributes
This chapter assumes you can write basic HTML attributes, and that you know how to embed a <video> or <audio> element with a src or <source> children. It covers the six attributes that set a media element's default playback behavior: controls, loop, muted, autoplay, preload, and playsinline. Scripting these behaviors comes in a later chapter.
How boolean attributes work
controls, loop, muted, autoplay, and playsinline are boolean attributes. If the attribute is present, the feature is on. The value does not matter.
<video src="clip.mp4" controls></video>
<video src="clip.mp4" controls="controls"></video>
<video src="clip.mp4" controls="false"></video>All three elements show controls. The third one is a common mistake, because "false" is only a string and the attribute is still present. To turn a feature off, remove the attribute.
preload is different. It is an enumerated attribute that takes one of three keywords.
The attributes at a glance
| Attribute | Applies to | Effect |
|---|---|---|
| `controls` | audio, video | Shows the browser's built-in player UI |
| `loop` | audio, video | Restarts playback from the start when the media ends |
| `muted` | audio, video | Starts with the sound off |
| `autoplay` | audio, video | Asks the browser to start playing as soon as it can |
| `preload` | audio, video | Hints how much to download before the user presses play |
| `playsinline` | video | Plays inside the page instead of forcing fullscreen on some mobile browsers |
controls
Without controls or your own script, a media element has no visible interface. A video shows only its first frame, and an <audio> element renders nothing at all. The user cannot start it.
With controls, the browser draws its own player. It usually has play and pause, a seek bar, volume, and often fullscreen and captions menus. The look differs between browsers, and you cannot style it with ordinary CSS. If you need a custom look, you build your own controls in a later chapter. Start with the built-in ones, because they are keyboard operable and support captions without extra work.
loop
loop makes the element start again at time zero when it reaches the end. The ended event does not fire while loop is active. Use it for short ambient clips and sound effects. Avoid it for long content, where an endless repeat annoys users.
muted
muted starts playback with the sound off. The user can still unmute with the controls. It matters mainly because browsers treat muted media as low-impact, so they allow it to autoplay.
The attribute only sets the initial state. In JavaScript, the defaultMuted property reflects the attribute. The muted property reflects the current state and changes when the user toggles the volume.
const video = document.querySelector('video');
console.log(video.defaultMuted); // true if the muted attribute is in the markup
console.log(video.muted); // current state, which the user may have changed
video.muted = false; // unmutes now, but does not edit the attributeautoplay
autoplay asks the browser to start playback as soon as enough data has loaded. Treat it as a request, not a command. Browsers block autoplay with sound in many situations, and the rules differ between browsers and between users' settings. A later chapter covers those policies and how to handle the blocked case in code. For now, remember one pattern that browsers usually allow, which is autoplay combined with muted.
<video autoplay muted loop playsinline src="background.mp4"></video>This is the standard markup for a decorative background clip. It has no controls because the clip carries no information the user must operate.
preload
preload tells the browser how much data to fetch before the user interacts. It is a hint, and the browser may ignore it, for example on a metered connection.
| Value | Meaning | Good for |
|---|---|---|
| `none` | Fetch nothing until playback starts | Pages with many media elements, or large files few visitors will play |
| `metadata` | Fetch only duration, dimensions, and the first frame if possible | The usual default choice for a single video |
| `auto` | The browser may download the whole file | A main video most visitors will watch |
If you omit preload, the browser picks its own default, which commonly is metadata but is not guaranteed. State your intent explicitly when it matters.
Two interactions are worth knowing. First, autoplay overrides the idea of waiting, because the browser must load data to play. Second, with preload="none" the element may not know its duration or size, so a layout that depends on the video's intrinsic size can shift once playback begins. Setting width, height, or a CSS aspect-ratio avoids that, and a poster image gives the box something to show.
playsinline
On iPhone, Safari historically played any video in its fullscreen player. playsinline tells the browser to play the video inside the page layout. Add it for any video that is part of the page design, and always add it to background videos. Without it, an autoplaying clip on some mobile browsers may fail to start, or it may take over the screen. On desktop browsers the attribute has no visible effect, so including it is harmless.
Choosing attributes for a use case
A worked example
This podcast page has several episodes. Visitors play one or two, so nothing should download until they choose.
<h2>Episode 12</h2>
<audio controls preload="none" src="episode-12.mp3">
<a href="episode-12.mp3">Download episode 12</a>
</audio>A product page has a short demo that most visitors watch. It is part of the layout and must stay on the page on phones.
<video controls playsinline preload="auto" poster="demo.jpg" width="640" height="360">
<source src="demo.webm" type="video/webm">
<source src="demo.mp4" type="video/mp4">
</video>Common errors
- Writing
controls="false"orautoplay="false"and expecting the feature to turn off. Remove the attribute instead. - Using
autoplaywithoutmutedand assuming it works everywhere. - Using
loopon content that carries information, which gives the user no clear ending. - Setting
mutedin the markup, then expectingvideo.mutedto stay true after the user unmutes. The property tracks live state. - Leaving
preloadunset on a page with many videos, so the browser may fetch metadata for all of them. - Forgetting
playsinlineon background video, which fails on some iPhones.
Exercise
A designer wants a silent, looping background video on a landing page. It should start by itself on desktop and iPhone and show no player UI. Which attributes do you add, and why is controls left out?
Show answer
Add autoplay, muted, loop, and playsinline. muted makes autoplay likely to be allowed, loop repeats the clip, playsinline keeps it in the page on iPhone, and autoplay starts it. controls is left out because a decorative clip needs no user interface.
Exercise
Why does <video controls="false" src="a.mp4"></video> still show controls, and how do you fix it?
Show answer
controls is a boolean attribute, so its presence turns the feature on regardless of the value. Remove the attribute entirely.