Add a label for a form control in HTML?

In HTML, the <label> tag is used to define labels for form controls, creating a clickable association between the label text and the form element. When a user clicks on a properly associated label, the browser automatically focuses or activates the corresponding form control.

The <label> tag can be associated with the following form elements −

<input> (all types)
<select>
<textarea>
<meter>
<progress>

Syntax

Following is the basic syntax of the <label> tag −

<label>Label text</label>

The <label> tag supports two key attributes −

  • for − Specifies the id of the form element the label is associated with.

  • form − Specifies which form the label belongs to when multiple forms exist on the page.

Syntax for the for attribute −

<label for="element_id">Label text</label>

Syntax for the form attribute −

<label form="form_id">Label text</label>

Using the for Attribute

The for attribute creates an explicit association between the label and a form control by matching the label's for value with the form element's id.

Example

Following example demonstrates how to use labels with radio buttons −

<!DOCTYPE html>
<html>
<head>
   <title>Label for Attribute</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Select Your Favorite Language</h2>
   <p>Click on the text labels to select radio buttons:</p>
   <form action="/action_page.php">
      <input type="radio" id="html" name="language" value="HTML">
      <label for="html">HTML</label>
      <br><br>
      <input type="radio" id="css" name="language" value="CSS">
      <label for="css">CSS</label>
      <br><br>
      <input type="radio" id="javascript" name="language" value="JavaScript">
      <label for="javascript">JavaScript</label>
      <br><br>
      <input type="submit" value="Submit">
   </form>
</body>
</html>

When you click on the label text (HTML, CSS, or JavaScript), the corresponding radio button gets selected automatically.

Using the form Attribute

The form attribute allows a label to be associated with a form even when the label is located outside the form element.

Example

Following example shows how to use the form attribute to associate a label with a form control from outside the form −

<!DOCTYPE html>
<html>
<head>
   <title>Label form Attribute</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>User Role Selection</h2>
   <p>The "Teacher" label is outside the form but still works:</p>
   <form action="/action_page.php" id="roleForm">
      <input type="radio" id="student" name="person" value="STUDENT">
      <label for="student">Student</label>
      <br><br>
      <input type="radio" id="teacher" name="person" value="TEACHER">
      <br><br>
      <input type="radio" id="admin" name="person" value="ADMIN">
      <label for="admin">Admin</label>
      <br><br>
      <input type="submit" value="Submit">
   </form>
   <br>
   <label form="roleForm" for="teacher">Teacher (outside form)</label>
</body>
</html>

The "Teacher" label is outside the form but still functions correctly due to the form="roleForm" attribute.

Nested Labels

Labels can also wrap around form controls, creating an implicit association without needing the for attribute.

Example

Following example demonstrates nested labels in a login form −

<!DOCTYPE html>
<html>
<head>
   <title>Nested Label Example</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Login Form</h2>
   <form action="/login" method="post">
      <label>
         Email Address:
         <br>
         <input type="email" name="email" size="30" placeholder="Enter your email">
      </label>
      <br><br>
      <label>
         Phone Number:
         <br>
         <input type="tel" name="phone" size="30" maxlength="10" placeholder="Enter 10-digit number" pattern="[0-9]{10}">
      </label>
      <br><br>
      <input type="submit" value="Login">
   </form>
</body>
</html>

In this example, the input fields are nested inside their labels, creating an automatic association without using the for attribute.

Login Form
Email Address:
[Enter your email          ]
Phone Number:
[Enter 10-digit number     ]
[Login]

Labels with Different Input Types

Labels work with various form controls to improve accessibility and user experience.

Example

Following example shows labels with different input types −

<!DOCTYPE html>
<html>
<head>
   <title>Labels with Various Controls</title>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
   <h2>Form with Various Controls</h2>
   <form>
      <label for="username">Username:</label>
      <input type="text" id="username" name="username">
      <br><br>
      
      <label for="birthdate">Birth Date:</label>
      <input type="date" id="birthdate" name="birthdate">
      <br><br>
      
      <label for="country">Country:</label>
      <select id="country" name="country">
         <option value="us">United States</option>
         <option value="uk">United Kingdom</option>
         <option value="ca">Canada</option>
      </select>
      <br><br>
      
      <label for="comments">Comments:</label>
      <br>
      <textarea id="comments" name="comments" rows="4" cols="30"></textarea>
      <br><br>
      
      <input type="submit" value="Submit">
   </form>
</body>
</html>

Each label is properly associated with its corresponding form control, making the form more accessible and user-friendly.

Label Association Methods Explicit (for attribute) <label for="email">Email:</label> <input id="email" type="email"> ? Works anywhere on page ? Clear structure ? Recommended approach Implicit (nested) <label>Email: <input type="email"> </label> ? No ID required ? Compact structure vs

Benefits of Using Labels

Using labels with form controls provides several important benefits −

  • Accessibility − Screen readers can properly announce the purpose of form controls to visually impaired users.

  • Usability − Clicking on the label text activates the associated form control, providing a larger clickable area.

  • Form validation − Browsers can provide better error messages when labels are properly associated with form controls.

Conclusion

The <label> tag is essential for creating accessible and user-friendly forms in HTML. Use the for attribute to explicitly associate labels with form controls, or nest controls within labels for implicit association. Always ensure every form control has a properly associated label to improve both accessibility and user experience.

Updated on: 2026-03-16T21:38:53+05:30

631 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements