What the label element does
<label> gives a form field its name. Connect it with for, set to the field's id, and two things happen: screen readers announce the label when the field is focused, and clicking the label text focuses the field. Click both texts below:
Clicking "City" puts the cursor in its field. Clicking "Country" does nothing, and a screen reader focusing that second field hears only "edit text", with no name. The two look identical; only one is labelled.
Two ways to connect a label
Use for and id, or put the field inside the label:
for and id | Wrapping | |
|---|---|---|
Needs an id | Yes, unique on the page | No |
| Label and field can be apart | Yes (a table cell, a grid) | No, the field is inside |
| Easy to break | By a typo or a duplicate id | By putting two fields in one label |
for must match the field's id, not its name. The two are often the same word, which hides the mistake until someone changes one. Doing both (wrapping and for) is fine and common.
A label can be connected to <input> (except type="hidden"), <select>, <textarea>, <button>, <meter>, <output> and <progress>. One label names one field. A field can have more than one label, though one clear label is easier for everyone.
A bigger click target for checkboxes and radios
For small controls the label matters most: clicking anywhere on it toggles the box, so the target is the whole line of text, not a 13-pixel square. Styling the label can make the target even larger:
Click the padding of each row, away from the box. The first row toggles; the second does nothing unless you hit the box itself. More on checkboxes in HTML checkbox.
Hints and error messages
A label should be short: the name of the field. Extra instructions and error messages go in separate elements, connected with aria-describedby, so screen readers read the label first and the description after it:
Press Create with the field empty, then with AB, then with ada99. With aria-describedby, a screen reader announces something like "Username, required, edit text, 3 to 16 lowercase letters or digits, Enter a username." The asterisk is aria-hidden because the required attribute already says "required"; hearing "star" adds nothing.
Hidden labels
Some fields have a visible cue other than a text label, such as a search box next to a Search button. The label is still needed; hide it visually but keep it in the page for screen readers:
Do not hide it with display: none or visibility: hidden: those hide it from screen readers too, and not every browser and screen reader still uses a hidden label as the name. The other option is aria-label="Search courses" on the input, which names it without any label element. A visible label is still the best choice whenever there is room: everyone benefits from seeing what a field is for.
Common mistakes
forpointing at thenameinstead of theid. Nothing connects.- Duplicate ids.
forconnects to the first element with that id, which may be a field in another form on the page. - Placeholder instead of a label. It vanishes on typing and is not a reliable name.
- Two fields inside one label. A label names one field; the second is left unlabelled.
- A link inside a checkbox's label, such as "I accept the terms" with "terms" as a link. Part of the line toggles the box and part opens a page, which is easy to hit by mistake. Put the link outside the label.
- Hiding the label with
display: none. Use a visually-hidden class. - Text next to a field that is not a
<label>. It looks the same and does nothing. For the form around your fields, see HTML forms.
Frequently Asked Questions
What does the label tag do in HTML?
It names a form field. Screen readers read the label when the field is focused, and clicking the label focuses the field, or toggles it for a checkbox or radio button.
What is the for attribute in a label?
for holds the id of the field the label belongs to: <label for="email">Email</label> <input id="email">. It must match the id exactly, not the name.
Should I put the input inside the label or use for?
Both work in current browsers and screen readers. Wrapping needs no ids; for and id let the label and the field sit in different places in the markup, such as separate table cells. Many teams wrap and add for as well.
Can a placeholder replace a label?
No. The placeholder disappears as soon as the reader types, is often low contrast, and is not reliably announced as the field's name. Use a visible <label> and keep the placeholder for an example value, if at all.
How do I hide a label but keep it for screen readers?
Keep the <label> and hide it visually with a class that shrinks it to 1 pixel and clips it, rather than display: none, which hides it from screen readers too. For a field with no room for any label, aria-label on the field is the alternative.