::placeholder
Styles the hint text displayed in an empty input or textarea field.
Example
#.input::placeholder {
color: #71717a;
font-style: italic;
}<label style="display: grid; gap: 6px;">
Player name
<input class="input" placeholder="e.g. CodePirate" style="padding: 10px 12px; border: 1px solid #52525b; border-radius: 10px; background: #18181b; color: inherit; font: inherit;">
</label>Definition and usage
#The ::placeholder pseudo-element lets you change the appearance of the text from the placeholder attribute: colour, typeface, size, or font style. It works in <input> and <textarea> fields and is visible only when the field is empty.
It mainly supports text properties. You set the field’s dimensions, border, and padding on the field itself, not on the placeholder.
Mind the contrast. A placeholder is often light grey and hard to read, and it disappears at the first character. Don’t put information in it that is needed to fill in the field, such as a required date format. Put such hints in the label or below the field.
The state “the field is showing a placeholder” is described by a separate pseudo-class, :placeholder-shown. It is useful when you want to style the whole field, not just the placeholder text.
Syntax
#selector::placeholder { property: value; }Common styles
#- The placeholder colour. Type something in the field and the placeholder disappears.
- Italics distinguish the placeholder from the text the user types.
- Uppercase with spaced-out letters and a smaller font size.
- After you click the field the placeholder fades, but stays visible until the first character.
Common styles in practice
#color
.demo::placeholder { color: #a78bfa; }font-style
.demo::placeholder { font-style: italic; }text-transform
.demo::placeholder {
text-transform: uppercase;
letter-spacing: 0.08em;
font-size: 12px;
}:focus::placeholder
.demo:focus::placeholder { color: #52525b; }Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- Watch the placeholder’s contrast. Accessibility guidelines recommend a contrast of at least 4.5:1 for text, and a too-light grey easily fails that threshold.
- A placeholder does not replace a
<label>. It disappears while typing, so users quickly forget what the field was for.
Related entries
#- :placeholder-shownMatches an input or textarea field that is currently showing its placeholder text, meaning it’s empty and has a placeholder attribute.
- :focusMatches the element that has focus, meaning it currently receives keyboard input, such as a clicked form field.
- colorSets the text colour and the default colour of borders, decorations and SVG icons that use currentcolor.
- font-styleTurns on italics or an oblique slant for text.
- ::selectionChanges the appearance of a piece of text selected by the user with the mouse or keyboard.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.