References

Beginner-friendly references for web development, with live, editable examples.

The HTML srclang attribute

Attribute All modern browsers Updated
In short

The HTML srclang attribute states the language of a <track> element's text. It takes a BCP 47 language tag such as en, es or pt-BR, it is required when kind is subtitles or missing, and browsers can use it to match tracks to a viewer's language preferences.

Overview

The srclang attribute gives the language of a text track's cues. It belongs on the <track> element and takes a valid BCP 47 language tag, the same format as the lang attribute, so values like en, fr, pt-BR and zh-Hant all work.

The attribute is required in one case. The HTML Standard says srclang must be present when the track's kind is subtitles, and because a missing kind means subtitles, a bare <track src="subs.vtt"> needs it too. On other kinds it is optional but still useful.

The value becomes the track's language in the DOM. When srclang has a non-empty value, textTrack.language returns it, and otherwise the track has no language. Browsers can compare that language, along with kind and label, against a viewer's caption preferences to turn on a matching track automatically.

The entry in the captions menu comes from label. When a track has no label, the HTML Standard says the browser should generate one from properties such as the track's kind and language. The language of the surrounding page comes from lang, so a Spanish subtitle track on an English page still uses srclang="es".

Syntax

<track kind="subtitles" src="subs-fr.vtt" srclang="fr" label="Français">

Values

Value
A BCP 47 language tag, e.g. en.

Example

Live example
<video id="v" src="https://codeshack.io/web/example.mp4" controls muted width="240">
  <track kind="subtitles" srclang="en" label="English" src="data:text/vtt,WEBVTT%0A%0A00:00.000%20--%3E%2000:31.000%0AEnglish%20subtitles">
  <track kind="subtitles" srclang="fr" label="Français" src="data:text/vtt,WEBVTT%0A%0A00:00.000%20--%3E%2000:31.000%0ASous-titres%20en%20fran%C3%A7ais">
</video>
<p id="out"></p>
<p><small>Each track inlines its WebVTT text as a data: URL. A real page points src at a .vtt file.</small></p>
<script>
  const wanted = navigator.language.split('-')[0];
  let picked = 'none';
  for (const track of document.getElementById('v').textTracks) {
    track.mode = track.language === wanted ? 'showing' : 'disabled';
    if (track.mode === 'showing') picked = track.label;
  }
  document.getElementById('out').textContent = 'Browser language: ' + navigator.language + ', showing: ' + picked;
</script>

Best practices

  • Use a tag no more specific than the text needs, such as es, and add a region only when it distinguishes the text, as with es-419 for Latin American and Caribbean Spanish. RFC 5646 advises using as precise a tag as possible, but no more specific than is justified.
  • Set srclang on every subtitles track and on any track without a kind, because the HTML Standard requires it there.
  • Match srclang to the language of the cue text, not to the page or the spoken audio.
  • Pair srclang with a readable label, since the label is the title browsers show when they list tracks.

Frequently asked questions

What is the srclang attribute used for on a track?
The srclang attribute tells the browser which language a track's text is written in. It takes a BCP 47 tag such as en or fr and becomes the TextTrack language in JavaScript.
Is srclang required on the track element?
The srclang attribute is required when a track's kind is subtitles, including when kind is missing. For captions, chapters, descriptions and metadata tracks it is optional.
What is the difference between srclang and lang?
The srclang attribute gives the language of the external track file's text. The lang attribute marks the language of an element's own content and text attributes, such as the label.
What format does srclang use?
The srclang attribute takes a BCP 47 language tag, the same format as lang and hreflang. Valid examples include en, en-GB, es-419 and zh-Hant.
Does srclang turn on subtitles in the viewer's language automatically?
The srclang attribute makes automatic language matching possible. Browsers may compare it with the viewer's caption preferences, and the default attribute marks the track to enable when those preferences do not pick one.
Can srclang differ from the language spoken in the video?
Yes, srclang describes the text of the track, not the audio. English subtitles on a French film use srclang set to en, and the HTML Standard describes subtitles as a transcription or translation of the dialogue.