HTML for Attribute

Beginner
⏱️ 5 min read
📚 Updated: Jun 2026
🎯 3 Examples
Forms & Accessibility

What You’ll Learn

The for attribute connects a <label> to a form control by matching that control’s id. Users can click the label text to focus the field or toggle a checkbox, and screen readers announce the connection clearly.

01

Label Element

Primary use case.

02

Matches id

for equals id value.

03

Click Target

Bigger tap area.

04

Checkboxes

Toggle on label click.

05

htmlFor

JS property name.

06

Implicit Label

Wrap input option.

Purpose of for

The primary purpose of the for attribute is to establish an explicit relationship between a label and a form control. When the values match, the browser treats the label as belonging to that control.

This improves accessibility because assistive technologies read the label when the control receives focus. It also improves usability: clicking label text focuses text inputs or toggles checkboxes and radio buttons, giving users a larger click target.

💡
for must match a unique id

The value of for must exactly equal the id of one labelable element on the page. Duplicate ids break the association.

📝 Syntax

Add for to a <label> and set the linked control’s id to the same value:

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

Syntax Rules

  • Used on <label> (and <output> for different purposes).
  • Value is the id of a labelable form-associated element.
  • The referenced id must be unique within the document.
  • Alternative: wrap the control inside the label without using for.

💎 Values

The for attribute takes a single string: the id of the associated control.

  • Text inputsfor="email" with <input id="email">
  • Textareasfor="message" with <textarea id="message">
  • Select menusfor="country" with <select id="country">
  • Checkboxes & radios — clicking the label toggles or selects the input
for-values.html
<label for="subscribe">Subscribe to newsletter</label>
<input type="checkbox" id="subscribe" name="subscribe">

⚡ Quick Reference

ItemDetailsNotes
Element<label>Primary use
ValueControl idCase-sensitive match
Click behaviorFocus or toggle controlLarger hit target
JS propertylabel.htmlForNot for in JS
AlternativeWrap input in labelImplicit association
Also on<output>References related elements

Applicable Elements

ElementSupported?Notes
<label>YesLinks label text to one control
<output>YesLists ids of elements that label the output
<input>, <select>NoControls use id; labels use for

Explicit for vs Implicit Label

ApproachMarkupWhen to Use
Explicit (for)<label for="x"> + <input id="x">Label and input are separate in layout
Implicit (wrapped)<label>Text <input></label>Simple forms; no id required

Examples Gallery

Basic label association, a login form with multiple labels, and dynamic htmlFor in JavaScript.

Example — Basic Label Association

Match for on the label with id on the input:

label-for.html
<label for="username">Username:</label>
<input type="text" id="username" name="username">
Try It Yourself

How It Works

The for attribute value username matches the input’s id. The browser connects them so the label activates the control.

Example — Login Form

Each field gets its own label with a matching for and id pair:

login-labels.html
<form>
  <label for="email">Email:</label>
  <input type="email" id="email" name="email" required>

  <label for="password">Password:</label>
  <input type="password" id="password" name="password" required>

  <input type="submit" value="Submit">
</form>
Try It Yourself

How It Works

Every visible field has a programmatic label. Screen readers announce “Email” when the email field is focused, and users can click either label to jump to its field.

Dynamic Values with JavaScript

Update the association at runtime with the htmlFor property:

dynamic-for.html
<label id="dynamicLabel" for="fieldA">Field A:</label>

<script>
  document.getElementById("dynamicLabel").htmlFor = "fieldB";
</script>
Try It Yourself

How It Works

Setting htmlFor re-points the label to a different control. Useful when UI layout changes dynamically or when reusing one label element in a wizard step.

♿ Accessibility

  • Every input needs a label — Use for + id or wrap the control inside <label>.
  • Do not rely on placeholder alone — Placeholders disappear when typing and are not a substitute for labels.
  • Checkboxes and radios benefit most — Small controls are hard to tap; labels enlarge the target.
  • Keep ids unique — Duplicate ids break label association and confuse assistive tech.

🧠 How for Works

1

Author sets for and id

Label for value matches control id.

Markup
2

Browser links label to control

Accessibility tree records the relationship.

A11y
3

User clicks label text

Control receives focus or toggles state.

Interaction
=

Inclusive, easier forms

Clear names for fields and larger click targets for everyone.

Browser Support

The for attribute on <label> is supported in all modern browsers. It has been a core HTML feature for decades.

HTML4+ · Fully supported

Universal label association

All major browsers honor for on label elements.

100% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
for attribute 100% supported

Bottom line: Always associate labels with controls using for or implicit wrapping.

💡 Best Practices

✅ Do

  • Match for exactly to a unique id on the control
  • Label every input, select, and textarea
  • Use for when label and input are in separate layout cells
  • Use htmlFor in JavaScript, not for
  • Test by clicking label text to confirm focus

❌ Don’t

  • Reuse the same id on multiple elements
  • Leave inputs without an accessible name
  • Point for at a non-existent id
  • Rely on placeholder text instead of labels
  • Put for on input elements instead of labels

Conclusion

The for attribute is essential for accessible HTML forms. It links label text to form controls, improves keyboard and screen reader support, and gives users a larger area to click or tap.

Pair every for with a matching id, or wrap controls inside labels when layout allows. Use htmlFor in JavaScript when you need to change the association dynamically.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about for

Bookmark these before building your next form.

5
Core concepts
🔗 02

Matches id

Exact string match.

Value
👆 03

Click label

Focus or toggle.

UX
⚙️ 04

htmlFor

JS property name.

Script
05

Wrap option

Implicit labels.

A11y

❓ Frequently Asked Questions

It associates a label with a form control by matching the control’s id. Clicking the label focuses or activates that control.
Primarily <label>. The <output> element also has a for attribute with a different purpose (listing related element ids).
The id of the linked control, such as for="email" with <input id="email">.
Use labelElement.htmlFor = "newId". The property name is htmlFor, not for.
Yes. Nesting the control inside <label> creates an implicit association without for.
Yes. Label association with for works in all modern and legacy browsers.

Build accessible labeled forms

Practice linking labels to inputs with for and matching id values in the Try It editor.

Try basic label example →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

3 people found this page helpful