Skip to content
SubtitleDesk

Adding captions to an HTML5 video with WebVTT

Last updated · 2 min read

The markup, file format and server settings you need to show subtitles on a video in your own web page — and the common reasons the captions do not appear.

Browsers have built-in support for subtitles on the <video> element. You need a WebVTT file and one extra line of HTML. Most problems come from the file format or how the file is served.

The markup

Add a <track> element inside the video:

  • kind="subtitles" for translations, or kind="captions" for same-language captions that include sound descriptions
  • src pointing to the .vtt file
  • srclang with the language code, such as en
  • label with the name shown in the player's menu, such as English
  • default on the track that should be on when the video starts

You can add several <track> elements for different languages; the browser's subtitle menu lists them by label.

The file must be WebVTT

Browsers do not read SRT files in a <track> element; they silently ignore them. The file has to start with WEBVTT and use dots before milliseconds. If you have an SRT file, convert it with the SRT to VTT converter. The differences are covered in SRT vs WebVTT.

Serving the file correctly

Two server-side details trip people up:

  • Content type. The server should send the file as text/vtt. Most hosts do this automatically for the .vtt extension; if captions fail on one server but work on another, check the response headers in your browser's developer tools.
  • Same origin or CORS. If the .vtt file is on a different domain than the page — a CDN or storage bucket, for example — the server must send CORS headers, and the <video> element needs the crossorigin attribute. Without both, the browser refuses to load the track.

Why captions do not appear

Work through these in order:

  1. The browser's captions are switched off. Use the player's CC menu, or add default to one track.
  2. The file is SRT, or not valid WebVTT. Open it in a text editor: the first line must be WEBVTT.
  3. Network error. Open the developer tools' network tab and check that the .vtt request succeeds with status 200.
  4. Encoding. Garbled characters mean the file is not UTF-8; re-save it, or run it through Fix Subtitles.
  5. Timing. Captions timed for a different cut of the video may simply appear at the wrong moments — see how to resync subtitles.

Styling

Browsers style captions with their own defaults, and users can override them in system accessibility settings. You can adjust the look with the CSS ::cue pseudo-element — font, colour, background — but keep contrast high and avoid forcing small text. WebVTT cue settings such as line:0 or align:start control position for individual cues.

Players and frameworks

Video.js, Plyr, hls.js and most other web players use the same <track> mechanism or accept a WebVTT URL in their configuration, so the same file works everywhere. YouTube and Vimeo embeds are different: upload the subtitle file to the platform itself rather than adding a track to the embed.