CSS pseudo-elementPseudo-elements

::placeholder

Styles the hint text displayed in an empty input or textarea field.

On this page

Example

#
CSS
.input::placeholder {
  color: #71717a;
  font-style: italic;
}
Preview

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

#
Syntax
selector::placeholder { property: value; }

Common styles

#
  • color

    The placeholder colour. Type something in the field and the placeholder disappears.
  • font-style

    Italics distinguish the placeholder from the text the user types.
  • text-transform

    Uppercase with spaced-out letters and a smaller font size.
  • :focus::placeholder

    After you click the field the placeholder fades, but stays visible until the first character.

Common styles in practice

#

color

The placeholder colour. Type something in the field and the placeholder disappears.
.demo::placeholder { color: #a78bfa; }

font-style

Italics distinguish the placeholder from the text the user types.
.demo::placeholder { font-style: italic; }

text-transform

Uppercase with spaced-out letters and a smaller font size.
.demo::placeholder {
  text-transform: uppercase;
  letter-spacing: 0.08em;
  font-size: 12px;
}

:focus::placeholder

After you click the field the placeholder fades, but stays visible until the first character.
.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

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.