Fieldset and Legend for Form Groups
What you'll learn: How to semantically group related form controls so assistive technologies announce their shared context.
Why Group Form Controls?
Imagine filling out a paper form where related questions are grouped together with a box and a title—like "Shipping Address" containing street, city, and zip code fields. That visual grouping helps you understand the questions share a common purpose. Screen reader users need the same clarity, but visual boxes alone don't convey this relationship.
The <fieldset> and <legend> Elements
The <fieldset> element wraps related form controls together, creating a programmatic group. The <legend> element provides a caption for that group, like a heading that describes what all the fields have in common.
When a screen reader enters a fieldset, it announces the legend text along with each input's label, giving users essential context without repetition.
<fieldset>
<legend>Shipping Address</legend>
<label for="street">Street:</label>
<input type="text" id="street" name="street">
<label for="city">City:</label>
<input type="text" id="city" name="city">
<label for="zip">ZIP Code:</label>
<input type="text" id="zip" name="zip">
</fieldset>
A screen reader might announce: "Shipping Address group, Street, edit text" instead of just "Street, edit text"—the user now knows this street belongs to the shipping address.
When to Use Fieldset
Use <fieldset> and <legend> for:
- Radio button groups (e.g., "Select your plan: Basic, Pro, Premium")
- Checkbox groups with a common theme
- Related text inputs (like address components)
- Multi-part date or time inputs
Key Takeaway: Use <fieldset> with <legend> to create semantic groups of related form controls, ensuring assistive technologies announce the shared context that sighted users perceive visually.