Is it possible to style text tracks (like subtitles and captions) in HTML5 video players?
I already found a way to do so for Chrome:
video::-webkit-media-text-track-container {
// Style the container
}
video::-webkit-media-text-track-background {
// Style the text background
}
video::-webkit-media-text-track-display {
// Style the text itself
}
This seems to confuse Safari a bit. It works, but the rendering is quite buggy.
But more important: How to achieve do this for Firefox and IE?
The only cross-browser solution I have found to date is: Hide the video’s text tracks and use your own.
This will allow you to create your own text nodes, with classes, id’s etc. which can then be styled simply via css.
In order to do so, you would utilize the onenter and onexit methods of the text cues in order to implement your own text nodes.
Use this for Chrome:
Firefox:
Not yet supported. Open bug to implement ::cue pseudo-element - https://bugzilla.mozilla.org/show_bug.cgi?id=865395
Edit:
Support for FireFox is available, it works similarly as it does in Chrome and Opera. But support is not yet available for Edge or IE.
I set out to style my captions to have a black background and be positioned below the video for Safari and Chrome. I have achieved success with the following code combined with editing the .vtt file with the following styles. Note you must add the styles to the .vtt file or else in safari your captions will jump around when the video controls (even if they're hidden) would appear:
Styles for chrome and safari captions:
Chrome uses the video::cue background-color and opacity.
Safari uses -webkit-media-text-track-display-backdrop for it's background color. Note the !important which overrides Safari's inherent styling.
The following webkit-media-text-track-display overflow is allow for more padding around Chrome's caption text:
Overflow visible is important on the following code for Safari and I'm setting the captions below the video with the transform, which is reliant on a fixed font-size:
EDIT
With some tweaking I ended up using this for my project:
Important: Delete all inline styling from your .VTT file.
Determine if the user is using chrome or safari.
Then in a SASS .scss file use the following styles. NOTE: If you're not using SASS you can simply create a class for the video element and nest the corresponding styles.
This is working for chrome,
you can also get some information from those links.
Link 1
Link 2