How to add subtitles to a video using the track element in HTML
Adding subtitles to videos using HTML requires the <track> element in the
Adding subtitles to a video in HTML is straightforward using the <track></track>
element alongside the <video></video>
tag. This method allows you to provide timed text tracks such as subtitles or captions, improving accessibility and user experience.

How the <track></track>
Element Works
The <track></track>
element is used inside a <video></video>
tag to specify external text tracks. It supports several kinds of text content, but for subtitles, you'll typically use kind="subtitles"
.
Here's the basic syntax:

<video controls> <source src="movie.mp4" type="video/mp4"> <track src="subtitles_en.vtt" kind="subtitles" srclang="en" label="English" default> Your browser does not support the video tag. </video>
Required Attributes for Subtitles
To make subtitles work correctly, you need to set a few key attributes on the <track>
element:
-
src
: The URL of the WebVTT file (eg,subtitles_en.vtt
) -
kind
: Set to"subtitles"
to indicate this track contains subtitles -
srclang
: The language of the subtitles (eg,"en"
for English,"es"
for Spanish) -
label
: A user-readable label showed in the player's menu (eg, "English") -
default
(optional) : Indicates this track should be enabled by default if the user's browser settings don't specify a preference
Note: The
default
attribute should only be used on one track to avoid conflicts.
Creating a WebVTT File
The subtitle file must be in WebVTT format (Web Video Text Tracks), which is a plain text format with time cues and text to display.
Example: subtitles_en.vtt
WEBVTT 1 00:00:01.000 --> 00:00:04.000 Hello, welcome to this video. 2 00:00:05.000 --> 00:00:08.000 Here we explain how to add subtitles.
Make sure:
- The file starts with
WEBVTT
(followed by a blank line) - Time format is
HH:MM:SS.mmm
→HH:MM:SS.mmm
- There's a blank line between cues
- The file is saved with UTF-8 encoding
Multiple Subtitle Languages
You can offer subtitles in multiple languages by adding more <track>
elements:
<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"> <track src="subtitles_fr.vtt" kind="subtitles" srclang="fr" label="French"> </video>
Users can switch between languages using the video player's subtitle menu (available in most modern browsers).
Important Notes
- The WebVTT file must be hosted on the same domain or served with proper CORS headers if hosted externally.
- Browsers may not display subtitles if the video is not played with user interaction (eg, autoplay restrictions).
- Always test across browsers—support is good in modern ones, but styling and positioning of subtitles can vary.
Basically, with a proper WebVTT file and the right <track></track>
attributes, adding subtitles becomes simple and effective.
The above is the detailed content of How to add subtitles to a video using the track element in HTML. 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)

It is a block-level element, used to divide large block content areas; it is an inline element, suitable for wrapping small segments of text or content fragments. The specific differences are as follows: 1. Exclusively occupy a row, width and height, inner and outer margins can be set, which are often used in layout structures such as headers, sidebars, etc.; 2. Do not wrap lines, only occupy the content width, and are used for local style control such as discoloration, bolding, etc.; 3. In terms of usage scenarios, it is suitable for the layout and structure organization of the overall area, and is used for small-scale style adjustments that do not affect the overall layout; 4. When nesting, it can contain any elements, and block-level elements should not be nested inside.

linkrel="preload" is a technology that optimizes page loading performance and is used to load critical resources in advance. Its core purpose is to prioritize loading of resources that are critical to home screen rendering, such as fonts, key CSS/JS and home screen images. Pay attention to when using: 1. Set the as attribute correctly to specify the resource type; 2. Avoid abuse and prevent excessive bandwidth usage; 3. Ensure that the resources will be actually used, otherwise it will cause waste of requests; 4. Add crossorigin attribute to cross-domain resources. Incorrect writing method such as the lack of the as attribute will cause the preload to be invalid. Rational use can improve page loading efficiency, otherwise it may be counterproductive.

To get started with HTML quickly, you only need to master a few basic tags to build a web skeleton. 1. The page structure is essential, and, which is the root element, contains meta information, and is the content display area. 2. Use the title. The higher the level, the smaller the number. Use tags to segment the text to avoid skipping the level. 3. The link uses tags and matches the href attributes, and the image uses tags and contains src and alt attributes. 4. The list is divided into unordered lists and ordered lists. Each entry is represented and must be nested in the list. 5. Beginners don’t have to force memorize all tags. It is more efficient to write and check them while you are writing. Master the structure, text, links, pictures and lists to create basic web pages.

ShadowDOM is a technology used in web component technology to create isolated DOM subtrees. 1. It allows the mount of an independent DOM structure on ordinary HTML elements, with its own styles and behaviors, and does not affect the main document; 2. Created through JavaScript, such as using the attachShadow method and setting the mode to open; 3. When used in combination with HTML, it has three major features: clear structure, style isolation and content projection (slot); 4. Notes include complex debugging, style scope control, performance overhead and framework compatibility issues. In short, ShadowDOM provides native encapsulation capabilities for building reusable and non-polluting UI components.

❌Youcannotnesttagsinsideanothertagbecauseit’sinvalidHTML;browsersautomaticallyclosethefirstbeforeopeningthenext,resultinginseparateparagraphs.✅Instead,useinlineelementslike,,orforstylingwithinaparagraph,orblockcontainerslikeortogroupmultipleparagraph

TheHTMLdownloadattributeallowsuserstodownloadfilesdirectlyfromalinkbyusingthetag.Toimplementit,adddownloadtotheanchortag,suchasDownloadPDF,orspecifyacustomfilenamelikeDownloadasmy-document.pdf.1.Itworksbestwithsame-originURLsandcommonfiletypeslikePDF

The style placement method needs to be selected according to the scene. 1. Inline is suitable for temporary modification of single elements or dynamic JS control, such as the button color changes with operation; 2. Internal CSS is suitable for projects with few pages and simple structure, which is convenient for centralized management of styles, such as basic style settings of login pages; 3. Priority is given to reuse, maintenance and performance, and it is better to split external link CSS files for large projects.

To embed YouTube videos in a web page, you can directly copy the embedded code provided by YouTube and paste it into the HTML page; 1. Open the target video page and confirm the correct video link; 2. Click "Share" → "Embed" to copy the generated iframe code; 3. Or manually create the iframe tag and set the src to https://www.youtube.com/embed/video ID; 4. It is recommended to use responsive width and add allowfullscreen attributes; 5. Advanced suggestions include using containers to achieve 16:9 ratios, delay loading to improve performance, and pay attention to privacy policy tips.
