How to add captions or subtitles to an HTML5 video?
Jul 27, 2025 am 12:06 AMUse the <track> element inside the <video> tag to add captions or subtitles. 2. Set the src attribute to point to the WebVTT file. 3. Specify the kind attribute as "subtitles", "captions", "descriptions", "chapters", or "metadata". 4. Include srclang for language identification when kind is "subtitles". 5. Add a label for the display name in the player menu. 6. Optionally use the default attribute to enable the track by default. 7. Ensure the WebVTT file starts with "WEBVTT", uses correct time formatting (HH:MM:SS.mmm), and has blank lines between cues. 8. Save the file in UTF-8 encoding. 9. Provide a fallback message inside the <video> tag for unsupported browsers. 10. Test browser compatibility and consider using libraries like video.js for enhanced support. 11. Style captions using the ::cue CSS pseudo-element. Captions enhance accessibility and user experience and are straightforward to implement when these steps are followed correctly.
Adding captions or subtitles to an HTML5 video is straightforward using the <track></track>
element. This feature enhances accessibility and improves user experience, especially for viewers who are deaf or hard of hearing, or those watching in noisy environments.

Use the <track></track>
Element Inside the <video></video>
Tag
The <track></track>
element allows you to specify external text tracks (like subtitles or captions) in supported formats—most commonly WebVTT (.vtt files).
Here’s a basic example:

<video controls> <source src="movie.mp4" type="video/mp4"> <track src="subtitles.en.vtt" kind="subtitles" srclang="en" label="English" default> <track src="subtitles.es.vtt" kind="subtitles" srclang="es" label="Spanish"> Your browser does not support the video tag or text tracks. </video>
Understand the <track>
Attributes
Each <track>
tag supports several important attributes:
src
: Path to the WebVTT file (e.g.,subtitles.en.vtt
)kind
: Specifies the type of text track:subtitles
– Translations of the dialogue (useful for non-native speakers)captions
– Includes dialogue and sound descriptions (ideal for accessibility)descriptions
– Text descriptions of the video for screen readerschapters
– Chapter titles for navigationmetadata
– Used for scripting, not visible to users
srclang
: Language of the track (e.g.,en
,es
,fr
). Required whenkind="subtitles"
.label
: Human-readable name shown in the player’s menu (e.g., "English").default
: (Optional) Tells the browser to enable this track by default. Only one track should have this attribute.
Format Your WebVTT File Correctly
WebVTT files must start with WEBVTT
and use a specific time format. Example (subtitles.en.vtt
):

WEBVTT 1 00:00:05.000 --> 00:00:08.000 Hello, welcome to the video. 2 00:00:10.000 --> 00:00:13.000 This is an example of subtitles.
Make sure:
- The file is saved with UTF-8 encoding
- Time format is
HH:MM:SS.mmm
- There’s a blank line between cues
Browser Support and Fallbacks
Most modern browsers support <track>
and WebVTT, but:
- Always include a fallback message inside the
<video>
tag - Test on target browsers (especially older versions of Safari or mobile browsers)
- Consider using JavaScript libraries like video.js for broader compatibility and better UI controls
Also, note that:
- Captions won’t display unless the user enables them (except the
default
track) - Styling captions is limited, but you can use
::cue
CSS pseudo-elements:
video::cue { background-color: black; color: white; font-size: 1.2em; }
Basically, just link your .vtt
file with the right attributes and ensure the format is correct. It’s simple but easy to miss small details like the srclang
or kind
values.
The above is the detailed content of How to add captions or subtitles to an HTML5 video?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

nfo files are not subtitles, and subtitle files are not in nfo format; common subtitle file formats include Srt format, Webvtt format, STL format, Sbv format, Ass format, Dfxp format, Ttml format, etc.; and nfo records the video producer’s production process A file of various data information for the film.

How to turn on live subtitles instantly in Windows 11 1. Press Ctrl+L on your keyboard 2. Click Agree 3. A popup will appear saying Ready to add subtitles in English (US) (depending on your preferred language) 4. Additionally, you can filter profanity by clicking the gear button? Preference? Filtering Swear Words Related Articles How to Fix Activation Error Code 0xc004f069 in Windows Server The activation process on Windows sometimes takes a sudden turn to display an error message containing this error code 0xc004f069. Although the activation process is online, some older systems running Windows Server may experience this issue. Pass these preliminary checks and if these checks do not

How to adjust the subtitles of karaoke? The subtitles of lyrics can be adjusted in the National Karaoke APP, but many users do not know how to adjust the subtitles of Karaoke moments. The latest solutions to the problems will be continuously sorted out below. Next is the National Karaoke that the editor brings to users. Here is a graphic tutorial on how to adjust subtitles. Interested users can take a look together! Tutorial on the use of National Karaoke. How to adjust the subtitles of National Karaoke. 1. First open the Universal Karaoke APP, switch to the [Voice Sing] section on the main page, select your favorite song and click [K Song]; 2. Then switch to the Karaoke page, Click the red button [Start Singing] at the bottom; 3. Then on the latest page, click the [three dots] function in the upper right corner; 4. Finally, expand the function bar at the bottom and select [Font Size] to adjust

[Dubbing] 1. Open the clipping app and import the video file to which dubbing needs to be added. 2. Click the "Audio" button at the bottom of the video editing interface and select the "Recording" option. 3. Click the red recording button below to start recording the desired dubbing content. 4. After the recording is completed, click the "Export" button in the upper right corner and save it. [With subtitles] 1. Open the clipping app and import the video file to which subtitles need to be added. 2. Click the "Text" button at the bottom of the video editing interface and select the "New Text" option. 3. Enter the subtitles you want to add in the text box, and then set

Many people are using the computer version of the computer version in the office, but do you know how to recognize the subtitles in the computer version of the computer version? Next, the editor will bring you the method of identifying the subtitles in the computer version of the computer version. Interested users should quickly Let’s take a look below. Open the computer version of the clip and enter the homepage. Select text in the menu bar above the cutout. Just select Start Recognition under Smart Subtitles.

Add subtitles: 1. Enter the software and click [Start Creating]. 2. Click [Text] below. 3. Select [New Text]. 4. Enter text, adjust the style and save. 5. The addition is completed. Add dubbing: 6. Select [Audio]. 7. Click [Recording]. 8. Start recording. Introductory Tutorial on Screen Cutting 1. Video Segmentation If you want to learn video editing, you must learn video segmentation. For users who have short video production needs, learning video segmentation can allow us to learn video in a very short time. For the basic operations of editing, next we use the Windows 10 version of Silhouette (Shadow) to demonstrate how to split a video. As shown below: The material of the video is a TV series image. New users can use this type of video first.

1. Now if you want to set up automatic subtitles, the first step is to shoot a video through Douyin, or import a local video file on your phone, enter the video editing page, and find the drop-down button in the upper right corner (as shown below). 2. Through the drop-down list, we can find the new [Automatic Subtitles] function. Click to identify the voice in the current video and add subtitles. 3. Moreover, the color, format and other effects of subtitles can now be automatically set by you, allowing more users to enjoy the convenience. If you are interested, start now.

How to play a video file with subtitles using PHP? With the development of Internet technology, video has become one of the most popular forms of online content. Now, many video files are equipped with subtitles, which makes it easier for users to understand and digest video content. So, how to use PHP to play video files with subtitles on a web page? This article will introduce it to you in detail. First, we need to prepare the following environment: a server with a PHP running environment (such as Apache or Nginx); a video file (support
