I’ve got the following scenario:
HTML
.input {
color: red;
}
.input:not(:disabled, :read-only):hover {
color: blue;
}
<input class="input" type="text" value="Hello">
<div class="input">Hello</div>
So the hover effect won’t apply to the div element, but if I remove the :read-only
pseudo-class part from the css, it will.
Why is this happening? 🤔
3
Answers
Because the
read-only
selector doesn’t just select elements, it will select any element that cannot be edited by the user. This includes that div element.@TemaniAfif provides a nice way to make the div editable to make it supports
read-only
anddisable
attributes, if you don’t want to make it editable for some reason, you can use classes instead.The
:not()
pseudo-selector does not take 2 delecrations the way you intend too. Simply use 2 complete selectors.The
:not()
selector you are using is applying an AND logic so both requirements need to be true.:not(:disabled)
and:not(:read-only)
. The div doesn’t match the second one (only the input does) so the hover effect won’t work.If you make it editable it will be like the input