HTML Course 6.5: controls, autoplay, loop, muted and poster Attributes
This section covers the five key attributes used on <video> and <audio> elements to enhance media playback: controls, autoplay, loop, muted and poster. We’ll dive deep into each, explore browser behaviors, accessibility considerations and provide code examples.
1. controls Attribute
The controls attribute tells the browser to display native playback controls (play/pause, seek bar, volume, etc.). It is a boolean attribute: if present, controls appear; if omitted, no UI is shown (useful when building custom controls).
- Applies to: <video>, <audio>
- Type: Boolean (no value needed)
- Accessibility: Always provide
aria-labelor associated
Example: Video with native controls
<!-- A video element with browser-provided controls --> <video controls width='640' height='360' aria-label='Sample video with controls'> <source src='video.mp4' type='video/mp4'> Your browser does not support the video tag. </video>
2. autoplay Attribute
The autoplay attribute starts media playback automatically when the media is ready. Modern browsers often require muted alongside autoplay to prevent unwanted noise. Autoplay policies vary per browser:
- Chrome, Safari and Firefox block autoplay with sound unless the user has recently interacted or granted permissions.
- Using
mutedallows silent autoplay in most environments.
Example: Autoplaying muted audio or video
<!-- Autoplay a muted video (allowed by most browsers) --> <video autoplay muted controls width='320'> <source src='intro.mp4' type='video/mp4'> Your browser does not support the video tag. </video> <!-- Autoplay an audio file silently --> <audio autoplay muted> <source src='theme.mp3' type='audio/mpeg'> Your browser does not support the audio element. </audio>
3. loop Attribute
The loop attribute restarts playback automatically every time the media ends. It’s also Boolean. Common use-cases include background videos and audio tracks.
- Applies to: <video>, <audio>
- Looping can impact performance—ensure media is optimized.
Example: A looping background video
<!-- Video loops indefinitely after ending --> <video autoplay muted loop playsinline width='800'> <source src='background.mp4' type='video/mp4'> Your browser does not support the video tag. </video>
4. muted Attribute
The muted attribute silences audio by default. It’s often paired with autoplay to satisfy browser policies. Muted media still renders visuals but emits no sound.
- Applies to: <video>
- Does not affect volume on <audio> (audio elements without sound require muted for autoplay).
Example: Muted video that user can unmute
<video controls autoplay muted width='480'> <source src='clip.mp4' type='video/mp4'> Your browser does not support the video tag. </video>
5. poster Attribute
The poster attribute specifies an image to show before the video starts. It improves user experience by providing context or a thumbnail. If omitted, the first video frame is used (which may be black or blank until decoded).
- Attribute value: URL of the image (JPEG, PNG, GIF)
- Best practices: Use optimized, responsive images and include
width/height.
Example: Video with a poster thumbnail
<video controls poster='thumbnail.jpg' width='640' height='360'> <source src='presentation.mp4' type='video/mp4'> Your browser does not support the video tag. </video>
6. Attribute Summary
| Attribute | Applies To | Type | Description |
|---|---|---|---|
| controls | video, audio | Boolean | Show native playback UI |
| autoplay | video, audio | Boolean | Start playback automatically |
| loop | video, audio | Boolean | Restart when ended |
| muted | video | Boolean | Silence audio output |
| poster | video | URL | Placeholder image before playback |
7. Combined Example
An all-in-one video that autoplays muted, loops forever, shows controls and displays a poster before start:
<video controls autoplay muted loop poster='cover.jpg' width='720' height='405'> <source src='movie.mp4' type='video/mp4'> <source src='movie.webm' type='video/webm'> Your browser does not support the video tag. </video>
In this example:
controls– user can pause/resume.autoplay muted– video starts silently.loop– repeats indefinitely.poster– showscover.jpguntil playback begins.
These attributes combined allow you to craft rich media experiences while respecting browser policies and accessibility guidelines. Always test across devices and screen readers to ensure a robust user experience.
Leave a Reply
You must be logged in to post a comment.