-3

I've seen code like:

<label for="username">Username:</label><br/>

What is the <label> tag being used for?

Grokify
  • 15,092
  • 6
  • 60
  • 81

3 Answers3

0

The html <label> tag creates a label for an <input> element. When the <label> element is clicked, it toggles the control for the input element. For example, the label for an <input> element with type="text" will create focus on the <input> element when clicked. For the label to work, its for attribute must equal the id of the <input> element it is labeling.

For more information, read this: https://www.w3schools.com/tags/tag_label.asp

Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

In a form, you generally have input controls like text boxes, checkboxes, radio buttons, select elements, etc. These often have display text to indicate what is being input. The <label> tag links this the display text to the input control so clicking on the display name will activate an onclick for the form control.

The MDN link provided by Felix King has a lot of info, but this is the relevant bit for why these exist and are used:

When a <label> is clicked or tapped, and it is associated with a form control, the resulting click event is also raised for the associated control.

Grokify
  • 15,092
  • 6
  • 60
  • 81
0

The <label> tag allows you to label and describe an <input> element in a form. The for attribute allows you to specify the id of the <input> element it describes.

Ben Soyka
  • 816
  • 10
  • 25