Emmanuel Akinfulubi

HTML and CSSHTML and CSS

Why placeholder is not a label in HTML forms

17 Placeholder is not a label

fair

Take a field that only had placeholder text and give it a real label, a hint that survives typing, and validation the browser performs itself.

Stuck? Three hints, smallest first
  1. 1. A placeholder disappears the moment someone types, so it cannot be the label.
  2. 2. Put the format hint in its own element and join it with aria-describedby.
  3. 3. pattern, minlength and inputmode get you validation and the right keyboard with no code.

index.html

<p class="field">
  <label for="plate">Vehicle plate</label>
  <input
    id="plate" name="plate" type="text"
    inputmode="latin" autocomplete="off"
    pattern="[A-Z]{3}-[0-9]{3}-[A-Z]{2}" minlength="10" maxlength="10"
    required aria-describedby="plate-hint">
  <span class="hint" id="plate-hint">Three letters, three digits, two letters. For example LAG-441-XA.</span>
</p>

style.css

.field {
  display: grid;
  gap: 4px;
  max-width: 320px;
  margin: 0;
}

.field label { font-weight: 600; font-size: 0.9rem; }

.field input {
  padding: 8px 10px;
  border: 1px solid #8e8e98;
  border-radius: 6px;
  font: inherit;
  text-transform: uppercase;
}

.field input:focus-visible {
  outline: 2px solid #3055d8;
  outline-offset: 1px;
}

/* Only after the field has been touched, so an untouched form is not all red. */
.field input:user-invalid {
  border-color: #a3341f;
}

.hint {
  color: #5c5c66;
  font-size: 0.8rem;
  line-height: 1.4;
}
Rendered result. Shown, not machine-checked: there is no browser in the build.
8 checks that prove this answer

Assertions, run in the browser. The markup and styles above are handed to them as html and css, so press Run and each check prints ok. The build fails if a single one fails.

Checks for exercise 17

test('there is a real label', /<label for="plate"/.test(html), true);
test('and no placeholder pretending to be one', /placeholder=/.test(html), false);
test('the format hint is a persistent element', /id="plate-hint"/.test(html), true);
test('joined to the field so it is announced', /aria-describedby="plate-hint"/.test(html), true);
test('the browser validates the shape itself', /pattern="\[A-Z\]\{3\}-\[0-9\]\{3\}-\[A-Z\]\{2\}"/.test(html), true);
test('a length is enforced both ways', /minlength="10"/.test(html) && /maxlength="10"/.test(html), true);
test('the invalid style waits until the field has been used', /:user-invalid/.test(css), true);
test('and :invalid alone is not used, which would redden it on load', /[^-]:invalid/.test(css), false);

Output


      

Placeholder text fails three ways at once: it vanishes as soon as there is a value, it is often too low-contrast to read, and several screen readers do not announce it at all. A label plus a hint joined with aria-describedby costs one more element and survives all three. The CSS trap is the last check: :invalid matches an empty required field immediately, so a fresh form loads covered in red. :user-invalid waits until the user has actually interacted, which is what you meant.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.