How to Connect a label to a Form Field with the for Attribute in HTML
<label for="email"> connects a label to the form field whose id is email, wherever the two are on the page. Clicking the label then puts the cursor in the field, and screen readers announce the field by the label's text.
Syntax
<label for="<id>"><text></label>
<input id="<id>">
| Part | What it does |
|---|---|
for="<id>" | The id of the field this label belongs to. It must match the id exactly, capital letters included. |
<text> | The label's text. |
id="<id>" | The field's id, the same value as in for. |
MDN's label page calls this an explicit association, and MDN's for attribute page describes the attribute.
The Example
The example form has the label Email address and a text box whose id is email, connected by for="email".
Step 1: Write the Code
Create a file named index.html in a text editor, such as Visual Studio Code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Label for</title>
</head>
<body>
<form>
<label for="email">Email address</label>
<input id="email" name="email">
</form>
</body>
</html>
Step 2: Click the Label
Double-click index.html to open it in your browser. Click the words Email address: the cursor jumps into the text box, so you can type straight away. Type ana@example.com:

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.
What Happened
- Clicking the label focused the text box: Chromium reported the element with the
idemailas the focused one. - The label's text became the field's name. In Chromium's accessibility tree, the information screen readers use, the text box was named Email address.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
A for that differs from the id in capital letters, such as for="Email" for id="email" | Nothing is connected: clicking the label left the focus on the page, and the text box had no name in the accessibility tree | Make for exactly the same as the id |
Putting the field's name in for | for only looks at id: with for="mail" and a text box with id="email" and name="mail", clicking the label did nothing | Use the id in for |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| Clicking the label doesn't put the cursor in the field | for and id don't match | Copy the id into for |
| A screen reader doesn't announce the field's label | The label isn't connected | Match for to the field's id |
FAQ
Do I need for when the field is inside the label?
No. A field inside its label is connected already. for is for a label that sits somewhere else on the page.
How do I read for in JavaScript?
As htmlFor, because for is a reserved word in JavaScript. For the example's label, document.querySelector('label').htmlFor was 'email'.
Can the label come after the field?
Yes. for connects them wherever they are on the page: with the label after the text box, clicking it still put the cursor in the box, and the box was still named Email address.
Conclusion
for on a label and id on a field, with exactly the same value, connect the two: the label becomes clickable, and it names the field for screen readers. If clicking the label does nothing, compare the two values letter by letter.
Additional Resources
- label: The Label element, on MDN.
- HTML attribute: for, on MDN.
