日本語

How to Change Subtitle Color and Size in Video with CSS ::cue

​​CSS​HTML,subtitles,video,WebVTT

Subtitles (WebVTT captions) attached to a <video> with a <track> can be styled with the CSS ::cue pseudo-element. You can change the text color, size, background, and outline. Use it when subtitles in a course or product video are too small to read, when a black background band covers the picture, or when you want a different color for each speaker.

css
video::cue {
  color: #fff45c;
  font-size: 150%;
  background-color: transparent;
  text-shadow: 0 0 3px #000, 0 0 3px #000, 0 0 3px #000;
}
Note

The test environment was Ubuntu 24.04, Node.js 22.23.5, Playwright 1.63.0, and Chromium 158. Subtitle elements live in the browser's internal shadow DOM and cannot be read with getComputedStyle(), so I read the computed values and sizes through the Chrome DevTools Protocol. I did not test in Firefox or Safari. The browser support versions come from the compatibility data (@mdn/browser-compat-data 8.1.11).

Subtitles with no CSS

First, I displayed subtitles on a 640×360px video with no CSS written. The subtitle file had this content:

text
WEBVTT

00:00:00.000 --> 00:00:10.000
こんにちは、<b>字幕</b>のテストです
html
<style>
  video { display: block; width: 640px; height: 360px; }
</style>
<video src="sample.webm" muted>
  <track default kind="subtitles" srclang="ja" label="日本語" src="basic.vtt">
</video>

In the test environment, the text was white (rgb(255, 255, 255)), the background was black with an opacity of 0.8 (rgba(0, 0, 0, 0.8)), and the text size was 18px.

The text size changed with the size of the video element. In each of the four sizes I tried, the text size was 5% of the shorter of the element's width and height.

Video element size Subtitle text size
640×360px 18px
640×480px 24px
1280×720px 36px
360×640px (portrait) 18px

Changing color, background, and outline

The color and background-color you write in video::cue were used as they are. With color: yellow; background-color: rgb(0 0 0 / 40%), the text was rgb(255, 255, 0) and the background was rgba(0, 0, 0, 0.4).

If you remove the background band and add a shadow around the text, you can make the subtitles readable without hiding the picture. The bottom half of the image below shows the result of the CSS at the top of this article (background-color: transparent and three blurred black text-shadow layers).

Two video frames stacked vertically. The top one has no CSS and shows small white subtitle text reading "Hello, this is a subtitle test" over a black band. The bottom one uses ::cue: there is no band, and the text is larger, yellow, and has a black outline
Top: default subtitles. Bottom: subtitles with color, size, and outline changed by ::cue

Even if you write only ::cue { color: yellow; } without a video selector, the subtitles turned yellow. To change only some videos on a page, narrow it down with a class, such as .large::cue { font-size: 28px; }. In the test environment, only the subtitles of the video with class="large" became 28px, and the video without the class stayed at 18px.

Text size: using % makes it scale with the video

If you write font-size in px, the size stays the same no matter how big the video is. If you write % or em, it is a multiplier applied to the default size described in the previous section.

font-size in video::cue 640×360px video 1280×720px video
Not written 18px 36px
24px 24px 24px
150% 27px 54px
1.5em 27px 54px

1.5rem is based on the page's font size (html { font-size: 16px; }), and gave 24px.

Subtitles written in px were 24px whether the video was displayed at 640×360px or 1280×720px. If you want the subtitle size to follow the video size, write it in % or em.

Position and spacing cannot be changed with ::cue

Not everything you write in ::cue is applied. The MDN page for ::cue lists the properties that can be used. The main ones are:

  • color, opacity, visibility
  • background and its individual properties (such as background-color)
  • font and its individual properties (such as font-size and font-weight), and line-height
  • text-shadow, text-decoration, outline
  • white-space, text-combine-upright, ruby-position

I tested padding, border, and margin, which are not in the list:

css
video::cue {
  color: lime;
  padding: 20px;
  border: 2px solid red;
  margin-top: 50px;
}

In the test environment, only color was applied. The top padding, margin, and border widths all stayed at 0px. The size of the subtitle background was also the same as with no CSS, 257×20px.

The position where subtitles appear is set not in CSS but in the cue settings written for each subtitle in the VTT file. The MDN explanation of the WebVTT format says that the STYLE block in a VTT file (explained in a later section) can change the look and size of subtitles, while position and layout are determined by the cue settings.

Changing color for part of a subtitle or per speaker

If you write a selector inside the parentheses of ::cue(), you can select only part of a subtitle. Inside the subtitle text, you can use the <b> and <i> tags, parts given a class with <c.classname>, and parts marked with a speaker using <v speaker name>.

text
WEBVTT

00:00:00.000 --> 00:00:10.000
<v たま>今日は<b>雨</b>だね</v>
<v みけ><c.yellow>傘</c>を持っていこう</v>
css
video::cue {
  font-size: 130%;
}
video::cue(v[voice="たま"]) {
  color: #7fe6f8;
}
video::cue(.yellow) {
  color: yellow;
}
video::cue(b) {
  color: #ff8fa8;
}

You select a speaker with an attribute selector on voice, such as v[voice="たま"]. In the test environment, the line for the speaker Tama was light blue, the word "rain" inside <b> was pink, and the word "umbrella" in <c.yellow> was yellow. The line for the speaker Mike, which had no rule, stayed white.

Two lines of subtitles appear at the bottom of the video frame. The first line, "It's rainy today," is light blue, with only "rain" in pink. The second line, "Let's take an umbrella," is white, with only "umbrella" in yellow
Subtitles with color changed per speaker, tag, and class using ::cue()

The same applies to ::cue(): writing padding: 10px in video::cue(b) kept the top padding of <b> at 0px. On the other hand, video::cue(b) { font-size: 30px; } took effect, and only "rain" became 30px.

The STYLE block in the VTT file took priority over the page CSS

You can also write the look of subtitles inside the VTT file. Put a block that starts with STYLE before the first subtitle, and write the CSS for ::cue in it. The block cannot contain blank lines.

text
WEBVTT

STYLE
::cue {
  color: lime;
  font-size: 30px;
}

00:00:00.000 --> 00:00:10.000
こんにちは、<b>字幕</b>のテストです

In the test environment, the subtitles from this VTT file were green (rgb(0, 255, 0)) at 30px.

In this case, the color written in the page CSS was not used. Whether I wrote color: yellow in video::cue, ::cue, or #v::cue (for the video with id="v"), the subtitles stayed green from the VTT file. Only when I wrote color: yellow !important in the page CSS did the subtitles turn yellow. If the subtitle color you set in the page CSS is not applied, check whether the VTT file has a STYLE block at the top.

Browser support

The support status in the compatibility data is as follows:

Browser ::cue ::cue() (with a selector in the parentheses)
Chrome and Edge From Chrome 26 and Edge 79 From Chrome 26 and Edge 79
Safari (including iOS) From 7 From 7
Firefox (including Android) From 55 Not supported

Firefox does not support ::cue(), which changes color by speaker or tag. According to notes in the compatibility data, from Firefox 69 onward, properties written in ::cue without parentheses are applied only if they appear in the MDN list.

​Posted by 86