A <label> element should have text content and be associated with a control.

Why is this an issue?

When a label element lacks text content or an associated control, it can lead to several issues:

  1. Poor Accessibility: Screen readers rely on correctly associated labels to describe the function of the form control. If the label is not properly associated with a control, it can make the form difficult or impossible for visually impaired users to understand or interact with.
  2. Confusing User Interface: Labels provide users with clear instructions about what information is required in a form control. Without a properly associated label, users might not understand what input is expected, leading to confusion and potential misuse of the form.
  3. Code Maintainability: Properly structured and labeled code is easier to read, understand, and maintain. When labels are not correctly associated, it can make the code more difficult to navigate and debug, especially for new developers or those unfamiliar with the codebase.

Control elements include:

Exceptions

This rule accepts some server-side and template constructs when they generate the association, the label text, or the control at runtime.

How to fix it

Make sure the <label> has text that describes the control.

An implicit association wraps the control element inside the <label>. An explicit association keeps the label and control separate, and links them with the label’s for attribute and the control’s id. Both approaches are compliant. If you lack a control element, add one.

Code examples

Noncompliant code example

<!-- Non-compliant: label without text -->
<label for="favorite-sport"></label>
<input id="favorite-sport" type="text" />

<!-- Non-compliant: missing "for" attribute on label -->
<label>Favorite food</label>
<input id="favorite-food" type="text" />

Compliant solution

<!-- Explicit association -->
<label for="favorite-sport">Favorite sport</label>
<input id="favorite-sport" type="text" />

<!-- Implicit association -->
<label>
    Favorite food
    <input id="favorite-food" type="text" />
</label>

Resources

Documentation