YogeshChauhan.com
Always add associated labels to your Form elements
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

dreamhost

Leave a Reply

Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to set opacity or transparency using CSS? #3 Pagination in CSS with multiple examples #4 How to make HTML form interactive and using CSS? #5 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

Mar 4 How to use data-* Attributes in HTML? Mar 4 The substr() method in JavaScript and how it’s different from substring() Mar 4 A complete guide to add responsive YouTube videos using HTML and CSS Mar 3 How to embed YouTube or other video links in WordPress? Mar 3 How to change the Login Logo in WordPress? Mar 3 substring() Method in JavaScript

You might also like these

PHP arrays for US states’ full names and abbreviationsPHPThe Sort and Compare Functions in JavaScriptJavaScriptHow to Sort (Shuffle) an Array in Random Order in JavaScript?JavaScriptDROP DATABASE (remove a database) from PostgresPostgres3 Ways we can create URLSearchParams Objects in JavaScriptJavaScriptHow to Use the EXISTS and NOT EXISTS Operator with a Subquery in SQL and MySQL?SQL/MySQL