Skip to content
Velloreth
Web Development 6 min read

Form Accessibility: Labels, Errors and Keyboard Support

A form is the most common interaction and the most common accessibility failure. Learn the labels, the errors, the focus and the ARIA.

By

The label

Every input needs a programmatically associated label.

<label for="email">Email</label>
<input id="email" type="email" name="email">

Or wrap the input.

<label>
  Email
  <input type="email" name="email">
</label>

The for and id approach is the most reliable.

A click on the label focuses the input, which is a usability benefit for everyone.

The placeholder

A placeholder is not a label.

Why:

  • It disappears when the user types
  • It has a low contrast by default
  • It is not reliably announced
  • It is often used for a hint, which is a different thing

Use a visible label, and a placeholder only as an example of the format.

<label for="phone">Phone</label>
<input id="phone" type="tel" placeholder="+977 98XXXXXXXX">

The error

An error must be announced, not only coloured.

<label for="email">Email</label>
<input id="email" type="email" aria-invalid="true" aria-describedby="email-error">
<p id="email-error" role="alert">Enter a valid email address.</p>

The three parts:

PartPurpose
The error text in the DOMit is read
aria-describedbyit is associated with the input
aria-invalidthe state is announced

Do not rely on a red border alone. A colour-blind user cannot see it.

Move the focus to the first invalid field, or to an error summary at the top.

The fieldset

A group of related controls needs a name.

<fieldset>
  <legend>Notification preference</legend>
  <label><input type="radio" name="notify" value="email"> Email</label>
  <label><input type="radio" name="notify" value="sms"> SMS</label>
</fieldset>

Without the fieldset, a screen reader announces each option with no context.

Use it for a radio group and a set of related checkboxes.

The keyboard

Every control must be reachable and operable by keyboard.

KeyAction
Tabmove to the next control
Shift+Tabmove to the previous control
Entersubmit, or activate a button
Spacetoggle a checkbox, activate a button
Arrow keysmove within a radio group or a select

The focus order must follow the visual order. A positive tabindex breaks it.

A custom control must handle the keys itself.

The custom control

Use a native element if one exists.

<!-- Correct -->
<button type="button">Save</button>

<!-- Wrong: not focusable, no keyboard -->
<div class="btn" onclick="save()">Save</div>

A div with a click handler is not a button, because it is not focusable and does not respond to Enter or Space.

If a native element does not exist, add the role, the keyboard handling, the focus management and the state attributes, and test with a screen reader.

The touch target

WCAG 2.2 requires at least 24 by 24 CSS pixels.

44 by 44 is the common recommendation for a touch interface.

A small target is hard to hit, which is an accessibility problem for a user with a motor impairment.

The fix: padding, a larger hit area, or a spacing between the targets.

The autocomplete

The autocomplete attribute helps everyone.

<input id="email" type="email" autocomplete="email">
<input id="name" type="text" autocomplete="name">
<input id="cc" type="text" autocomplete="cc-number">

It fills the field, which is a benefit for a user with a motor or cognitive impairment.

It is also a security consideration, because a browser may fill a field the user did not intend.

The required field

Mark it in the label and with the attribute.

<label for="name">Name <span aria-hidden="true">*</span></label>
<input id="name" required aria-required="true">

Explain the marker at the top of the form, or use the word “required”.

A colour-only marker is not accessible.

The form as a whole

A form is more than a set of fields, and the structure around them matters as much as the fields themselves. The first element is a heading, so a screen reader user can navigate to the form and know what it is for. The second is a description, associated with the form, that explains what happens on submit. The third is the submit button, which must be a real button and must say what it does: “Create account” rather than “Submit”. The fourth is the order, which must follow the visual order, because a screen reader reads the DOM order and a keyboard user tabs through it. The fifth is the confirmation, which must be announced, because a message that appears visually and is not in a live region is invisible to a screen reader user. A form that is correct field by field can still fail as a whole if the structure is missing.

Testing a form

A form is tested with a keyboard, a screen reader and a zoom, and each finds a different class of problem. The keyboard test is the fastest: tab through the form and check that every control is reachable, that the focus is visible, and that the order matches the visual order. The screen reader test finds the missing labels and the unannounced errors, because a field with no label is announced as an edit box with no name. The zoom test at 200 percent finds the layout problems, because a form that overflows horizontally is unusable for a user who zooms. The fourth test is the error path: submit an invalid form and check that the error is announced, that the focus moves to the first invalid field, and that the message says what to do rather than only that something is wrong. Those four tests take a few minutes and catch the large majority of the failures.

Common mistakes

A placeholder as a label. The field has no name once filled.

No for and id. The label is not associated.

A colour-only error. A colour-blind user cannot see it.

No aria-describedby. The error is not associated.

No aria-invalid. The state is not announced.

A div as a button. Not focusable, no keyboard.

A positive tabindex. The focus order is broken.

No fieldset for a radio group. No context.

A small touch target. Hard to hit.

No autocomplete. A missed benefit.

A required marker that is colour-only. Not accessible.

A quotable summary

Every input needs a programmatically associated label, and a placeholder is not a label because it disappears when the user types. An error must be in the DOM, associated with aria-describedby and marked with aria-invalid, not only coloured, and a group of related controls needs a fieldset and a legend. Use a native element before an ARIA role, keep the focus order matching the visual order, and make the touch target at least 24 by 24 CSS pixels.

For the related topics, see aria-labels-explained and color-contrast-guide. For the broader context, form-validation-patterns and css-grid-complete-guide.

Frequently asked questions

Why is a placeholder not a label?
Because it disappears when the user types, so the field has no visible name once it is filled. It also has a low contrast by default and is not reliably announced. Use a visible label, and a placeholder only as an example of the expected format.
How do I associate a label with an input?
Use the for attribute on the label with the id of the input, or wrap the input inside the label. The for and id approach is the most reliable, and it works with a screen reader and a click on the label.
How do I announce an error?
Add the error text to the DOM, associate it with the input with aria-describedby, and set aria-invalid to true. Do not rely on a red border alone, because a colour-blind user cannot see it. Move the focus to the first invalid field, or to an error summary.
What is a fieldset and a legend?
A fieldset groups related controls, and a legend names the group. They are used for a radio group or a set of related checkboxes, so a screen reader announces the group name with each option. Without them, the options are announced with no context.
How do I make a custom control accessible?
Use a native element if one exists. If not, add the role, the keyboard handling, the focus management and the state attributes, and test with a screen reader. A div with a click handler is not a button, because it is not focusable and does not respond to Enter or Space.
What is the minimum touch target size?
WCAG 2.2 requires a target of at least 24 by 24 CSS pixels, and 44 by 44 is the common recommendation for a touch interface. A small target is hard to hit, which is an accessibility problem for a user with a motor impairment.