Captions, Subtitles and Text Tracks

Intermediate · 7 min

How the track element and WebVTT add captions and subtitles to video, when a track is fetched, and what happens with cross-origin files and missing attributes.

Before thisAudio and Video Elements

Miljan Peric · 7 min

Captions, Subtitles and Text Tracks

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Miljan PericReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `<track kind="captions" src="en.vtt" srclang="en" label="English" default>` inside a `<video>` attaches a WebVTT file. Captions include non-speech sound such as music and who is speaking. Subtitles only translate or transcribe speech.
  • A track that is not `default` stays `disabled` and is not downloaded. In a test, the non-default track's file was not requested until its `mode` was set to `hidden`.
  • A WebVTT file is plain text starting with `WEBVTT`, then cues, each with a start and end time and the text. In a test two cues loaded with the expected text and start time.
  • A track file on another origin needs CORS. In a test a cross-origin `.vtt` without a `crossorigin` attribute failed with `readyState` 3, error, and no cues.
  • Captions are for people who cannot hear the audio, and WCAG asks for them on prerecorded video. They also help people watching with the sound off.

Sound is not available to everyone

A video of someone speaking is useless to a viewer who is deaf, hard of hearing, or in a library without headphones. A text version of the audio fixes that. HTML gives you a standard way to attach one: the <track> element, pointing at a WebVTT file.

<video controls src="talk.webm">
  <track kind="captions" src="talk.en.vtt" srclang="en" label="English" default>
  <track kind="subtitles" src="talk.fr.vtt" srclang="fr" label="Français">
</video>

The attributes: kind says what the track is, src is the file, srclang is its language, label is the name in the player’s menu, and default turns it on without being asked. The two kinds that matter most are different on purpose. Captions are for people who can’t hear: they include what’s said and also meaningful sound (“[door slams]”, “[tense music]”) and who’s speaking. Subtitles assume the viewer can hear, and transcribe or translate the speech.

The WebVTT file

WebVTT is a plain text format, which is why you can write one in an editor:

WEBVTT

00:00.000 --> 00:01.000
Hello there

00:01.000 --> 00:02.000
Second cue

The first line must be WEBVTT. Each cue then has a time range and the text on the next lines. Optional cue settings can position the text. I used this exact file in a test with headless Chromium 159 and a short recorded WebM clip: the track loaded two cues, the first with text Hello there and start time 0, and a cuechange check at 0.4 seconds found that cue active.

When the file is fetched

Browsers don’t download every track straight away. Each track has a mode: disabled, hidden or showing. In a test with two tracks, one default and one not:

  • The default track was showing and its file was fetched and parsed (two cues).
  • The other stayed disabled, with no cues and no request for its file.
  • Setting its mode to hidden from script made the file download and expose two cues. A hidden track fires events and holds cues without drawing them, which is how you’d build a custom caption display or a transcript.

So listing five language tracks doesn’t cost five downloads. Viewers pick one from the player, and only then does it load.

The CORS trap

A caption file on another origin, such as a CDN, is fetched under CORS rules. In a test a .vtt served from a different origin, with no crossorigin attribute, ended with the track’s readyState at 3 (error) and no cues, so the captions silently did not appear. The fix is to add crossorigin to the <video> (or <track>) and have the file’s server send Access-Control-Allow-Origin. If your captions work locally and vanish in production, check this first. Network details like these are in the frontend HTTP lesson.

A note on srclang

The spec says srclang is required when kind is subtitles. In one test, a subtitles track with no srclang and with default still loaded and showed two cues in Chromium, so a missing attribute may not fail loudly. Don’t take that as permission: the language tag is what lets players list and choose tracks sensibly, so always write it.

Beyond captions

Other kind values exist: descriptions for text descriptions of the visuals, chapters for navigation points and metadata for data your script reads. Support for the less common ones varies by player, so a transcript on the page remains the most reliable way to give everyone the content. The accessibility lessons later in the roadmap and the frontend accessibility lesson cover the wider picture.

What the video covers

The embed is Miljan Peric’s “HTML5 Tutorial #26: Video Subtitles and Captions Explained” (November 2026, about 7 minutes). The description lists the track element, WebVTT files, and the src, kind, srclang, label and default attributes, multiple languages, and the difference between subtitles and captions, with a practical project of English and Serbian subtitles. It’s a brand-new video from a small channel, so it is a pick on fit rather than reputation. I could not confirm its audio language and I could not read a transcript, so I describe only the listed topics. The fetch-timing, CORS and srclang results are this lesson’s own, from one Chromium build. The deeper link is a short WebVTT walkthrough from another small channel. Google’s 2011 talk on WebVTT exists but is too old to recommend.

Where this fits

This builds on audio and video elements. Next, iframes and embedded content covers embedding another site’s player instead of hosting the file.

Check yourself

  1. 01What is the difference between `kind="captions"` and `kind="subtitles"`?

    Captions are for viewers who cannot hear: they include speech plus important sounds and speaker identification, in the same language. Subtitles assume the viewer can hear and transcribe or translate the speech. Use `captions` for accessibility, and add `subtitles` tracks for other languages.

  2. 02You add two `<track>` elements but only one has `default`. When is the other one downloaded?

    Only when it is turned on. In a test the non-default track stayed `disabled` and its file was never requested until its `mode` was set to `hidden` or `showing`, which a viewer does from the video's captions menu.

  3. 03Your captions load from `https://cdn.example.com/en.vtt` but the page is on another origin. Nothing shows. Why?

    Track files are fetched with CORS rules. A cross-origin file needs the `crossorigin` attribute on the `<video>` or `<track>` and a matching `Access-Control-Allow-Origin` header. In a test the track went to `readyState` 3 (error) and had no cues.

  4. 04What are the essential parts of a WebVTT cue?

    A time range such as `00:01.000 --> 00:02.500` and then the cue text on the following lines, with the file beginning with the line `WEBVTT`. Optional parts are a cue identifier and cue settings for position. The test file used two cues and both loaded.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark