div:empty:not(:focus):before {
content: attr(data-text);
}
div:focus:before {
content: "";
}
<div data-text="placeholder text..."></div>
The placeholder text is shown by CSS but when I click the div element, placeholder text doesn’t get hidden.
What is wrong with this approarch, div:focus:before
?
2
Answers
The
:focus
pseudo-class only applies to elements that can receive focus, such as form elements (<input>
,<select>
, etc.) and links (<a>
). The<div>
element in your code cannot receive focus by default, so the:focus
pseudo-class doesn’t work.This code will allow the
<div>
element to receive focus when the user clicks on it or tabs to it using the keyboard. With thetabindex
attribute set, the:focus
pseudo-class will work as expected, and the placeholder text will be hidden when the<div>
element is in focus.Try to change your
<div>
element for<input>
or<textarea>
like