There isn't exactly a pseudo-class (like ::focus
) for when an input contains data. Focus & hover are interaction events and have nothing to do with the input content itself.
There are a few options here.
You could utilize the :valid
pseudo-class but that will only trigger if the input value matches a regex. So probably not what you really want.
Another option would be to add a class to the input with javascript using the onChange
or blur
events to check if input value === "" or not.
Finally you can utilize the :placeholder-shown
pseudo-class it has pretty good support now but verify your browser targets first.
input:not(:placeholder-shown) {
...
}
Of those, the second option will be the most widely supported and controllable.
This answer has more detail about CSS options: Detect if an input has text in it using CSS -- on a page I am visiting and do not control?
And here's a list of all CSS pseudo classes/elements https://developer.mozilla.org/en-US/docs/Web/CSS/Pseudo-classes
Important Note: There are things listed in that document that are NOT in the CSS spec and NOT supported by any browsers. Be sure to check the Can I Use database before deciding what to use.