Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / HTML

legend — group caption

The `legend` element captions a group in `fieldset` or labels an option group in `optgroup`. It must be the first element child; it does not replace labels for individual controls.

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

Syntax

<fieldset>
  <legend>Group caption</legend>
  <!-- Group contents -->
</fieldset>

A legend placed as the first element child of fieldset captions the remaining group contents. Use it for a shared question or group name, and use label for individual controls.

Both tags are required. The element accepts global attributes. Inside fieldset, it can contain phrasing content, such as text and emphasis, optionally intermixed with headings. It is not an unrestricted block container.

legend is also allowed as the first element child of optgroup, where it supplies the option group's label. In that context, only phrasing content is permitted, with no interactive content and no descendants with a tabindex attribute.

When a fieldset has disabled, that disabling excludes controls inside its first legend child; other causes can still disable them. The read-only DOM property legend.form returns the parent fieldset's form or null; without such a parent, it also returns null.

Examples

<!doctype html>
<html lang="en">
<meta charset="utf-8">
<title>Legend example</title>
<form id="preferences">
  <fieldset>
    <legend id="caption">Contact method</legend>
    <label><input type="radio" name="contact" value="email" checked> Email</label>
    <label><input type="radio" name="contact" value="phone"> Phone</label>
  </fieldset>
</form>
<script>
  const caption = document.getElementById('caption');
  console.log(caption.textContent);
  console.log(caption.form.id);
</script>
</html>

Result

Contact method
preferences

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