Sub topic 10
Forms: labels, fieldsets and legends
Prerequisites and scope
This chapter assumes you know basic HTML syntax (tags, attributes, nesting) and can open your browser's developer tools. It covers how to associate labels with controls, how to group related controls, and which common errors break that association. Native interactive elements such as button get a fuller treatment in a later chapter. Here we only use them where a form needs them.
The working model: a form control needs a name
A form control is an element the user can type into or choose from, such as input, select or textarea. Every control needs an accessible name. This is the text that a screen reader announces and that speech-control software matches when a user says "click Email address".
Placeholder text and nearby text do not provide this name reliably. A label element does. It creates a programmatic link between the text and the control. Without that link, the browser has only visual proximity, and assistive technology cannot use that.
A label gives you three practical benefits:
- A screen reader announces the label text when the control gets focus.
- Clicking or tapping the label focuses the control. For checkboxes and radio buttons, it also toggles them. This enlarges the target for people with limited motor control.
- Speech-control users can refer to the control by its visible text.
Two ways to associate a label
Explicit association with for and id
The for attribute on the label holds the id of the control. The two elements can be anywhere in the document, as long as the id is unique.
<label for="email">Email address</label>
<input id="email" name="email" type="email" required>Implicit association by nesting
If the control is inside the label, the association is automatic and no id is needed.
<label>
<input type="checkbox" name="weekly"> Weekly digest
</label>Nesting suits checkboxes and radio buttons, where the text sits beside the control. For text inputs, the explicit form is usually easier to style and is the more widely used pattern. You can also combine both forms. This is harmless and sometimes helps older assistive technology.
Id and name are different attributes
Beginners often mix up two attributes that look alike.
| Attribute | Purpose | Must be unique? |
|---|---|---|
| `id` | Lets `label for`, CSS and scripts find the element | Yes, across the whole page |
| `name` | The key sent to the server with the value | No. Radio buttons in one group share a name |
A label connects to id. The server receives name. A control with no name is not submitted at all.
Grouping with fieldset and legend
Some controls only make sense together. Three radio buttons for "Delivery speed" each have their own label, such as "Standard" or "Express". Heard alone, "Express" does not say what is being chosen. The fieldset element groups the controls, and its first child legend supplies the group's name.
<form action="/order" method="post">
<fieldset>
<legend>Delivery speed</legend>
<label>
<input type="radio" name="speed" value="standard" checked> Standard
</label>
<label>
<input type="radio" name="speed" value="express"> Express
</label>
</fieldset>
<button type="submit">Place order</button>
</form>Browsers expose a fieldset as a group, and a legend as the group's name. When a screen reader user moves into the first radio button, many screen readers announce something like "Delivery speed, group. Standard, radio button, 1 of 2". The exact wording differs between screen readers, but the legend text is included.
Use fieldset and legend for:
- Radio button groups, where it is almost always needed.
- Groups of related checkboxes, such as "Notify me about".
- Sets of fields that form one unit, such as the parts of a postal address.
Do not wrap every pair of fields in a fieldset. Extra groups add announcements without adding meaning. A single email field needs a label, not a group.
How the pieces map to the accessibility tree
The browser builds an accessibility tree from your markup. Here is how this chapter's elements appear in it.
| Markup | Role in the tree | Accessible name comes from |
|---|---|---|
| `input type="email"` | textbox | the associated `label` |
| `input type="checkbox"` | checkbox | the wrapping or associated `label` |
| `fieldset` | group | the `legend` |
| `button type="submit"` | button | its text content |
A complete example
This form combines everything above. It extends the newsletter pattern with a select and a text area, and each control has exactly one label.
<form action="/subscribe" method="post">
<fieldset>
<legend>Newsletter</legend>
<label for="name">Full name</label>
<input id="name" name="name" type="text" autocomplete="name" required>
<label for="email">Email address</label>
<input id="email" name="email" type="email" autocomplete="email" required>
<label for="topic">Favourite topic</label>
<select id="topic" name="topic">
<option value="html">HTML</option>
<option value="css">CSS</option>
<option value="js">JavaScript</option>
</select>
<label for="notes">Anything else?</label>
<textarea id="notes" name="notes" rows="3"></textarea>
<label>
<input type="checkbox" name="weekly"> Weekly digest
</label>
</fieldset>
<button type="submit">Subscribe</button>
</form>Three details are worth noting:
- The
type="email"value gives mobile users a suitable keyboard and gives the browser basic validation. - The
autocompletevalues let browsers and password managers fill the fields. This also helps users with motor or memory difficulties. - The
buttonhas atype. Inside a form, a button with notypedefaults tosubmit, which can surprise you when you add a button that is not meant to submit.
Edge cases
Placeholder is not a label
The placeholder attribute shows hint text inside an empty field. It disappears when the user types, it often has low contrast, and it is not a dependable replacement for a label. Use a visible label, and use the placeholder only for a short example such as [email protected].
Hiding a label visually
Sometimes a design shows no visible label, such as a search box next to a button labelled "Search". Keep the label in the markup and hide it visually with CSS that keeps it available to assistive technology. Do not use display: none, because that removes it from the accessibility tree as well.
<style>
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
}
</style>
<form role="search" action="/search">
<label for="q" class="visually-hidden">Search the site</label>
<input id="q" name="q" type="search">
<button type="submit">Search</button>
</form>Several labels, or hints and errors
A control can have more than one label. Their text is joined to form the name. For help text or error messages, do not put them inside the label. Use aria-describedby, which adds a description that is announced after the name.
<label for="pw">Password</label>
<input id="pw" name="pw" type="password" aria-describedby="pw-hint">
<p id="pw-hint">At least 12 characters.</p>Disabled fieldsets
Adding the disabled attribute to a fieldset disables every control inside it. This is useful for sections that become unavailable. Controls inside the first legend of the fieldset are excepted.
Common errors
- A
forthat matches noid. A typo such asfor="e-mail"againstid="email"silently breaks the link. Clicking the label does nothing, which is a quick test. - Duplicate ids. If two controls share an
id, the label links to the first one only. - Using
aria-labelwhere a visiblelabelwould do. Visible text helps everyone. Reservearia-labelfor cases with no room for visible text. - A
legendthat is not the first child of itsfieldset. The browser may not treat it as the group's name. - A
labelwithout a control. Usinglabelas a styled heading adds a false association. Use a heading or apinstead. - Radio buttons with different
namevalues. They stop behaving as one group, and several can be selected at once. - Putting interactive content inside a
label. A link or button inside a label creates ambiguous click behaviour.
Checking your work
Open devtools and use the Accessibility panel on a control. Look at its computed name and role. In Chrome and Edge, hover over a field with the inspector to see a tooltip that includes its accessible name. In Firefox, the Accessibility panel shows a tree where each field has a name. If the name is empty, the label link is broken. Also try the click test: click each label and confirm that focus moves to its control.
Exercises
Exercise
A form has three radio buttons named "Small", "Medium" and "Large", each with its own label. A screen reader user hears only "Medium, radio button" and does not know what is being chosen. What element would you add, and where?
Show answer
Wrap the three radio buttons in a fieldset and add a legend as its first child, for example "T-shirt size". The legend becomes the accessible name of the group, so the screen reader announces it together with each option.
Exercise
This markup has a bug that makes clicking the label do nothing. Find it and fix it. <label for="user-email">Email</label> <input id="email" name="email" type="email">
Show answer
The for value "user-email" does not match the input's id "email". Change them to match, for example for="email" with id="email". Then clicking the label focuses the input and the input's accessible name becomes "Email".
Exercise
A designer wants a search field with no visible label. How do you keep the field accessible, and why is display: none the wrong way to hide the label?
Show answer
Keep a real label element linked by for and id, and hide it with a visually-hidden CSS technique (absolute position, 1px size, clipped). display: none removes the element from the accessibility tree, so the input would lose its accessible name.