163

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?

6 Answers 6

324

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>

Sign up to request clarification or add additional context in comments.

8 Comments

Good answer, it's only unsupported on IE < 11, so not that much of a problem any more.
@MarkBuikema this is true. Edited the answer to reflect that. In many cases it is an added bonus.
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 .
@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 pointer-events: none; Removed the on-hover background change but also disabled hyperlink from my element. How to remove hover effect but retain hyperlink?
|
253

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!

4 Comments

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.
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 { ... }
Yepp, that solution is much better.
This should be accepted.
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>

Comments

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.

1 Comment

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

Comments

-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);
  }
);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.