<media-captions-button>

The <media-captions-button> component is used to toggle captions and subtitles on and off. When turning on captions and subtitles, captions tracks will be preferred over subtitles tracks, but either type will be used.

The contents and behavior of the <media-captions-button> will update automatically based on the text track media state.

  • When any captions or subtitles track is shown, the component will switch to display the contents of the on slot.
  • When all captions and subtitles track are disabled, the component will switch to display the content of the off slot.
<media-controller defaultsubtitles>
  <video
    playsinline muted crossorigin
    slot="media"
    src="https://stream.mux.com/A3VXy02VoUinw01pwyomEO3bHnG4P32xzV7u1j1FSzjNg/low.mp4"
  >
    <track label="English" kind="captions" srclang="en" src='https://media-chrome.mux.dev/examples/vanilla/vtt/en-cc.vtt' />
  </video>
  <media-captions-button></media-captions-button>
</media-controller>

You can modify the contents of the <media-captions-button> component using slots. This is useful if you’d like to use your own custom captions button instead of the default one provided by media-chrome.

Here’s an example of how you can replace the default CC on and CC off icons with CC CC, respectively. Media Controller is also set up to enable captions and subtitles by default.

<media-controller defaultsubtitles>
  <video
    playsinline muted crossorigin
    slot="media"
    src="https://stream.mux.com/A3VXy02VoUinw01pwyomEO3bHnG4P32xzV7u1j1FSzjNg/low.mp4"
  >
    <track label="English" kind="captions" srclang="en" src='https://media-chrome.mux.dev/examples/vanilla/vtt/en-cc.vtt' />
  </video>
  <media-captions-button>
    <span slot="on"><b><u>CC</u></b></span>
    <span slot="off">CC</span>
  </media-captions-button>
</media-controller>

The <media-captions-button> doesn’t expose any configuration attributes. However, it will be updated with Media UI Attributes any time the selected caption or subtitle changes.

You can use these attributes to style the button. For example, if there aren’t any subtitles, hide the button

media-captions-button:not([mediasubtitleslist]) {
  display: none;
}

Or make the background blue if a captions is showing.

media-captions-button[mediasubtitlesshowing] {
  --media-control-background: blue;
}
Name Description
on An element that will be shown while closed captions or subtitles are on.
off An element that will be shown while closed captions or subtitles are off.
Name Type Description
disabled boolean The Boolean disabled attribute makes the element not mutable or focusable.
mediacontroller string The element `id` of the media controller to connect to (if not nested within).

The media UI attributes will be set automatically by the controller if they are connected via nesting or the mediacontroller attribute. Only set these attributes manually if you know what you're doing.

Name Type Description
mediasubtitleslist string A list of all subtitles and captions.
mediasubtitlesshowing boolean A list of the showing subtitles and captions.
Name Default Description
--media-captions-button-display inline-flex display property of button.
--media-primary-color rgb(238 238 238) Default color of text and icon.
--media-secondary-color rgb(20 20 30 / .7) Default color of button background.
--media-text-color var(--media-primary-color, rgb(238 238 238)) color of button text.
--media-icon-color var(--media-primary-color, rgb(238 238 238)) fill color of button icon.
--media-control-display display property of control.
--media-control-background var(--media-secondary-color, rgb(20 20 30 / .7)) background of control.
--media-control-hover-background rgba(50 50 70 / .7) background of control hover state.
--media-control-padding 10px padding of control.
--media-control-height 24px line-height of control.
--media-font var(--media-font-weight, bold) var(--media-font-size, 14px) / var(--media-text-content-height, var(--media-control-height, 24px)) var(--media-font-family, helvetica neue, segoe ui, roboto, arial, sans-serif) font shorthand property.
--media-font-weight bold font-weight property.
--media-font-family helvetica neue, segoe ui, roboto, arial, sans-serif font-family property.
--media-font-size 14px font-size property.
--media-text-content-height var(--media-control-height, 24px) line-height of button text.
--media-button-icon-width width of button icon.
--media-button-icon-height var(--media-control-height, 24px) height of button icon.
--media-button-icon-transform transform of button icon.
--media-button-icon-transition transition of button icon.