Sub topic 10
Making media accessible
This chapter assumes you can write basic HTML, know how the video element and source element work, and are comfortable with JavaScript event listeners. It covers what a page must provide so that people who cannot see, hear, or use a mouse can use your media.
Who needs what
Different users need different alternatives.
| Need | Alternative |
|---|---|
| Cannot hear audio | Captions, transcript |
| Cannot see video | Audio description, transcript |
| Cannot use a mouse | Keyboard-operable controls |
| Cannot process fast speech | Pause, seek, and speed controls |
Captions include speech and meaningful sounds, such as "[door slams]". Subtitles usually translate only the speech. Use kind="captions" for the first and kind="subtitles" for the second.
Add captions with track
A track element points to a WebVTT file. Set srclang and label so the browser can list the track in its menu.
<video controls width="640" preload="metadata">
<source src="clip.mp4" type="video/mp4">
<track kind="captions" src="clip.en.vtt" srclang="en" label="English" default>
<p>Your browser cannot play this video. <a href="clip.mp4">Download it instead</a>.</p>
</video>The default attribute turns the track on when the page loads. A WebVTT file is plain text:
WEBVTT
00:00:00.000 --> 00:00:03.500
Welcome to the tutorial.
00:00:03.500 --> 00:00:07.000
First, we will add a video element.Transcripts and audio description
Captions do not help someone who cannot see the picture. Provide a text transcript near the player, and link to it from the page. For audio-only content such as a podcast, a transcript is the main alternative. For video where important information appears only on screen, add audio description. You can do this with a second version of the video that has a narrated track, linked beside the player.
Keep custom controls keyboard operable
The built-in controls are keyboard operable and labeled by the browser. If you replace them, you take on that work. Use a real button element, which receives focus and responds to Enter and Space without extra code.
<video id="player" src="clip.mp4"></video>
<button id="toggle" type="button" aria-pressed="false">Play</button>
<script>
const video = document.querySelector('#player');
const button = document.querySelector('#toggle');
button.addEventListener('click', async () => {
try {
if (video.paused) {
await video.play();
} else {
video.pause();
}
} catch (err) {
console.error('Playback failed:', err.name);
}
});
video.addEventListener('play', () => {
button.textContent = 'Pause';
button.setAttribute('aria-pressed', 'true');
});
video.addEventListener('pause', () => {
button.textContent = 'Play';
button.setAttribute('aria-pressed', 'false');
});
</script>The script updates the button from the play and pause events, not from the click. That keeps the label correct when something else changes playback, such as a keyboard media key.
Other practices
- Avoid
autoplaywith sound. Audio that starts on its own talks over a screen reader. - Give a muted autoplaying decorative video no information that is not available elsewhere. Offer a way to pause it.
- Keep captions in sync and complete, including speaker changes and meaningful sounds.
- Check that every control has a visible focus style and an accessible name.
Exercise
A teammate builds a custom play control from a span with a click listener. Name two problems for users and the fix.
Show answer
The span cannot receive keyboard focus, and it exposes no button role or name to screen readers. Replace it with a button element with type="button", and update its label from the play and pause events.