Labels in HTML are used to improve the accessibility of forms by associating text labels with form controls, such as input fields, checkboxes, and radio buttons. This association helps screen readers and other assistive technologies understand the purpose of the form controls, making it easier for users with disabilities to interact with the form.

Key Features of the 'label' Element:

  • Associating a Label with a Form Control: The ‘<label>‘ element is used to bind a text description to a form control.
  • For Attribute: The ‘for‘ attribute of the ‘<label>‘ element specifies which form element the label is bound to by matching its ‘id‘ attribute.
  • Implicit Association: If a label wraps a form control (without using the ‘for‘ attribute), the association is automatic.

Syntax:

				
					<label for="element_id">Label Text</label>
<input type="text" id="element_id" name="element_name">

				
			

Examples:

1. Using the for Attribute

				
					<form>
    <label for="username">Username:</label>
    <input type="text" id="username" name="username">
</form>

				
			
  • Explanation:
    • The <label> element has a for attribute set to “username”.
    • This attribute matches the id of the <input> element.
    • When the user clicks on the text “Username:”, the corresponding input field is focused.

2. Implicit Association (Wrapping the Control)

				
					<form>
    <label>
        Password:
        <input type="password" name="password">
    </label>
</form>

				
			
  • Explanation:
    • The <input> element is placed inside the <label> element.
    • This automatically associates the label with the input.
    • Clicking the “Password:” text will focus the password input.

3. Labeling a Checkbox

				
					<form>
    <label for="subscribe">
        <input type="checkbox" id="subscribe" name="subscribe">
        Subscribe to newsletter
    </label>
</form>

				
			
  • Explanation:
    • The checkbox is wrapped within the <label>.
    • Clicking on “Subscribe to newsletter” will check/uncheck the checkbox.

4. Labeling Multiple Radio Buttons

				
					<form>
    <p>Choose your favorite color:</p>
    <label for="color_red">Red</label>
    <input type="radio" id="color_red" name="color" value="red">
    
    <label for="color_green">Green</label>
    <input type="radio" id="color_green" name="color" value="green">
    
    <label for="color_blue">Blue</label>
    <input type="radio" id="color_blue" name="color" value="blue">
</form>

				
			
  • Explanation:
    • Each radio button has its own label.
    • The for attribute connects each label to its corresponding radio button.

Importance of Using Labels:

  • Accessibility: Labels make forms more accessible by providing clear text associations with form controls. Screen readers rely on these labels to convey what each form control does.
  • Usability: Clicking on a label that is associated with a form control (like a checkbox or radio button) will activate the control, making the form easier to use.
  • SEO: Properly labeled forms can improve the semantic structure of your HTML, which can indirectly impact search engine optimization
Scroll to Top