YogeshChauhan . com

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:

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

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">

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 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 How to Use SQL MAX() Function with Dates? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

Jun 16 What are Stored Procedures for SQL Server? Jun 16 What are Class Constants in PHP? Jun 15 A short basic guide on states in React Jun 15 How to define constants in PHP? Jun 15 How to define visibility for a property in PHP? Jun 15 How to use @if and @else in SCSS?

You might also like these

Solution to pod install fails with json error on Mac OS X 10.15 (or Mac OS Catalina)MiscRelative Length Units in CSSCSSWhat is Host Hardening and What are some Important Hardening Steps?MiscStyling Lists with CSSCSS@use rule in SCSS (Sass)SCSSSQL Left JoinSQL/MySQL