# Labels & Text Inputs

> Every text input needs a label that announces its purpose to screen readers and enlarges the click target for everyone. The type attribute tells the browser which virtual keyboard to show and which built-in validation to apply.

Subject: HTML | Lesson 26 of 42 | 5 min read
Interactive lesson: https://codewithanimation.in/html/labels-and-text-inputs
Previous: [The form Element & Submission](https://codewithanimation.in/html/form-and-submission.md) | Next: [Checkboxes, Radios & Pickers](https://codewithanimation.in/html/checkbox-radio-and-pickers.md)

## What it is

`<label>` connects a text description to a form control. The explicit association uses a `for` attribute on the label whose value matches the `id` of the input: `<label for="email">Email address</label>`. The implicit association wraps the input inside the label element directly. Both approaches tell the browser that clicking the label should focus the input and tell screen readers what to announce when the field receives focus.

`<input>` is a void element that covers a wide range of control types through its `type` attribute. For text collection: `type="text"` is the plain default; `type="email"` validates the value against an email format and shows an email-optimised keyboard on mobile; `type="password"` masks characters and prevents autocomplete from saving the value; `type="tel"` shows a phone dialpad on mobile but does not validate the format (phone number formats vary too much between countries for a universal pattern); `type="number"` shows a numeric keyboard; `type="search"` adds search semantics and often a clear button.

## Why it matters

A label does two things that placeholder cannot: it persists when the user starts typing, and it is reliably announced by screen readers when the field receives focus. A placeholder disappears the moment a character is entered, leaving a user who paused mid-form with no reminder of what the field expected. Placeholder text also tends to fail contrast ratio requirements. Using placeholder as the sole label is one of the most common accessibility failures in web forms.

The `type` attribute is the lightest possible enhancement to a text input: it changes nothing visually on desktop but on a mobile device it switches the virtual keyboard to match the expected input. A phone number field with `type="tel"` shows a dialpad; an email field with `type="email"` shows `@` on the primary keyboard row. These ergonomic improvements cost one attribute.

## How it works

For explicit association, give the input a unique `id` and set the label's `for` to the same value. The two elements can be anywhere in the DOM relative to each other as long as the `id` is unique. For implicit association, place the input inside the `<label>` element with no `for`/`id` needed. Explicit is generally preferred: it keeps the label and input visually separable and works better with CSS grid layouts where the two elements are in different grid areas.

The `autocomplete` attribute hints to the browser which stored value to offer. Common values include `name`, `email`, `current-password`, `new-password`, `tel`, and `street-address`. Setting `autocomplete="off"` suppresses autofill for sensitive fields, but browsers may ignore it for known field types like passwords. The `inputmode` attribute goes a step further: it overrides the virtual keyboard independently of `type`. A PIN field styled as `type="text"` with `inputmode="numeric"` shows a number pad without triggering the number input's stepper controls.

Every input in a form that is not decorative needs a programmatic label. If a visual design places the label away from the input or uses an icon instead of text, use `aria-label` (a string directly on the input) or `aria-labelledby` (pointing to an element by id) to supply the accessible name. These are fallbacks; a real `<label>` is always preferable.

## Common mistake: Using placeholder as the only label

Placeholder text is a hint, not a label. It is shown only when the field is empty and typically renders at a contrast ratio well below the 4.5:1 minimum for text. When a user tabs into a field or starts typing, the hint disappears and the field is visually unlabelled. Screen reader behaviour for placeholder is inconsistent across browser and AT combinations. A form where every field says "Enter your email" and has no persistent label is an accessibility failure that is also a usability problem for everyone.

## Tip: Pair autocomplete with type for the fastest possible autofill experience

When both `type` and `autocomplete` are set correctly, browsers can fill an entire address form in one tap. `type="email" autocomplete="email"` makes the browser confident enough to autofill the field without asking. `type="tel" autocomplete="tel"` does the same for phone numbers. The combination also enables password manager integration: `type="password" autocomplete="current-password"` signals a login field; `autocomplete="new-password"` signals a registration field and triggers password generator UX. Missing either attribute degrades the autofill experience.

## Try it: Try it

Click the label text to see the input focus activate. Compare the associated pair with a placeholder-only field.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/html/labels-and-text-inputs

## Check yourself

### Question 1

A user clicks the text "Email address" next to an input field but the input does not focus. What is the most likely cause?

- A. The <label> is missing a for attribute that matches the input's id
- B. The input type is "email" instead of "text"
- C. The label and input are in different <div> elements

Correct answer: A

Clicking a `<label>` only focuses its associated input when the `for` attribute matches the input's `id` (explicit) or the input is a descendant of the label (implicit). Without that link the label is just text.

- Why not B: Input type does not affect label association. The missing for/id link is what prevents the label click from focusing the input.
- Why not C: Explicit label association (for/id) works regardless of DOM nesting. The label and input can be in completely different containers as long as the id is unique and matches.

### Question 2

Why is placeholder not a replacement for <label>?

- A. It disappears when the user types and has inconsistent screen reader support
- B. Placeholder text cannot be styled with CSS
- C. Placeholder only works on type="text" inputs

Correct answer: A

Placeholder disappears when the user starts typing, leaving the field without a visible label. It also typically fails the 4.5:1 contrast requirement and is announced inconsistently by screen readers.

- Why not B: Placeholder text can be styled via the ::placeholder pseudo-element. The accessibility problem is its disappearing nature and contrast, not styling.
- Why not C: Placeholder works on most text-based input types. The core problem is that it vanishes on typing, leaving the field unlabelled for a user who pauses mid-entry.

### Question 3

Does type="tel" validate that the entered value is a valid phone number?

- A. No. It shows a phone dialpad on mobile but applies no format validation
- B. Yes. It checks for a valid international E.164 format
- C. Yes, but only on mobile devices where the dialpad is shown

Correct answer: A

`type="tel"` changes the virtual keyboard to a dialpad but performs no validation. Phone number formats differ globally, so HTML leaves validation to the `pattern` attribute or server-side logic.

- Why not B: Phone number formats vary enormously between countries. The HTML spec deliberately does not validate tel inputs, leaving format enforcement to the pattern attribute or server-side logic.
- Why not C: There is no tel validation on any device. The type value only influences the virtual keyboard on mobile. Validation must be done separately via pattern or custom logic.

## Remember this

- Always pair a `<label>` with every input: `for` on the label must match the `id` on the input for explicit association.
- `placeholder` is not a label: it disappears when the user types and is not reliably announced by screen readers.
- `type="tel"` shows a phone dialpad but does not validate the number format.
- `autocomplete` and `type` together enable one-tap autofill and password manager integration.
