Yogesh Chauhan's Blog

Always add associated labels to your Form elements

in UI/UX on December 3, 2020

If you try to run an audit check from Google or any other services, you might get an accessibility error saying that, “Form elements do not have associated labels”.

Labels ensure that form controls are announced properly by assistive technologies like screen readers.

Assistive technology users rely on these labels to navigate forms.

Mouse and touchscreen users also benefit from labels because the label text makes a larger click target.

How to add labels to form elements

There are two ways to associate a label with a form element. Either place the input element inside a label element:


<label>
   Receive promotional offers?
  <input type="checkbox">
</label>

Or use the label’s for attribute and refer to the element’s ID:


<input id="promo" type="checkbox">
<label for="promo">Receive promotional offers?</label>

When the checkbox has been labeled correctly, assistive technologies report that the element has a role of checkbox, is in a checked state, and is named “Receive promotional offers?”

HTML label Tag

The label tag defines a label for several elements:


<input type="color">
<input type="date">
<input type="datetime-local">
<input type="email">
<input type="file">
<input type="month">
<input type="number">
<input type="password">
<input type="radio">
<input type="range">
<input type="search">
<input type="tel">
<input type="text">
<input type="time">
<input type="url">
<meter>

Screen reader users will read out loud the label, when the user is focused on the element. So that users who have difficulty clicking on very small regions (such as checkboxes) – because when a user clicks the text within the label element, it toggles the input (this increases the hit area). 

The for attribute of the label tag should be equal to the id attribute of the related element to bind them together. 

Credit: Web Dev


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to add navigation menu in Angular 9 app?AngularHow to display random posts in WordPress?WordPressWhat is the best way to add JavaScript Code into HTML?HTMLHow to embed YouTube or other video links in WordPress?WordPressAdd animated hamburgers menu using Hamburgers collection on GitHubCSSadd_filter function in WordPressWordPress