The <label> HTML element represents a caption for a form element in a user interface. It improves accessibility by linking text to form elements. When a user clicks on the label, it automatically focuses on or activates the associated input, such as text fields, checkboxes, or radio buttons. This helps make forms more user-friendly and easier to navigate.
The <label> tag can be used in two ways:
- Using the for attribute: The label is connected to an input field by using the for attribute, which matches the id of the input.
- Wrapping the input inside the label: The input element can also be placed directly inside the label, where no for or id attributes are needed.
HTML Inputs and Labels
In HTML, the <label> tag works hand-in-hand with input elements. It allows users to click on the label, which then selects or focuses on the associated input field. This connection between inputs and labels enhances both usability and accessibility for forms, making it easier to interact with them.
Supported Tags
The <label> tag can be defined with the following Tags:
Syntax
<label> form content... </label>
Attribute Value
Attribute Value | Descriptions |
|---|
for | It refers to the input control that this label is for. Its value must be the same as the value of the input control's "id" attribute. |
form | It refers to the form to which the label belongs to. |
HTML <label> Tag Examples
Here are some examples of how labeling can be used:
Example 1: Using the <label> Tag with the For Attribute
This example illustrates the basic usage of the <label> tag in HTML. Here, we will use the <input> tag outside of the <label> tag.
html
<!DOCTYPE html>
<html>
<head>
<title>
HTML label Tag
</title>
</head>
<body>
<strong>HTML label Tag</strong>
<br><br>
<form>
<!-- Starts label tags from here -->
<label for="student">
Student
</label>
<input type="radio"
name="Occupation"
id="student"
value="student"><br>
<label for="business">
Business
</label>
<input type="radio"
name="Occupation"
id="business"
value="business"><br>
<label for="other">
Other
</label>
<!-- Ends label tags here -->
<input type="radio"
name="Occupation"
id="other"
value="other">
</form>
</body>
</html>
Output:
HTML label Tag Example OutputExample 2: Using the <input> Tag Inside the <label> Tag
In this example, we will demonstrate how to use the <input> tag inside the <label> tag.
html
<!DOCTYPE html>
<html>
<body>
<strong> HTML label Tag</strong>
<br><br>
<form>
<!-- label tag starts from here -->
<label>
Male
<input type="radio"
name="gender"
id="male"
value="male" />
</label><br />
<label>
Female
<input type="radio"
name="gender"
id="female"
value="female" />
</label><br />
<label>
Other
<input type="radio"
name="gender"
id="other"
value="other" />
</label>
<!-- label tag ends from here -->
</form>
</body>
</html>
Output:
HTML label Tag Example OutputSupported Browsers
Here are the browsers that support the <label> tag:
Note: Internet Explorer is not supported.
Explore
HTML Basics
Structure & Elements
Lists
Visuals & Media
Layouts & Designs
Projects & Advanced Topics
Tutorial References