What is the placeholder attribute?

placeholder shows a short hint inside an empty text-like input or textarea, such as an example format (name@example.com), and disappears as soon as the user types. It is a hint, not a label: it should never replace a visible <label>, because it vanishes during typing, often has poor contrast, and is not reliably announced by assistive technology.

Good uses

  • Example values: placeholder="e.g. 2026-11-21" or "+91 98765 43210".
  • Short hints in a search box that also has a (possibly visually hidden) label.

Problems when misused

  • Users forget what the field was for once they start typing.
  • Light gray text often fails contrast requirements (WCAG 1.4.3).
  • Some users mistake placeholder text for a prefilled value and skip the field.
  • Translation tools and some screen readers handle it inconsistently.

Better alternatives for instructions

Put format rules or requirements in helper text below the field, linked with aria-describedby.

Styling

  • ::placeholder pseudo-element: input::placeholder { color: #64748b; } (ensure 4.5:1 contrast if it carries information).
  • :placeholder-shown matches inputs currently showing the placeholder, which enables CSS-only "floating label" patterns.

Applies to

text, search, email, url, tel, password, number and <textarea>. Not to checkboxes, radios, dates or <select>.

Code example

html
1<style>
2 body { font-family: system-ui; }
3 .field { position: relative; margin: 16px 0; max-width: 300px; }
4 input { width: 100%; padding: 18px 10px 6px; font-size: 16px; border: 1px solid #94a3b8; border-radius: 6px; box-sizing: border-box; }
5 input::placeholder { color: #64748b; } /* accessible contrast */
6 .field label { position: absolute; left: 10px; top: 14px; color: #475569; transition: all 0.15s; pointer-events: none; }
7 input:focus + label, input:not(:placeholder-shown) + label { top: 2px; font-size: 12px; } /* floating label */
8 .hint { font-size: 13px; color: #475569; }
9</style>
10
11<!-- Bad: placeholder used as the only label -->
12<p><input placeholder="Email"></p>
13
14<!-- Good: real label + example placeholder + helper text -->
15<div class="field">
16 <input id="phone" type="tel" placeholder="+91 98765 43210" aria-describedby="phone-hint">
17 <label for="phone">Phone number</label>
18</div>
19<p id="phone-hint" class="hint">Include your country code.</p>

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.

Open in study app