Skip to main content

Importance of Labels in Form Accessibility

Provide essential context, helping users understand the purpose of each form control.


Importance of Labels in Form Accessibility

Shawn Sandy (Ally.Cafe) ~


Labels are crucial for making web forms accessible. They provide essential context, helping users understand the purpose of each form control. Without proper labels, users who rely on screen readers or other assistive technologies may struggle to interact with form elements, leading to a frustrating and often unusable experience.

1.1.1 Non-text Content

Level A: All non-text content that is presented to the user has a text alternative that serves the equivalent purpose.

2.4.6 Headings and Labels

Level AA: Headings and labels describe topic or purpose.

3.3.2 Labels or Instructions

Level A: Labels or instructions are provided when content requires user input.

Best Practices for Using Labels

  1. Explicit Labeling Each form control should have an explicit label associated with it. This is typically done using the <label> element.

    Example:

    <label for="username">Username:</label>
    <input type="text" id="username" name="username">
  2. Positioning of Labels Labels should be positioned close to their corresponding form controls, usually directly above or to the left. This helps users understand the relationship between the label and the control.

  3. Using for Attribute The for attribute of the <label> element should match the id of the associated form control.

    Example:

    <label for="email">Email:</label>
    <input type="text" id="email" name="email">
  4. Hidden Labels for Visual Design Sometimes, for design reasons, labels may need to be visually hidden but still accessible to screen readers. This can be achieved using CSS.

    Example:

    <label for="phone" class="visually-hidden">Phone Number:</label>
    <input type="text" id="phone" name="phone">

    CSS:

    .visually-hidden {
      position: absolute;
      width: 1px;
      height: 1px;
      margin: -1px;
      padding: 0;
      overflow: hidden;
      clip: rect(0, 0, 0, 0);
      border: 0;
    }
  5. Associating Labels with ARIA In cases where the standard <label> element is not practical, ARIA properties like aria-label and aria-labelledby can be used to associate labels with controls.

    Example:

    <input type="text" id="city" name="city" aria-label="City">
  6. Providing Instructions and Help Text Additional instructions or help text should be provided when necessary, using aria-describedby to link the input with the help text.

    Example:

    <label for="password">Password:</label>
    <input type="password" id="password" name="password" aria-describedby="passwordHelp">
    <span id="passwordHelp">Your password must be 8-20 characters long.</span>

Common Mistakes to Avoid

  1. Using Placeholder Text as Labels Placeholder text is not a substitute for a proper label. Placeholders can be used to provide examples, but they disappear when the user starts typing, which can be confusing.

  2. Inconsistent Labeling Ensure that all form controls have labels and that the labels are consistently applied across the form.

  3. Lack of Context Avoid using labels that are too generic (e.g., “Field 1”). Labels should be descriptive and provide enough context about the expected input.

Conclusion

Labels are fundamental to the accessibility of web forms. They provide the necessary context for users to understand and interact with form controls. By following best practices and WCAG guidelines, developers can create forms that are both accessible and user-friendly.

For more information on making forms accessible, consider the following resources:

WCAG 2.2 Quick Reference

Using ARIA: Alert Role

W3C Web Accessibility Tutorials - Forms

WebAIM: Accessible Forms

The A11Y Project Checklist - Forms


Found an error, typo, or bug please edit on github or open an issue or ticket