Skip to main content

How to Label a Form Field in HTML by Wrapping It in a label

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Wrap a form field and the words that describe it in one <label>, and the two belong together: clicking the words works like clicking the field, and screen readers announce the field by those words.

Syntax​

Syntax
<label><field> <text></label>
<label><text> <field></label>
PartWhat it does
<label> … </label>The caption of the one form field inside it.
<field>A form field, such as <input type="checkbox"> for a checkbox or <input> for a text box.
<text>The words that describe the field. They can come before or after it.

MDN's label page calls this an implicit association: the field is inside the label, so it needs no id.

The Example​

The example form has a checkbox for a newsletter, with its words after it, and a text box for an email address, with its word before it. Each field is wrapped in a label with its words.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with a form holding two paragraphs: a label wrapping a checkbox named news and the words Send me the newsletter, and a label wrapping the word Email and a text box named email.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Labels</title>
</head>
<body>
<form>
<p><label><input type="checkbox" name="news"> Send me the newsletter</label></p>
<p><label>Email <input name="email"></label></p>
</form>
</body>
</html>

Step 2: Click the Words​

Double-click index.html to open it in your browser. Click the words Send me the newsletter, not the box itself:

The page in Chromium: a ticked checkbox with the words Send me the newsletter, and under it the word Email with an empty text box.

The checkbox is ticked. Now click the word Email: the cursor moves into the text box. The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • Clicking the words ticked the checkbox, because the words and the checkbox are in the same label.
  • Clicking Email put the cursor in the text box: Chromium reported the text box named email as the focused element.
  • Each field got its label's words as its name. In Chromium's accessibility tree, the information screen readers use, the checkbox was named Send me the newsletter and the text box Email.

Common Mistakes​

MistakeWhat happensDo this instead
Writing the words next to the field without a labelClicking the words does nothing: the checkbox stayed unticked, and in Chromium's accessibility tree it had no name at allWrap the field and its words in one label
Putting two fields in one labelThe label belongs to the first field only: clicking the words put the cursor in the first text box, and in Chromium's accessibility tree the second one had no nameGive each field its own label

Troubleshooting​

SymptomLikely causeFix
Clicking the words doesn't tick the checkboxThe words aren't inside the same label as the checkboxMove both into one label
A screen reader announces a checkbox without its wordsThe checkbox has no labelWrap it and its words in a label

FAQ​

Should I wrap the field or use the for attribute?​

Both connect a label to its field. Wrapping needs no id. The for attribute lets the label and the field sit apart, for example in different cells of a table.

Can the words come before the field?​

Yes. In the example, Email comes before its text box, and clicking it still moved the cursor into the box.

Conclusion​

Wrapping a field and its words in a label connects them with nothing else to add: the words become clickable, and they become the field's name for screen readers.

Additional Resources​