Course contentsShow
← Web Development
Lesson 102 of 2,9822. HTML FundamentalsFree lesson

The <label> Element and for Attribute

Associating labels with inputs for accessibility and clickable label areas.

The <label> Element and for Attribute

What you'll learn: How to connect text descriptions to form inputs, making your forms accessible and easier to use.

What Is a Label?

A <label> element provides a descriptive text label for a form control. Think of it like a name tag at a conference—it tells users what each input field is for. Without labels, users stare at empty boxes wondering what to type.

The Power of the for Attribute

The magic happens when you connect a label to its input using the for attribute. The for attribute's value must match the input's id:

<label for="username">Username:</label>
<input type="text" id="username" name="username">

When properly connected, clicking the label text itself focuses or activates the input. This is like having a bigger target to aim for—especially helpful on mobile devices where tapping small checkboxes or radio buttons is frustrating.

Two Ways to Associate Labels

Explicit association (shown above): Label and input are separate, connected by for and id.

Implicit association: Wrap the input inside the label:

<label>
  Email Address:
  <input type="email" name="email">
</label>

Both work, but explicit association is generally preferred for flexibility and clarity.

Why This Matters for Accessibility

Screen readers rely on labels to announce what each input field is for. Without a properly associated label, visually impaired users have no idea what information to enter. The label isn't just visual—it's structural information about your form.

Key Takeaway: Always associate labels with inputs using the for attribute and matching id—it makes your forms clickable, accessible, and user-friendly for everyone.