Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

prevent a pseudo element from triggering hover?

If I have markup:

<div class="a b"></div>

where the .a class has a hover class associated with it

and the .b class has a pseudo element associated with it... like so:

div
{
    width: 100px;
    height: 100px;
}
.a { background: red; display: inline-block; }
.a:hover { background: green; }

.b:after
{
    content: '';
    display: inline-block;
    width: 100px;
    height: 100px;
    margin-left: 100px;
    background: pink;
}

Is it possible with css to prevent a pseudo element from triggering the .a class hover?

FIDDLE

like image 994
Danield Avatar asked May 21 '13 12:05

Danield


People also ask

How do I turn off hover effect?

To remove the CSS hover effect from a specific element, you can set the pointer-events property of the element (the hover behavior of which you want to disable) to “none”.

Can you hover a pseudo element?

You can't apply a hover to just the pseudo element.

How do you set hover to none?

One method to do this is to add: pointer-events: none; to the element, you want to disable hover on. (Note: this also disables javascript events on that element too, click events will actually fall through to the element behind ).

Is hover a pseudo?

The :hover CSS pseudo-class matches when the user interacts with an element with a pointing device, but does not necessarily activate it. It is generally triggered when the user hovers over an element with the cursor (mouse pointer).


1 Answers

The following css does the trick for modern browsers (not IE10-):

.b:after {
  pointer-events: none;
}

pointer-events: none allows elements to not receive hover/click events.

See this fiddle.


Caution

pointer-events support for non-SVG elements is in a relative early state. developer.mozilla.org gives you the following warning:

The use of pointer-events in CSS for non-SVG elements is experimental. The feature used to be part of the CSS3 UI draft specification but, due to many open issues, has been postponed to CSS4.

Chrome's box model interpretation of display: inline-block causes a flicker in the above fiddle.
If you switch to display: block instead, you will get the proper interpretation of the box.
Firefox does not have this issue.
To ensure consistent box model interpretation, use the following:

.b:after {
  pointer-events: none;
  display: block;
}

View this fiddle in Chrome to see the flicker effect.

like image 139
tessi Avatar answered Sep 19 '22 05:09

tessi