:placeholder-shown

Term side of the Card.
Topic: Pseudo Classes
Unit title: :placeholder-shown
Categories: Pseudo-class
Explanation side of the Card

Explanation: The pseudo-class that selects input or textarea elements when their placeholder text is visible. It can be used to apply styles when the placeholder is present, such as adjusting the background color or adding a border to indicate that the field is empty.

Browser compatibility:
  • The chrome browser supports this feature starting from version 47.
  • The edge browser supports this feature starting from version 79.
  • The firefox browser supports this feature starting from version 51.
  • The safari browser supports this feature starting from version 9.
Availability: 

Example

Wrongcss
input:empty + label {
  font-size: 0.75rem;
}
Rightcss
input:not(:placeholder-shown) + label {
  font-size: 0.75rem;
}

input:empty always matches because inputs have no children; :not(:placeholder-shown) matches only once the field has a value.

Common mistakes

  • :placeholder-shown never matches because the input has no placeholder attribute.
  • Confusing it with ::placeholder, which styles the hint text, not the field.

Key points

  • The input has a placeholder attribute, even a single space, or :placeholder-shown never matches.
  • Floating labels use a real <label>, not the placeholder as the only label.
  • Styles for the hint text itself use ::placeholder, not :placeholder-shown.

Interview questions

What is the difference between :placeholder-shown and ::placeholder?

:placeholder-shown selects the input while its hint is visible. ::placeholder styles the hint text itself.

Can I use :placeholder-shown without a visible placeholder?

Yes. Set placeholder=' ' with a single space, and the pseudo-class still matches while the field is empty.

Analytics cookiesWe'd like to use analytics cookies to see which pages are useful. They're set only if you accept. Privacy Policy