HTML Captions: Text Tracks and WebVTT
Attach a caption track
track is a void child of a media element. Use kind="captions" for speech and meaningful sound information; subtitles typically translates or transcribes dialogue. Place tracks after source children. srclang identifies the language and label names the menu option. These examples use the original files in media-fixtures/; open the supplied preview from a local static server or place the files at the shown paths.
<video controls width="160" height="90" src="media-fixtures/demo.webm"> <track kind="captions" src="media-fixtures/demo.en.vtt" srclang="en" label="English" default></video>A caption option named English is available when the track loads.
Understand timed cues
A WebVTT file is plain text beginning with WEBVTT, followed by a blank line and timed cues. The sample file contains 00:00:00.000 --> 00:00:02.000 followed by [Steady tone]. The arrow separates start and end timestamps. Save as .vtt and serve as text/vtt. The following HTML shows the file text, not the file itself.
<pre>WEBVTT
00:00:00.000 --> 00:00:02.000[Steady tone]</pre>The timestamp and caption text appear exactly as a readable example.
Provide an independent transcript
Captions synchronize sound information with the video; a transcript can be read without playback. Describe meaningful visual information separately when it is not conveyed by audio. default requests a default track, but users and browsers can choose another.
<video controls width="160" height="90" src="media-fixtures/demo.webm"> <track kind="captions" src="media-fixtures/demo.en.vtt" srclang="en" label="English" default></video><p>Transcript: A steady tone plays for two seconds while a blue screen is shown.</p>The descriptive transcript remains readable independently of the player.
Common mistakes and practice
Do not assume an automatically generated caption is accurate. Check timing, meaningful sound effects and speaker identification. Keep same-origin media and track files for this example; cross-origin tracks require suitable CORS configuration.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Captions: Text Tracks and WebVTT — Knowledge Check
Apply the media or embedding concepts from this lesson.
What should captions convey besides speech?
What is the purpose of srclang?
What can users read without playing the video?
Which file format is commonly used by HTML track for timed text?
Which track kind is intended for dialogue and relevant sound information?