Understanding the :placeholder-shown Pseudo-Class in CSS
The :placeholder-shown pseudo-class in CSS targets input or textarea elements that are currently displaying their placeholder text. This allows you to style elements differently when they are empty and showing a placeholder.
Applies only to form elements with a placeholder attribute that is currently visible.
Helps indicate an empty state visually or apply custom styling when input is not yet filled.
Once the user enters text, the element no longer matches :placeholder-shown.
In this example, inputs with visible placeholders have a dashed gray border and light gray background. When the user types into the input, the border turns solid green and the background becomes light green, providing a clear visual cue.
Use :placeholder-shown to differentiate empty inputs from filled inputs.
Combine with transitions for smooth visual feedback as the user types.
Do not rely solely on color; provide additional cues for accessibility.
Test across browsers, as placeholder behavior may vary slightly.
How would you style a form input to turn its border red only when the placeholder is visible, but revert to gray when the user starts typing?
What happens if you apply :placeholder-shown to an input that already has text in it — does the style still apply?
A form looks broken on mobile: the placeholder text is invisible, but the input border is still red. What could be causing this, and how would you debug it using :placeholder-shown?
We’re seeing inconsistent placeholder styling across Safari and Chrome. How would you approach making it consistent without breaking other form elements?
We’re building a reusable form component library and want to support dynamic placeholder text. How would you design the CSS to ensure :placeholder-shown works reliably across all input types without requiring developers to write custom styles each time?
A legacy form uses inline styles and JavaScript to toggle placeholder appearance. How would you migrate this to use :placeholder-shown while maintaining accessibility and performance?
Our design system has hundreds of forms across 12 products, and placeholder styling is inconsistent. How would you architect a scalable, maintainable solution using :placeholder-shown that can be adopted across teams without breaking existing behavior?
We’re planning to deprecate legacy placeholder JavaScript hacks. What migration strategy would you propose to ensure zero UX regression, especially for users with assistive technologies, while adopting :placeholder-shown at scale?