Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

:hover but :not on a specific class

How do I apply a hover effect on an a element, but not to an a element with the class active?

a:hover(not: .active) 

Seems something is missing.

like image 367
Jürgen Paul Avatar asked Feb 11 '12 16:02

Jürgen Paul


People also ask

Can you use hover with a class?

CSS – Div class hover Hover effect can be directly given to the elements but when it is applied to a particular element like div then the hover effect will be reflected to all the div elements. Using a class to apply the hover effect, gives us a choice to apply it on selective elements.

Can you use hover on a class in CSS?

The :hover selector CSS pseudo-class is used to style elements when the mouse hovers over them. It can be used on every element.

What does &: hover mean in CSS?

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).

Why is CSS hover not working?

To solve the issue, you need to go over the CSS hover code to establish if you use the right selector. As well, ensure you use an opening curly bracket ({) after each selector and a closing curly bracket (}) at the end of the property list.


2 Answers

The functional notation is on :not(), not :hover:

a:not(.active):hover 

If you prefer to put :hover first, that's fine:

a:hover:not(.active) 

It doesn't matter which pseudo-class comes first or last; either way, the selector works the same. It just happens to be my personal convention to put :hover last as I tend to place user-interaction pseudo-classes behind structural pseudo-classes.

like image 192
BoltClock Avatar answered Sep 20 '22 21:09

BoltClock


You have the option of using the not() selector.

a:not(.active):hover { ... } 

However, this may not work in all browsers, as not all browsers implement CSS3 features.

If you are targeting a large audience and want to support older browsers, the best way would be to define a style for the .active:hover and undo whatever you're doing in a:hover.

like image 42
Mendhak Avatar answered Sep 17 '22 21:09

Mendhak