:placeholder-shown
Example
input:empty + label {
font-size: 0.75rem;
}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-shownnever matches because the input has noplaceholderattribute.- Confusing it with
::placeholder, which styles the hint text, not the field.
Key points
- The input has a
placeholderattribute, even a single space, or:placeholder-shownnever 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.