<track> Tag in HTML | Adding Text Tracks to Media
The <track> tag in HTML is used to specify text tracks (such as subtitles, captions, or descriptions) for media elements like <video>
and <audio>
. It allows users to better understand media content, making it accessible to a wider audience.
Key Points on <track> Tag:
- Media Support: The
<track>
tag must be used inside a<video>
or<audio>
element. - Types of Tracks: It supports different track types, such as subtitles (
subtitles
), captions (captions
), descriptions (descriptions
), chapters (chapters
), and metadata (metadata
). - Accessibility: The
<track>
tag enhances accessibility by providing captions and subtitles, making content accessible to users with hearing impairments. - Kind Attribute: The
kind
attribute specifies the type of track (e.g.,subtitles
,captions
), anddefault
can indicate the default track to display. - File Formats: Track files typically use the WebVTT (.vtt) format for compatibility.
Syntax of <track> Tag:
Syntax Example
<video controls>
<source src="movie.mp4" type="video/mp4">
<track src="subtitles_en.vtt" kind="subtitles" srclang="en" label="English" default>
</video>
Example of Using <track> Tag:
This example shows a video with an English subtitle track.
Code Example
<video controls width="400">
<source src="sample_video.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>
Output
Attributes of <track> Tag:
- src: Specifies the path to the track file (e.g., subtitles file).
- kind: Defines the track type, such as
subtitles
,captions
,descriptions
,chapters
, ormetadata
. - srclang: Specifies the language of the track (e.g., "en" for English).
- label: Provides a user-friendly label for the track (e.g., "English").
- default: Indicates that this track should be the default displayed track.
Best Practices:
- Use WebVTT Format: Use WebVTT (.vtt) format for compatibility across most modern browsers.
- Accessibility: Add captions and subtitles to enhance accessibility for users with hearing impairments.
- Multiple Languages: Use multiple
<track>
elements to support captions in different languages.
The <track>
tag is a powerful feature for enhancing media accessibility. Adding subtitles, captions, and other types of tracks allows users to fully engage with content, especially when audio may be difficult to hear or understand.