HTML Video Captions
What are Captions?
Captions are lines of text that show on top of a video while it plays.
They help many people:
- people who are deaf or hard of hearing
- people who watch without sound, like on a bus or in a library
- people who do not speak the language of the video well
In HTML, you add captions with the <track> element and a text file in the WebVTT format.
The WebVTT File
WebVTT (Web Video Text Tracks) is a plain text file with the extension .vtt.
It lists each caption, and the time when it should show:
mov_bbb_en.vtt
WEBVTT
00:00:00.000 --> 00:00:03.000
This caption shows from 0 to 3 seconds.
00:00:03.000 --> 00:00:06.000
This caption shows from 3 to 6 seconds.
00:00:06.000 --> 00:00:10.000
And this one shows from 6 to 10 seconds.
- The file must start with the word
WEBVTT - Each caption starts with a start time and an end time, written as hours:minutes:seconds.milliseconds
- The text of the caption comes on the next line
- An empty line separates the captions
Note: Save the file with the UTF-8 character set, so all letters show correctly.
The track Element
Put a <track> element inside the <video> element, after the <source> elements:
Example
<video width="400" controls>
<source src="mov_bbb.mp4" type="video/mp4">
<track src="mov_bbb_en.vtt" kind="captions" srclang="en" label="English" default>
</video>
Try it Yourself »
| Attribute | What it does |
|---|---|
| src | The address of the .vtt file |
| kind | The kind of text, like captions or subtitles |
| srclang | The language of the text, like en or es |
| label | The name the user sees in the captions menu |
| default | Turns this track on when the video starts |
Captions or Subtitles?
The kind attribute says what the text is for:
| Value | Meaning |
|---|---|
| captions | The words that are spoken, and important sounds, like [door closes]. For people who cannot hear the video |
| subtitles | A translation of the words that are spoken. For people who do not understand the language |
| descriptions | A text description of what happens on the screen. For people who cannot see the video |
| chapters | Chapter titles, so the user can jump to a part of the video |
Captions in Several Languages
Add one <track> element for each language.
The user can then choose a language in the video controls:
Example
<video width="400" controls>
<source src="mov_bbb.mp4" type="video/mp4">
<track src="mov_bbb_en.vtt" kind="subtitles" srclang="en" label="English" default>
<track src="mov_bbb_es.vtt" kind="subtitles" srclang="es" label="Spanish">
</video>
Try it Yourself »
Only one track should have the default attribute.
Style the Captions
Use the CSS ::cue pseudo-element to change the look of the captions:
Example
video::cue {
background-color: rgba(0, 0, 0, 0.8);
color: yellow;
font-size: 18px;
}
Try it Yourself »
Tip: Keep a strong contrast between the text and the background, so the captions are easy to read on any part of the video.
Things to Know
- The .vtt file must be on the same website as the page, unless the other website allows it
- The web server must send .vtt files with the type
text/vtt - Users can turn captions on and off in the video controls, so add the
controlsattribute
Summary
- Captions are written in a WebVTT file, with a time for each line of text
- The
<track>element connects the file to the video kind,srclang, andlabeldescribe the track- Add one track for each language
- Style the captions with
::cue
HTML Tag Reference
| Tag | Description |
|---|---|
| <video> | Defines a video or movie |
| <track> | Defines text tracks in media players |
For a complete list of all available HTML tags, visit our HTML Tag Reference.