DEV Community

Cover image for A11y tips: don't use links, buttons or headings inside labels
Carlos Espada
Carlos Espada

Posted on • Edited on

5

A11y tips: don't use links, buttons or headings inside labels

A label should only contain text, do not put interactive elements such as links and buttons inside, since:

  • It is more complicated to activate the form control associated with <label>.
  • Screen readers will have problems announcing the content of <label> (they only read the part that does not contain text).

DON'T

<label for="tac">
  <input
    type="checkbox"
    id="tac"
    name="tac"
  />
  I agree with the <a href="terms-and-conditions.html">Terms and Conditions</a>
</label>
Enter fullscreen mode Exit fullscreen mode

The best option is to take the interactive element out of <label> and relate it to the form control using the aria-describedby attribute.

DO

<label for="tac">
  <input
    type="checkbox"
    id="tac"
    name="tac"
    aria-describedby="tac-desc"
  />
  I agree with the Terms and Conditions
</label>
<p id="tac-desc">
  Please read <a href="terms-and-conditions.html">our Terms and Conditions</a>
</p>
Enter fullscreen mode Exit fullscreen mode

Also, do not use headings within a label as many assistive technologies have trouble advertising them and are the most frequent navigation resource for their users.

DON'T

<label for="your-name">
  <h3>Your name</h3>
  <input
    id="your-name"
    name="your-name"
    type="text"
  />
</label>
Enter fullscreen mode Exit fullscreen mode

If the label text needs visual adjustments to make it look like a heading, they have to be done using CSS.

DO

<label for="your-name" class="large-label">
  Your name
  <input
    id="your-name"
    name="your-name"
    type="text"
  />
</label>
Enter fullscreen mode Exit fullscreen mode

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more