HTML <label>
Tag
The HTML <label>
tag is used to define a label for an <input>
element. The label provides a user-friendly name for the input field and helps improve accessibility for users using screen readers or other assistive technologies.
Example:
<label for="username">Username:</label>
<input type="text" id="username">
In the example above, the <label>
tag is used to create a label for the input field with the id "username". When the user clicks on the label, the input field is focused.
Attributes of the <label>
tag:
Attribute | Description |
---|---|
for | Specifies which input element the label is associated with. The value of this attribute should be the id of the input element. |
form | Specifies one or more forms the label belongs to. This can be used if the label is not a descendant of the input element. |
accesskey | Specifies a keyboard shortcut to activate or add focus to the element. |
onfocus | Specifies a script to run when the element receives focus. |
style | Specifies CSS styles for the label. |
It is important to note that the <label>
tag should always have a "for" attribute that matches the id of the input field it is associating with, in order for it to work correctly.
Summary:
- The
<label>
tag is used to create a label for an input field. - It improves accessibility for users using screen readers or other assistive technologies.
- The label should have a "for" attribute that matches the id of the input field.