:hover but :not on a specific class

HtmlCssCss Selectors

Html Problem Overview


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.

Html Solutions


Solution 1 - Html

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.

Solution 2 - Html

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.

Solution 3 - Html

We can use :not() operator on hover like below example.
>We can use this when we don't want to apply :hover effect to the selected item

.block-wraper {
  display: flex;
}

.block {
  width: 50px;
  height: 50px;
  border-radius: 3px;
  border: 1px solid;
  margin: 0px 5px;
  cursor: pointer;
}

.active {
  background: #0095ff;
}

.block:not(.active):hover {
  background: #b6e1ff;
}

<div class="block-wraper">
  <div class="block"></div>
  <div class="block active"></div>
  <div class="block"></div>
  <div class="block"></div>
</div>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJ&#252;rgen PaulView Question on Stackoverflow
Solution 1 - HtmlBoltClockView Answer on Stackoverflow
Solution 2 - HtmlMendhakView Answer on Stackoverflow
Solution 3 - HtmlRohit TagadiyaView Answer on Stackoverflow