In below example, CSS "helpfully" triggers input:hover
when the label is hovered. How can this label hover effect be suppressed with only changing the CSS, so that the input
border only changes when it is hovered itself? pointer-events: none
is not an option.
input:hover {
border: 1px solid red;
}
<div><label for="foo">label</label></div>
<div><input id="foo"/></div>
2
Answers
A bit hacky but it works.
When an element is hovered, then so are its ancestor elements. But this doesn’t apply to the
:hover
matched by the labelled control. So we can test the parent element of the input to see if it’s also hovered. Like this: