Styling WebVTT Video Subtitles: Customizing ::cue, Positioning & Accessibility
To customize HTML5 video subtitles in pure CSS, target the ::cue pseudo-element (e.g., video::cue or video::cue(b)). You can style font-family, font-size (using clamp() or cqi units), color, background-color, text-shadow, and outline. To change where captions appear on screen, define WebVTT positioning cues directly inside your .vtt file using line: (vertical positioning e.g. line:85%), position: (horizontal anchor e.g. position:50%), and align: (text alignment).
Step 1: Author Semantic WebVTT Caption Files (.vtt)
WebVTT (Web Video Text Tracks) files begin with the WEBVTT header and define synchronized timestamp cues alongside optional positioning payload attributes.
WEBVTT
00:00:01.000 --> 00:00:04.500 line:85% position:50% align:center
Welcome to the <c.highlight>future of accessible</c> video styling.
00:00:05.000 --> 00:00:08.200 line:85% position:50% align:center
<v Alice>Alice:</v> Modern CSS <b class="bold-text">::cue</b> makes captions beautiful.
00:00:08.500 --> 00:00:12.000 line:20% position:50% align:center
<v Bob>Bob:</v> And you can reposition cues away from important lower-third graphics!
Step 2: Mount WebVTT Track Elements in HTML5 Video
Attach the .vtt file to your video element using the <track> tag with kind="subtitles" or kind="captions", specifying the source language and default attribute.
<video controls playsinline class="custom-sub-video">
<source src="/media/keynote.mp4" type="video/mp4">
<!-- Accessible Captions Track -->
<track
label="English"
kind="captions"
srclang="en"
src="/media/subtitles.vtt"
default
/>
</video>
Step 3: Style Captions with the CSS ::cue Pseudo-Element
Target video::cue to override browser-default subtitle formatting. You can adjust typography, padding, letterboxing background colors, and fluid font sizing.
/* Style all subtitles rendered within the video */
video::cue {
color: #ffffff;
background-color: rgba(3, 7, 18, 0.85);
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
font-size: clamp(14px, 2.2cqi, 22px); /* Responsive container-relative sizing */
font-weight: 600;
line-height: 1.4;
text-shadow: 0 1px 3px rgba(0, 0, 0, 0.9);
padding: 4px 12px;
border-radius: 6px;
}
/* Style specific voice speaker tags (e.g. <v Alice>) */
video::cue(v[voice="Alice"]) {
color: #38bdf8;
font-weight: 700;
}
video::cue(v[voice="Bob"]) {
color: #facc15;
font-weight: 700;
}
/* Style custom classes inside WebVTT cues (e.g. <c.highlight>) */
video::cue(.highlight) {
color: #ec4899;
text-decoration: underline;
}
Step 4: Ensure WCAG AA Accessibility and High Contrast Modes
Captions must maintain a 4.5:1 contrast ratio against the video background. Account for Windows High Contrast Mode and provide user-adjustable text size controls.
/* Respect Forced Colors Mode (Windows High Contrast) */
@media (forced-colors: active) {
video::cue {
color: CanvasText;
background-color: Canvas;
border: 1px solid CanvasText;
}
}
/* Dynamically adjust caption font scale via CSS Custom Properties */
.video-wrapper {
--caption-scale: 1;
}
.video-wrapper video::cue {
font-size: calc(16px * var(--caption-scale));
}
/* JavaScript trigger to boost caption size */
function setCaptionSize(multiplier) {
document.querySelector('.video-wrapper').style.setProperty('--caption-scale', multiplier);
}
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
Here's my golden rule when implementing this in production: test across multiple devices and browsers early. Keep your DOM hierarchy shallow, optimize assets with modern formats (like WebP/AVIF), and verify keyboard navigation for accessibility.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
What CSS properties are supported inside the ::cue selector?
The ::cue pseudo-element permits a restricted subset of CSS properties for security and rendering performance: color, background-color, font-family, font-size, font-style, font-weight, text-decoration, text-shadow, and outline. Complex properties like position, transform, display, or width are intentionally ignored by browsers.
How do I reposition subtitles to the top of the video screen?
Because ::cue ignores CSS position and top, positioning must be configured inside the .vtt cue header using line: syntax (e.g. 00:00:01.000 --> 00:00:05.000 line:10% position:50%). Values near line:10% position the subtitle at the top, while line:90% positions it at the bottom.
What is the difference between kind="subtitles" and kind="captions"?
Subtitles (kind="subtitles") provide speech translations for viewers who can hear the audio but do not speak the language. Captions (kind="captions" or SDH) include spoken dialogue plus sound effects, speaker names, and musical cues for deaf or hard-of-hearing audiences.
Can I style individual words differently inside a single caption line?
Yes. Use WebVTT class tags like <c.highlight>word</c> or voice tags like <v Alice>word</v> within your .vtt file. Then target them in CSS with video::cue(.highlight) or video::cue(v[voice="Alice"]).
Why are my custom ::cue styles not displaying in Safari on iOS?
When iOS plays video in native fullscreen player mode, the Apple operating system takes control of subtitle rendering, ignoring custom webpage CSS ::cue styles. To maintain custom styles on mobile, embed videos with the playsinline attribute.