Home/Learn/HTML, CSS & Responsive/Forms — Labels, Input Types and Autofill

Forms — Labels, Input Types and Autofill

Intermediate
Semantic HTML

The right input type gives a phone the right keyboard, the right autocomplete lets a browser fill a form in one tap, and a real label makes the field usable at all.

Overview

Forms are where the gap between working and usable is widest. A text input with a placeholder instead of a label technically accepts typing, but the hint vanishes the moment someone starts, fails contrast, and is not announced. A type="text" for a phone number gives a mobile user the full alphabet keyboard. A missing autocomplete attribute turns a thirty-second checkout into two minutes of typing. None of this is difficult — it is a set of attributes most developers never learned, and it is immediately visible to anyone reviewing a take-home.

Labels

Every input needs one. A placeholder is not a label.

label, sr-only, aria-describedby
<label for="email">Email address</label>
<input id="email" name="email" type="email">

// Or wrap, which needs no id
<label>
  Email address
  <input name="email" type="email">
</label>

// A real label also expands the click target — tapping the text
// focuses the field, which matters a lot on a phone.

// Placeholder as label: fails on four counts
<input placeholder="Email address">      // no announced name,
// disappears on typing, low contrast, and looks pre-filled.

// Visually hidden when the design has no room — still announced
<label for="q" class="sr-only">Search problems</label>
<input id="q" type="search" placeholder="Search…">

// Help text and errors, linked so they are announced with the field
<input id="pw" aria-describedby="pw-help pw-error" aria-invalid="true">
<p id="pw-help">At least 8 characters.</p>
<p id="pw-error" role="alert">Include a number.</p>

// Group related controls
<fieldset><legend>Difficulty</legend> …radios… </fieldset>

Input Types

The type changes the mobile keyboard, the validation and the picker.

type, inputmode, and why number is a trap
<input type="email">     // @ key on the keyboard, format validation
<input type="tel">       // numeric keypad, no format assumptions
<input type="url">       // .com key
<input type="search">    // a clear button, "Search" on the return key
<input type="date">      // native date picker
<input type="password">  // masked, offers the password manager
<input type="number">    // spinner — for QUANTITIES only

// number is wrong for phone numbers, OTPs, PINs and card numbers:
// it strips leading zeros, allows e and -, and scrolling changes it.
<input type="text" inputmode="numeric" pattern="[0-9]*" autocomplete="one-time-code">

// inputmode picks the keyboard without changing validation
inputmode="numeric" | "decimal" | "tel" | "email" | "url" | "search"

// Constraint attributes — free validation and messages
<input type="email" required minlength="8" maxlength="64"
       pattern="[0-9]{6}" step="0.01" min="1" max="10">

// Style the states, but only after the user has interacted
input:user-invalid { border-color: var(--danger) }

Autofill

The attribute that halves the time to complete a form, and is almost always missing.

The attribute that halves completion time
<input name="name"     autocomplete="name">
<input name="email"    autocomplete="email">
<input name="phone"    autocomplete="tel">
<input name="address"  autocomplete="street-address">
<input name="city"     autocomplete="address-level2">
<input name="pincode"  autocomplete="postal-code">
<input name="country"  autocomplete="country">

// Sign in vs sign up — this is what makes a password manager offer
// to save, and offer the right entry
<input type="password" autocomplete="current-password">   // sign in
<input type="password" autocomplete="new-password">       // sign up
<input inputmode="numeric" autocomplete="one-time-code">  // OTP autofill

// Payment fields
autocomplete="cc-number" | "cc-name" | "cc-exp" | "cc-csc"

// autocomplete="off" on a whole form is nearly always wrong —
// browsers ignore it for logins anyway, and it only punishes users
// filling a long form.

// Also: never disable paste on a password or OTP field.

Key Points to Remember

  • 1Every input needs a real label — a placeholder is not announced, vanishes on typing and fails contrast
  • 2A label also enlarges the tap target, which matters most on phones
  • 3The input type sets the mobile keyboard and native validation; type="number" is wrong for phone numbers, OTPs and PINs
  • 4inputmode picks the keyboard without changing validation semantics
  • 5autocomplete tokens let the browser and password manager fill a form in one tap — current-password and new-password are the important pair

Interview Questions

Sign in to ask Aria
1

Why is a placeholder not a substitute for a label?

Easy
2

Why is type="number" the wrong choice for a phone number or an OTP?

Medium
3

What does the autocomplete attribute do beyond convenience?

Medium

Ask Aria about Forms — Labels, Input Types and Autofill

Your personal AI tutor — ask anything about this concept

Revision Status

Personal Notes

Sign in to save personal notes for this topic.

Discussion

Sign in to join the discussion.

Loading discussion…