Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use <label>?

Tags:

html

From a presentation perspective, if I write a text between a <label> tag it looks identical as to if I hadn't.

So, why do we use this tag at all?

like image 878
Simplicity Avatar asked Oct 03 '11 14:10

Simplicity


People also ask

What is a benefit of using the label element in HTML?

The <label> tag in HTML is used to provide a usability improvement for mouse users i.e, if a user clicks on the text within the <label> element, it toggles the control. The <label> tag defines the label for <button>, <input>, <meter>, <output>, <progress>, <select>, or <textarea> element.

What mean label in HTML?

The <label> tag is used to define a caption for a form control element in an HTML form. Each label is associated with one specific element in a form.


3 Answers

HTML is not about presentation. It is a way of describing data. If you have some text that represents a label for an input, you wrap it in label tags not for presentation but because that's what it is. Without the label tag, that text is almost meaningless. With the label tag and its for attribute (or not*) you are providing meaning and structure and forming a relationship between your markup that can be better understood by computers/parsers/browsers/people.

* you don't necessarily need the for if you wrap the label around the input:

<label>My input
  <input type="text" id="my-input" />
</label>
like image 177
punkrockbuddyholly Avatar answered Oct 13 '22 06:10

punkrockbuddyholly


The for attribute of a label element corresponds to the id attribute of an input element. If you click the label, it puts focus on the input box.

Example:

<input type="checkbox" id="agree" /> 
<label for="agree">I agree with the Terms and Conditions</label>

See this in action.

If you click on the text, it checks the box.

like image 59
Peter Olson Avatar answered Oct 13 '22 05:10

Peter Olson


When you click on the label, the focus goes to the related input. Very handy for checkboxes when it is hard to hit the small rectangle.

like image 6
Mārtiņš Briedis Avatar answered Oct 13 '22 06:10

Mārtiņš Briedis