Remove ':hover' CSS behavior from element

后端 未结 6 1377
别那么骄傲
别那么骄傲 2020-11-29 16:57

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

HTML:

blah

CSS:

相关标签:
6条回答
  • 2020-11-29 17:37

    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.

    0 讨论(0)
  • 2020-11-29 17:39

    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!

    0 讨论(0)
  • 2020-11-29 17:50

    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>

    0 讨论(0)
  • 2020-11-29 17:51

    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 ( 97.73% as of Aug 24,2020 )

    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">Im a red button hover over me</div>
    
    <br/>
    
    <div class="button button-green">Im a green button hover over me</div>
    
    <br/>
    
    <div class="button button-red disabled">Im a disabled red button</div>
    
    <br/>
    
    <div class="button button-green disabled">Im a disabled green button</div>

    0 讨论(0)
  • 2020-11-29 18:02

    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);
      }
    );
    
    0 讨论(0)
  • 2020-11-29 18:04

    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>

    0 讨论(0)
提交回复
热议问题