# video, audio & Captions

> HTML embeds media natively. Captions are not an optional extra: they are a WCAG requirement, and `<track>` is how you attach them.

Subject: HTML | Lesson 21 of 42 | 6 min read
Interactive lesson: https://codewithanimation.in/html/video-audio-and-captions
Previous: [Image Performance & Inline SVG](https://codewithanimation.in/html/image-performance-and-svg.md) | Next: [iframe, sandbox & Embeds](https://codewithanimation.in/html/iframe-and-embeds.md)

## What they are

`<video>` embeds a video clip. `<audio>` embeds a sound file. Both use the same set of core attributes: `controls` shows the browser's built-in playback UI, `autoplay` starts playback immediately (blocked by browsers unless `muted` is also set), `loop` repeats, `muted` silences audio, `poster` sets a placeholder image, and `preload` hints how much the browser should fetch before the user presses play.

`<source>` provides format alternatives inside `<video>` or `<audio>`, following the same first-match logic as `<picture>`. `<track>` links a WebVTT text-track file to the media element. The `kind` attribute identifies the track's purpose: `captions` for synchronized text including sound effects and speaker identification, `subtitles` for spoken dialogue only, `chapters` for navigation, `descriptions` for audio descriptions of visual content, and `metadata` for machine-readable data.

## Why it matters

Captions are a WCAG 2.1 Level AA requirement for all prerecorded video that has audio. They serve users who are deaf or hard of hearing, users watching in a noisy or silent environment, and non-native speakers following along. Treating captions as optional puts the site in legal risk in jurisdictions with web accessibility laws, and it excludes a significant share of viewers.

The distinction between `captions` and `subtitles` matters. Subtitles carry spoken dialogue. Captions carry dialogue plus sound effects, music cues, and speaker identification: everything a viewer needs to understand the audio track without hearing it. A video about an accessibility lecture needs captions, not subtitles, so that `[applause]` and `[laugh]` are included.

## How it works

Add `controls` to every video and audio element that users should be able to interact with. Without it, there is no play button, no seek bar, and no volume control. Add `width` and `height` to reserve space and prevent layout shift. Use `poster` to show a meaningful image while the video is loading rather than a blank box.

For format compatibility, nest `<source>` elements inside `<video>`. List the modern format first: `<source src="film.webm" type="video/webm">` then `<source src="film.mp4" type="video/mp4">`. The browser picks the first type it can decode; the `type` attribute lets it skip formats it does not support without downloading them.

Attach captions with `<track kind="captions" src="captions.vtt" srclang="en" label="English" default>`. The `src` points to a WebVTT file, a plain-text format with timestamps and cue text. The `default` attribute activates this track when the page loads. A viewer can switch tracks using the browser's native caption controls.

## Common mistake: autoplay without muted, or no controls

Two mistakes appear constantly. First: `<video autoplay>` without `muted` is silently blocked in every major browser to prevent unexpected audio. Add `muted` or the video simply will not play. Second: removing `controls` to use a custom player and then not providing one leaves the video permanently stuck, because users have no way to play, pause, seek, or change volume. Build the custom controls before removing the native ones.

## Tip: Use kind="captions", not kind="subtitles", for accessibility

WCAG calls for captions, not subtitles. Subtitles carry dialogue only. Captions carry dialogue, sound effects, and speaker labels, everything the viewer needs to follow a video without audio. If you add a `<track>` file for accessibility compliance and set `kind="subtitles"`, you have the right file format but the wrong semantic label, and the track will appear to users under the wrong menu entry in the browser's caption UI.

## Try it: Try it

Watch a mock player advance and see caption cues appear on time, the way a linked WebVTT file drives the browser caption track.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/html/video-audio-and-captions

## Check yourself

### Question 1

You add autoplay to a <video> but it never plays automatically. What is missing?

- A. muted. Browsers block autoplay with audio by default.
- B. preload="auto". The video must be preloaded before it can autoplay.
- C. controls. Without controls the video cannot start.

Correct answer: A

Browsers block autoplay unless the video is muted. Add `muted` alongside `autoplay` and the video will start automatically. A muted autoplaying video can be unmuted by the user via the controls.

- Why not B: preload controls buffering hints, not whether autoplay is permitted. The missing piece is muted: browsers block autoplay with audio to prevent unexpected sound.
- Why not C: controls adds the UI but is not related to autoplay. Browsers block autoplay-with-audio regardless of whether controls is present. Add muted to unblock it.

### Question 2

A cooking video includes sound effects like sizzling and a jingle. Which track kind do you use for accessibility?

- A. captions, because it includes sound effects and not just dialogue
- B. subtitles, because the primary content is speech
- C. descriptions, because the food is a visual element

Correct answer: A

`kind="captions"` includes dialogue, sound effects, and music cues. WCAG 2.1 AA requires captions for prerecorded video. Subtitles carry only the spoken dialogue and do not satisfy that requirement alone.

- Why not B: Subtitles carry dialogue only. A deaf viewer would miss the sizzling and jingle without captions, which include sound effects and music cues.
- Why not C: descriptions is for audio descriptions of visual content (what is seen on screen), used by blind viewers. captions is the correct kind for synchronized text serving deaf viewers.

### Question 3

You provide WebM and MP4 sources inside a <video>. Which should come first?

- A. WebM, because it is the modern format and the browser picks the first one it supports
- B. MP4, because it has the widest compatibility
- C. Order does not matter; the browser picks the smallest file

Correct answer: A

The browser tries `<source>` elements in order and picks the first supported type. List the modern format (WebM, AVIF) first so capable browsers use it, and put the universally-supported fallback (MP4, JPEG) last.

- Why not B: If MP4 comes first, every browser picks it and the WebM (smaller, better quality per bit) is never used. List the preferred modern format first and MP4 as the fallback.
- Why not C: The browser picks the first source whose type it can decode, in document order. File size plays no part in the selection.

## Remember this

- `<video controls>` is the minimum viable video: always include `controls` so users can interact.
- `autoplay` requires `muted` to work in modern browsers; without `muted` it is silently ignored.
- `<track kind="captions">` includes dialogue, sound effects, and speaker labels; WCAG 2.1 AA requires it for prerecorded video.
- `<source>` elements inside `<video>` provide format fallback; list the modern format first.
