Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

label attribute — a user-visible name

The label attribute names an optgroup group, an option, or a track. It is not a global attribute or a replacement for the label element. An option’s label and its submitted form value are separate concepts.

Version: WHATWG HTML Living Standard; index snapshot date recorded separately

Syntax

label="text"

label is a text attribute of optgroup, option, and track, not a global attribute. Do not confuse it with the label element, which labels a form control.

On optgroup, it names a group of options; the group itself is not selectable. The attribute is required when the group has no child legend. When the first element child is legend, that element’s text takes precedence over the attribute.

On option, a nonempty label supplies the label; otherwise, the label comes from the option’s content. The attribute must not be empty when specified. It does not replace value: without value, the value comes from the content, not label. When both label and value are specified, the option element must be empty. The selectedcontent element does not reflect the label attribute’s value.

On track, the attribute supplies a readable track title in the player interface. It is optional but must not be empty when specified; without it, the track label is an empty string. It does not specify the language — that is srclang’s role. Tracks of one media element with the same kind and language must not also have identical labels or both omit their labels.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Label attribute</title>
<label for="fruit">Fruit</label>
<select id="fruit" name="fruit">
  <optgroup label="Citrus">
    <option label="Orange" value="orange" selected></option>
  </optgroup>
</select>
<pre id="result"></pre>
<script>
  const select = document.getElementById('fruit');
  const option = select.options[0];
  document.getElementById('result').textContent =
    option.label + '\n' + select.value;
</script>
</html>

Result

Orange
orange

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources