EthicsComputerCourses
Join training +91 90532 10052

Labels & Placeholders

HTML · Lesson 31 of 234 Beginner 8 min read Updated

By the end of this lesson you will be able to give every form field a proper label, add hint text that screen readers announce, hide a label visually without hiding it from assistive technology, and make labels easy to tap on a phone.

HTML Forms Introduction showed the two ways to connect a <label>, and Advanced Form Attributes warned that a placeholder is not a label. Here you will go further.

What a label does

Think of the steel tiffin boxes in an office kitchen. Each one has a name sticker, so the right person picks up the right lunch. A form field without a label is a tiffin with no sticker: you can open it and guess, but you should not have to.

A <label> does three jobs at once:

  • It shows the question. Sighted users read it.
  • It names the field. When a screen reader user moves into the field, the label is read aloud, for example "Mobile number, edit text".
  • It grows the click area. Clicking the label text puts the cursor in a text box, or ticks a checkbox. On a phone, that bigger target matters a lot.

Text that merely sits next to an input, such as <p>Name</p>, does none of these jobs.

Two ways to connect a label

Explicit labels use for and id. The label's for value must exactly match the field's id, and that id must be unique on the page.

Implicit labels wrap the field inside the <label> element. No id is needed.

for + idWrapping
Label and field can be far apart (grid, table)YesNo
Needs a unique idYesNo
Typical useText boxes, selects, textareasCheckboxes and radio buttons

Both are correct, and you may combine them if for points at the wrapped field. A label names exactly one field and must not contain a second one, but one field can have several labels.

Labels work with every labelable element: <input> (except type="hidden"), <select>, <textarea>, <button>, <meter>, <output> and <progress>.

Step 1: build a library card form. The script at the end uses the labels property, which every labelable element has. It lists the labels the browser has linked to the field you are in, so you can test your connections.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Library Card</title>
  <style>
    label { display: block; margin-top: 12px; }
    output { font-weight: bold; }
  </style>
</head>
<body>
  <h1>Kochi Public Library: new card</h1>
  <form action="#">
    <label for="member">Full name</label>
    <input id="member" name="member">

    <label>
      Mobile number
      <input name="mobile" type="tel">
    </label>

    <label for="mail">Email</label>
    <input id="email" name="email" type="email">

    <input name="school" placeholder="School or college">
  </form>
  <p>This field is labelled by: <output id="info">click a field</output></p>

  <script>
    for (const field of document.querySelectorAll("input")) {
      field.addEventListener("focus", () => {
        const names = [...field.labels].map((l) => l.textContent.trim());
        document.querySelector("#info").textContent =
          names.length ? names.join(" + ") : "nothing (no label)";
      });
    }
  </script>
</body>
</html>

Click into each field. The first two report their labels. The Email field reports "nothing", because for="mail" does not match id="email", and clicking the word "Email" does nothing. Fix the typo and both problems disappear. The last field has only a placeholder, so it has no label.

Why a placeholder is not a label

A placeholder is the grey text inside an empty field. It looks tidy, so designers like it. But it fails as a label:

  • It disappears as soon as the user types, so they forget what the field wanted. This is hardest for people with memory or attention difficulties.
  • Browser autofill fills the field and hides the hint, so the user cannot check that the right value went into the right box.
  • The default grey is faint, and making it darker makes the field look already filled in.
  • Some browsers use the placeholder as a backup name for screen readers, but sighted users still lose it while typing.

Use a placeholder only for a short example of the format, and always with a real label.

HTML
<!-- Weak: the hint vanishes on the first keystroke -->
<input name="pin" placeholder="PIN code">

<!-- Better: a visible label, with the placeholder as an example -->
<label for="pin">PIN code</label>
<input id="pin" name="pin" inputmode="numeric" placeholder="e.g. 682001">

Hint text with aria-describedby

Some fields need more help, such as a password rule. Put the hint in visible text, give it an id, and point to it from the field with aria-describedby. ARIA attributes add accessibility information to HTML; you will study them in ARIA Roles & Attributes.

HTML
<label for="pass">Create a password</label>
<input id="pass" name="pass" type="password" aria-describedby="pass-hint">
<p id="pass-hint">At least 10 characters. Mix letters and numbers.</p>

A screen reader now reads the label first, then the field type, then the hint, roughly "Create a password, edit text, at least 10 characters...". The label stays short and the details are still announced. aria-describedby can list several ids separated by spaces, which is useful when an error message appears later (see Form Errors).

Visually hidden labels

Sometimes a design shows no label text, such as a search box next to a magnifying-glass button. The field still needs a name. Do not use display: none on the label: that hides it from screen readers too. Instead, use a small CSS class that removes the text from view but keeps it in the page.

Step 2: build a search bar with a hidden label and large tap targets.

HTMLOpen in editor
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Search Books</title>
  <style>
    .visually-hidden {
      position: absolute;
      width: 1px;
      height: 1px;
      margin: -1px;
      padding: 0;
      border: 0;
      overflow: hidden;
      clip-path: inset(50%);
      white-space: nowrap;
    }
    input, button { font-size: 1rem; padding: 10px; }
    .check { display: inline-block; padding: 12px 8px; cursor: pointer; }
  </style>
</head>
<body>
  <search>
    <form action="#">
      <label for="q" class="visually-hidden">Search books</label>
      <input id="q" name="q" type="search" placeholder="e.g. Malgudi Days">
      <button>Search</button>
      <br>
      <label class="check"><input type="checkbox" name="available"> Only books on the shelf</label>
    </form>
  </search>
</body>
</html>

The label "Search books" is invisible, but screen readers still announce it. Now click anywhere on the words "Only books on the shelf", even in the padding around them: the box ticks. The padding made the label a bigger target.

aria-label="Search books" on the input would also name it, but real label text is easier to inspect and can be made visible later without changing the HTML.

Common mistakes

  • for and id that do not match. One wrong letter breaks the link, silently.
  • Duplicate ids. Two fields with id="name" mean one label points at the wrong field.
  • Placeholder instead of a label. The question disappears while typing.
  • Hiding a label with display: none. Screen readers lose it as well. Use a visually hidden class.

Interview questions

1. What is the difference between an explicit and an implicit label?

An explicit label uses for with the value of the field's id, so the two can be anywhere on the page. An implicit label wraps the field inside the <label> element, so no id is needed. Both give the field an accessible name and make the label clickable.

2. Why should a placeholder not replace a label?

It disappears once the user types or autofill runs, so they cannot check what the field was for. Its default colour is faint, and a darker one looks like a filled value. A placeholder should only show a short example next to a real label.

3. How do you add hint text that screen readers announce?

Put the hint in a visible element with an id and reference it from the field with aria-describedby="that-id". Screen readers read the label, then the role, then the description. You can list several ids for hints and error messages.

4. How do you hide a label visually but keep it for screen readers?

Use a visually hidden CSS class that shrinks the element to one pixel and clips it, while keeping it in the accessibility tree. display: none and visibility: hidden remove it from screen readers too, so they are wrong here.

5. Which elements can a label be connected to?

Labelable elements: <input> except hidden inputs, <select>, <textarea>, <button>, <meter>, <output>, <progress> and form-associated custom elements. A <div> cannot be labelled with <label>.

Key points

  • A label shows the question, names the field for screen readers and enlarges the click area.
  • Connect with for and a matching unique id, or by wrapping the field.
  • A placeholder is a short example, never a replacement for a label.
  • Put extra help in visible text linked with aria-describedby.
  • Hide labels with a visually hidden class, never with display: none.
  • Padding on labels helps meet the 24 by 24 pixel minimum target size.

Exercise

Build a sign-up form for a cricket coaching camp in Rohtak with three fields: Player name (explicit label), Date of birth with the hint "Players must be 10 to 16 years old" linked through aria-describedby, and a checkbox "I agree to the camp rules" with a wrapping label padded to a comfortable size. Add a search-style field "Find your nearest ground" with a visually hidden label. Test every field with the labels script from Step 1.

Show solution
HTML
<style>
  .visually-hidden { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; border: 0; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
  .check { display: inline-block; padding: 12px 8px; }
</style>
<form action="#">
  <p>
    <label for="player">Player name</label>
    <input id="player" name="player">
  </p>
  <p>
    <label for="dob">Date of birth</label>
    <input id="dob" name="dob" type="date" aria-describedby="dob-hint">
    <span id="dob-hint">Players must be 10 to 16 years old.</span>
  </p>
  <label class="check"><input type="checkbox" name="rules" value="agreed"> I agree to the camp rules</label>
  <p>
    <label for="ground" class="visually-hidden">Find your nearest ground</label>
    <input id="ground" name="ground" type="search" placeholder="e.g. Model Town">
  </p>
</form>

Every field reports a label in the test script, the hint is read after the date field's name, and the padded checkbox label is easy to tap.

Quick Quiz

1. A label has for="city". What must the input have?

2. Why is display: none wrong for a label you want to hide visually?

3. What does aria-describedby add to a field?

4. What is a good use of placeholder?

Learn HTML with a mentor

Free lessons get you started. Our Full Stack Web Development training adds real projects and code reviews from working developers.

  • Project-based learning
  • Mentor code reviews
  • Free counselling call