**`<legend>`** must be the first child inside a `<fieldset>`, and browsers render it overlapping the fieldset's top border by default, like a labeled box. It plays an important accessibility role: screen readers announce the legend's text as context whenever the user focuses ANY control inside that fieldset — so for a group of radio buttons, each option gets announced together with the legend (e.g. 'Payment Method, Card, radio button'), giving the user the group's purpose along with each individual choice.
1Understanding <legend>
`<legend>` must be the first child inside a <fieldset>, and browsers render it overlapping the fieldset's top border by default, like a labeled box. It plays an important accessibility role: screen readers announce the legend's text as context whenever the user focuses ANY control inside that fieldset — so for a group of radio buttons, each option gets announced together with the legend (e.g. 'Payment Method, Card, radio button'), giving the user the group's purpose along with each individual choice.
For a group of radio buttons representing one question (like 'Payment Method'), the <legend> effectively acts as that question's label — screen readers announce it alongside every option in the group.
<fieldset>
<legend>Preferred Contact Time</legend>
<label><input type="radio" name="time" value="morning"> Morning</label>
<label><input type="radio" name="time" value="evening"> Evening</label>
</fieldset>2Practical Example
Here is a real-world application of <legend> showing how it is used in production HTML.
<!-- Styling the legend, which browsers otherwise render overlapping the border -->
<style>
legend { font-weight: bold; padding: 0 8px; }
</style>3Best Practices
Follow these guidelines when working with <legend>:
1. Always make <legend> the first child of its <fieldset>
2. Use it to describe the group's overall purpose (e.g. 'Payment Method'), not an individual option
3. Rely on it as the 'question' label for a related set of radio buttons or checkboxes
Tip: For a group of radio buttons representing one question (like 'Payment Method'), the <legend> effectively acts as that question's label — screen readers announce it alongside every option in the group.
<fieldset>
<legend>Preferred Contact Time</legend>
<label><input type="radio" name="time" value="morning"> Morning</label>
<label><input type="radio" name="time" value="evening"> Evening</label>
</fieldset>