Understand how the HTML placeholder attribute provides temporary hint text within form inputs, and learn why it must never replace a visible, accessible label.
What it is
The placeholder attribute adds grayed-out hint text inside an input field. This text disappears as soon as the user begins typing or focuses on the field in some browsers. It serves as a visual cue for expected format or content, such as "YYYY-MM-DD" or "user@example.com". Related terms include hint text, input guidance, and form accessibility.
Why it matters
- Visual Guidance: Helps users understand specific formatting requirements without cluttering the interface with permanent instructions.
- Space Efficiency: Allows designers to keep forms clean by hiding hints until needed.
- Accessibility Awareness: Understanding its limitations prevents common usability errors that exclude screen reader users or those with cognitive disabilities.
- Error Prevention: Proper use reduces submission errors by clarifying expectations before data entry begins.
Syntax or steps
Add the placeholder attribute directly to any valid input element (e.g., <input>, <textarea>). The value is a string containing the hint text.
<input type="text" placeholder="Enter your name">
Example
Below is a correct implementation showing both a proper label and a helpful placeholder.
<label for="email">Email Address</label>
<input type="email" id="email" name="email" placeholder="name@example.com">
Part-by-part explanation:
<label for="email">: Creates a persistent, clickable label associated with the input via the matchingid. This ensures accessibility and clarity even after typing starts.<input type="email">: Defines the input field with validation for email formats.id="email": Links the input to the label above.placeholder="name@example.com": Provides a non-persistent hint about the expected format. Note that this does not describe what the field is (the label does), but rather how to fill it.
Common mistakes
- Using placeholder as a label: If you omit the
<label>tag and rely solely on the placeholder, the text vanishes when the user types. Screen readers may ignore placeholders entirely, leaving users unaware of what they are filling out. - Overloading with information: Placeholders should be short. Long sentences break layout and are hard to read against low-contrast backgrounds.
- Ignoring contrast ratios: Default placeholder text often has poor color contrast. Ensure it meets WCAG guidelines if customizing styles, though relying on browser defaults is safer.
- Assuming universal behavior: Some older browsers or assistive technologies handle focus and visibility differently. Never assume the hint will always be seen.
When to use it
Compare placeholder with <label> and title attributes.
| Feature | Best For | Limitations |
|---|---|---|
<label> |
Identifying the field's purpose (e.g., "First Name"). | Takes up vertical space; essential for accessibility. |
placeholder |
Showing format examples (e.g., "MM/DD/YYYY"). | Disappears on input; poor accessibility support. |
title |
Additional tooltips on hover. | Invisible on touch devices; inconsistent across browsers. |
Use placeholder only for supplementary formatting hints, never for primary identification.
Practice
Guided Exercise: Create a date input field. Add a label saying "Date of Birth" and a placeholder showing "YYYY-MM-DD".
Challenge: Modify your code so that the placeholder text remains visible even when the user clicks into the field (using CSS :focus pseudo-class). Does this solve the accessibility problem? Why or why not?
Solution Hint: Keeping the placeholder visible helps sighted users but still fails screen readers because placeholders are not reliably announced as labels. Always pair with a real <label>.
Quick check
Question: Can a placeholder attribute serve as the sole identifier for a form field for all users?
Answer: No. Placeholders disappear upon typing and have inconsistent support with assistive technologies like screen readers. A visible <label> is required for accessibility.
Summary
The placeholder attribute is a useful tool for indicating input format but is strictly supplementary. It must always accompany a persistent, programmatically associated <label> to ensure usability and accessibility for all users.