Web development

Give a form input an explicit label

Associate a visible label with an input by matching the label’s for attribute to the input’s unique ID. A placeholder can supplement a label but should not replace it.

Worked example

HTML example · save as example.html and open in a browser

<form>
  <label for="query">Search articles</label>
  <input id="query" name="q" type="search" placeholder="Try: JSON files">
  <button type="submit">Search</button>
</form>

How it works

The association gives the input an accessible name and usually lets clicking the label focus the field. The name attribute is the submitted field key; id identifies the element within the document. These attributes serve different purposes.

Limits and mistakes

Duplicate IDs can break the association. Placeholder text disappears during typing and may have poor contrast, so it is unsuitable as the only description. This example provides structure, not an implemented search backend.

Check the result

Click Search articles and confirm the field receives focus. Inspect the input’s accessible name, then type a value and verify the visible label remains available.

Sources

Reference documentation for the behavior used in this guide: