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

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

Advertisement
<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…

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

Securing phpMyAdmin Like a Pro: Essential Tips and Tricks

Securing phpMyAdmin is crucial to prevent unauthorized access and protect your databases. Here's a guide…

5 months ago

Pasqal raises $100M to build a neutral atom-based quantum computer

Pasqal, a Paris-based quantum computing startup, today announced that it has raised a $100 million…

1 year ago

Apple in talks with Disney, others on VR content for new headset: Report

Developed with Sony Group Corp, the headset will have two ultra-high-resolution displays to handle the…

1 year ago

Microsoft, Amazon results to highlight softening cloud business

After years of blistering growth, most recently fuelled by remote working and studying during the…

1 year ago

Intel chairman Omar Ishrak steps down

Omar Ishrak had stepped down and the chipmaker appointed board director Frank Yeary as his…

1 year ago

Canada to commercialise world's first photonic-based quantum computer

Canadian Prime Minister Justin Trudeau has announced a new federal investment to build and commercialise…

1 year ago