The HTML5 <track> element is used to add timed text tracks such as subtitles and captions to a video. 1. It works inside the <video> element, supporting multiple tracks for different languages or purposes. 2. Key attributes include kind (e.g., subtitles, captions), src (path to .vtt file), srclang (language code), label (user-friendly name), and default (auto-enable track). 3. WebVTT files define the timed text cues with specific formatting: first line is WEBVTT, each cue has a time range and text, separated by blank lines. 4. Subtitles translate dialogue for viewers who can hear audio, while captions describe both speech and non-speech elements for accessibility. Proper setup ensures browsers display text at correct times and users can select preferred tracks.
If you're adding subtitles or captions to a video on the web, the HTML5 <track></track>
element is the standard way to go. It's built right into the HTML5 video spec and gives you control over text overlays like subtitles, captions, descriptions, chapters, and metadata.

What the <track></track>
Element Does
The <track></track>
tag works inside a <video></video>
element to define timed text tracks. You can use it to show subtitles in different languages, provide closed captions for accessibility, or even add metadata cues that your JavaScript can react to.

Here’s a basic example:
<video controls> <source src="movie.mp4" type="video/mp4"> <track kind="subtitles" src="subtitles_en.vtt" srclang="en" label="English"> </video>
Each track has several attributes that help browsers know how to handle it.

Key Attributes of <track>
To get the most out of <track>
, you need to understand its main attributes:
kind
– Defines what type of text track it is. Options includesubtitles
,captions
,descriptions
,chapters
, andmetadata
.src
– The path to the WebVTT file (.vtt
) that contains the actual timed text.srclang
– Specifies the language of the track using ISO language codes (likeen
for English ores
for Spanish).label
– A user-readable name shown in the browser's menu.default
– If present, this track will be enabled by default when the video plays.
You can include multiple <track>
elements inside one <video>
tag — for example, to offer subtitles in different languages.
How to Create and Use WebVTT Files
Web Video Text Tracks (WebVTT) is the format used by <track>
. It looks simple but must follow specific formatting rules.
Here’s a quick example of a .vtt
file:
WEBVTT 00:00:01.000 --> 00:00:04.000 This is the first subtitle. 00:00:05.000 --> 00:00:07.500 And here comes the second one.
A few things to note:
- The first line must be
WEBVTT
. - Each cue starts with a time range, followed by the text.
- Blank lines separate cues.
- Browsers parse these files and display the text at the correct times.
Make sure to host your .vtt
files correctly and test them across browsers. Some older browsers may not support all features, so always check compatibility if you're targeting a broad audience.
When to Use Subtitles vs Captions
It’s easy to treat subtitles and captions as the same thing, but they serve slightly different purposes:
- Subtitles are meant for viewers who can hear the audio but might not understand the spoken language. They usually only contain dialogue.
- Captions are for viewers who can’t hear the audio. They include both speech and non-speech information like sound effects, music, or speaker identification.
Use the kind="subtitles"
attribute for translating spoken content, and kind="captions"
when you want to fully describe everything happening in the audio.
For example:
<track kind="captions" src="captions_en.vtt" srclang="en" label="English Captions" default>
Choosing the right kind improves accessibility and makes your video content more inclusive.
That’s basically it. Once you have your .vtt
files ready and linked properly, the browser handles the rest. Just make sure your file paths are correct, and you’re good to go.
The above is the detailed content of HTML5 `` element for video subtitles and captions. 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)

Hot Topics

HTML5isbetterforcontrolandcustomization,whileYouTubeisbetterforeaseandperformance.1)HTML5allowsfortailoreduserexperiencesbutrequiresmanagingcodecsandcompatibility.2)YouTubeofferssimpleembeddingwithoptimizedperformancebutlimitscontroloverappearanceand

The way to add drag and drop functionality to a web page is to use HTML5's DragandDrop API, which is natively supported without additional libraries. The specific steps are as follows: 1. Set the element draggable="true" to enable drag; 2. Listen to dragstart, dragover, drop and dragend events; 3. Set data in dragstart, block default behavior in dragover, and handle logic in drop. In addition, element movement can be achieved through appendChild and file upload can be achieved through e.dataTransfer.files. Note: preventDefault must be called

Yes,youcanrecordaudioandvideo.Here'show:1)Foraudio,useasoundcheckscripttofindthequietestspotandtestlevels.2)Forvideo,useOpenCVtomonitorbrightnessandadjustlighting.3)Torecordbothsimultaneously,usethreadinginPythonforsynchronization,oroptforuser-friend

Use and elements to add audio and video to HTML. 1) Use elements to embed audio, make sure to include controls attributes and alternate text. 2) Use elements to embed video, set width and height attributes, and provide multiple video sources to ensure compatibility. 3) Add subtitles to improve accessibility. 4) Optimize performance through adaptive bit rate streaming and delayed loading. 5) Avoid automatic playback unless muted, ensuring user control and a clear interface.

inputtype="range" is used to create a slider control, allowing the user to select a value from a predefined range. 1. It is mainly suitable for scenes where values ??need to be selected intuitively, such as adjusting volume, brightness or scoring systems; 2. The basic structure includes min, max and step attributes, which set the minimum value, maximum value and step size respectively; 3. This value can be obtained and used in real time through JavaScript to improve the interactive experience; 4. It is recommended to display the current value and pay attention to accessibility and browser compatibility issues when using it.

AnimatingSVGwithCSSispossibleusingkeyframesforbasicanimationsandtransitionsforinteractiveeffects.1.Use@keyframestodefineanimationstagesforpropertieslikescale,opacity,andcolor.2.ApplytheanimationtoSVGelementssuchas,,orviaCSSclasses.3.Forhoverorstate-b

Audio and video elements in HTML can improve the dynamics and user experience of web pages. 1. Embed audio files using elements and realize automatic and loop playback of background music through autoplay and loop properties. 2. Use elements to embed video files, set width and height and controls properties, and provide multiple formats to ensure browser compatibility.

WebRTC is a free, open source technology that supports real-time communication between browsers and devices. It realizes audio and video capture, encoding and point-to-point transmission through built-in API, without plug-ins. Its working principle includes: 1. The browser captures audio and video input; 2. The data is encoded and transmitted directly to another browser through a security protocol; 3. The signaling server assists in the initial connection but does not participate in media transmission; 4. The connection is established to achieve low-latency direct communication. The main application scenarios are: 1. Video conferencing (such as GoogleMeet, Jitsi); 2. Customer service voice/video chat; 3. Online games and collaborative applications; 4. IoT and real-time monitoring. Its advantages are cross-platform compatibility, no download required, default encryption and low latency, suitable for point-to-point communication
