HTML

Chapter 22: Label Element

AttributesDescription
forReference to the target ID Element. I.e: for=”surname”
formHTML5, [Obsolete] Reference to the form containing the Target Element. Label elements are expected within a <form> Element. If the form=”someFormId” is provided this allows you to place the Label anywhere in the document.

About Label

The <label> element is used to reference a form action element. In the scope of User Interface it’s used to ease the target / selection of elements like Type radio or checkbox.

<label> as wrapper

It can enclose the desired action element

<label>
 <input type="checkbox" name="Cats">
 I like Cats!
</label>

(Clicking on the text the target input will toggle it’s state / value)

<label>as reference

Using the for attribute you don’t have to place the control element as descendant of label – but the for value must
match it’s ID

<input id="cats" type="checkbox" name="Cats">
<label for="cats" >I like Cats!</label>

Note

Don’t use more than one Control Element within a <label> element

Basic Use

Simple form with labels…

<form action="/login" method="POST">
 
 <label for="username">Username:</label>
 <input id="username" type="text" name="username" />
 <label for="pass">Password:</label>
 <input id="pass" type="password" name="pass" />
 <input type="submit" name="submit" />
</form>
Version ≥ 5
<form id="my-form" action="/login" method="POST">
 
 <input id="username" type="text" name="username" />GoalKicker.com – HTML5 Notes for Professionals 66
 <label for="pass">Password:</label>
 <input id="pass" type="password" name="pass" />
 <input type="submit" name="submit" />
</form>
<label for="username" form="my-form">Username:</label>

Recent Posts

Unlocking the Secrets of JSON.stringify(): More Than Meets the Eye

JSON (JavaScript Object Notation) is a lightweight data-interchange format widely used in web development. At…

2 months ago

How to Handle AJAX GET/POST Requests in WordPress

AJAX (Asynchronous JavaScript and XML) is a powerful technique used in modern web development that…

3 months ago

Page Speed Optimization: Post-Optimization Dos and Don’ts

Introduction After successfully optimizing your website for speed, it's essential to maintain and build upon…

3 months ago

Ultimate Guide to Securing WordPress Folders: Protect Your Site from Unauthorized Access

Securing your WordPress folders is crucial to safeguarding your website from unauthorized access and potential…

4 months ago

HTML CSS PHP File Upload With Circle Progress Bar

Creating a file upload feature with a circular progress bar involves multiple steps. You'll need…

4 months ago

Using WP Rocket with AWS CloudFront CDN

Integrating WP Rocket with AWS CloudFront CDN helps to optimize and deliver your website content…

4 months ago