161

I have CSS that changes formatting when you hover over an element.

.test:hover { border: 1px solid red; }
<div class="test">blah</div>

In some cases, I don't want to apply CSS on hover. One way would be to just remove the CSS class from the div using jQuery, but that would break other things since I am also using that class to format its child elements.

Is there a way to remove 'hover' css styling from an element?

TylerH
  • 20,799
  • 66
  • 75
  • 101
dev.e.loper
  • 35,446
  • 76
  • 161
  • 247

6 Answers6

318

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

Browser Support ( 98.12% as of Jan 1, 2021 )

This seems to be much cleaner

/**
 * This allows you to disable hover events for any elements
*/
.disabled {
  pointer-events: none;  /* <----------- */
  opacity: 0.2;
}

.button {
  border-radius: 30px;
  padding: 10px 15px;
  border: 2px solid #000;
  color: #FFF;
  background: #2D2D2D;
  text-shadow: 1px 1px 0px #000;
  cursor: pointer;
  display: inline-block;
  margin: 10px;
}

.button-red:hover {
  background: red;
}

.button-green:hover {
  background:green;  
}
<div class="button button-red">I'm a red button hover over me</div>

<br />

<div class="button button-green">I'm a green button hover over me</div>

<br />

<div class="button button-red disabled">I'm a disabled red button</div>

<br />

<div class="button button-green disabled">I'm a disabled green button</div>
Saiansh Singh
  • 583
  • 5
  • 16
Bodman
  • 7,938
  • 3
  • 29
  • 34
  • 4
    Good answer, it's only unsupported on IE < 11, so not that much of a problem any more. – Olivier - interfaSys May 20 '15 at 20:59
  • @MarkBuikema this is true. Edited the answer to reflect that. In many cases it is an added bonus. – Bodman Aug 31 '16 at 22:23
  • annoying that this isn't widely supported. "don't change formatting on hover of tab.current" is a behavior probably intended on most nav menus. For those reporting to simply use 2 classes, consider how widely prevalent Bootstrap and Angular styling libraries at this point. One doesn't simply code 2 classes. You are probably detangling 3rd party styles. Messy. Thanks for info @Olivier-interfaSys . – Eleanor Zimmermann Aug 31 '16 at 22:45
  • @EleanorZimmermann I would say that this is widely supported. Globally 90.97% of users. But yes, if you are worried about IE9 , you should provide another class. – Bodman Aug 31 '16 at 23:19
  • @Bodman , good info, thankyou. I did end up using your solution (I am lucky in that I don't need to worry about IE9). – Eleanor Zimmermann Sep 02 '16 at 16:39
  • Neat solution for me. I wanted to disabled all the underneath functionalities like click event as well. Server the purpose without disabling the cursor. Great! – Dash Oct 08 '17 at 13:40
  • 3
    @Bodman pointer-events: none; Removed the on-hover background change but also disabled hyperlink from my element. How to remove hover effect but retain hyperlink? – BioDeveloper Dec 26 '17 at 05:38
  • @BioDeveloper in this scenario, you can't use pointer-events:none. You will have to use one of the other solutions. – Bodman Jan 02 '18 at 22:47
251

Use the :not pseudo-class to exclude the classes you don't want the hover to apply to:

FIDDLE

<div class="test"> blah </div>
<div class="test"> blah </div>
<div class="test nohover"> blah </div>

.test:not(.nohover):hover {  
    border: 1px solid red; 
}

This does what you want in one css rule!

Danield
  • 121,619
  • 37
  • 226
  • 255
  • This worked for me but I had to make one change, a space before the `:hover`. This may be because I am using SASS, not sure. – nurdyguy Oct 03 '16 at 21:30
  • 2
    Thanks, I used this to disable any hover effects on custom buttons for touch devices in my app by adding `touchDevice` class to `body` and changing my css rules to something like `body:not(.touchDevice) .button:hover { ... }` – Alexander Jun 23 '17 at 09:41
  • 1
    Yepp, that solution is much better. – Tsurule Vol Sep 22 '17 at 12:23
  • thank you! this should be the correct answer! – Douglas C Nov 04 '21 at 23:54
50

I would use two classes. Keep your test class and add a second class called testhover which you only add to those you want to hover - alongside the test class. This isn't directly what you asked but without more context it feels like the best solution and is possibly the cleanest and simplest way of doing it.

Example:

.test {  border: 0px; }
.testhover:hover {  border: 1px solid red; }
<div class="test"> blah </div>
<div class="test"> blah </div>
<div class="test testhover"> blah </div>
Luca Antonelli
  • 349
  • 1
  • 2
  • 21
biscuitstack
  • 11,591
  • 1
  • 26
  • 41
2

I also had this problem, my solution was to have an element above the element i dont want a hover effect on:

.no-hover {
  position: relative;
  opacity: 0.65 !important;
  display: inline-block;
}

.no-hover::before {
  content: '';
  background-color: transparent;
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 60;
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" rel="stylesheet" />

<button class="btn btn-primary">hover</button>
<span class="no-hover">
  <button class="btn btn-primary ">no hover</button>
</span>
Stem Florin
  • 896
  • 1
  • 7
  • 18
2

add a new .css class:

#test.nohover:hover { border: 0 }

and

<div id="test" class="nohover">blah</div>

The more "specific" css rule wins, so this border:0 version will override the generic one specified elsewhere.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • It will. And it will also override the default look, which is different. So there'll be always a browser in which hover will be visible. – maaartinus May 13 '14 at 20:53
-3

You want to keep the selector, so adding/removing it won't work. Instead of writing a hard and fast CSS selectors (or two), perhaps you can just use the original selector to apply new CSS rule to that element based on some criterion:

$(".test").hover(
  if(some evaluation) {
    $(this).css('border':0);
  }
);
buley
  • 28,032
  • 17
  • 85
  • 106