Hover and Active only when not disabled

后端 未结 7 1990
长情又很酷
长情又很酷 2020-12-22 21:29

I use hover, active and disabled to style Buttons.

But the problem is when the button is disabled the hover and ac

相关标签:
7条回答
  • 2020-12-22 21:54
    .button:active:hover:not([disabled]) {
        /*your styles*/
    }
    

    You can try this..

    0 讨论(0)
  • 2020-12-22 21:59

    In sass (scss):

     button {
      color: white;
      cursor: pointer;
      border-radius: 4px;
    
      &:disabled{
        opacity: 0.4;
    
        &:hover{
          opacity: 0.4;  //this is what you want
        }
      }
    
      &:hover{
        opacity: 0.9;
      }
    }
    
    0 讨论(0)
  • 2020-12-22 21:59

    One way is to add a partcular class while disabling buttons and overriding the hover and active states for that class in css. Or removing a class when disabling and specifying the hover and active pseudo properties on that class only in css. Either way, it likely cannot be done purely with css, you'll need to use a bit of js.

    0 讨论(0)
  • 2020-12-22 22:03

    A lower-specificity approach that works in most modern browsers (IE11+, and excluding some mobile Opera & IE browsers -- http://caniuse.com/#feat=pointer-events):

    .btn {
      /* base styles */
    }
    
    .btn[disabled]
      opacity: 0.4;
      cursor: default;
      pointer-events: none;
    }
    
    .btn:hover {
      color: red;
    }
    

    The pointer-events: none rule will disable hover; you won't need to raise specificity with a .btn[disabled]:hover selector to nullify the hover style.

    (FYI, this is the simple HTML pointer-events, not the contentious abstracting-input-devices pointer-events)

    0 讨论(0)
  • 2020-12-22 22:09

    If you are using LESS or Sass, You can try this:

    .btn {
      &[disabled] {
        opacity: 0.6;
      }
      &:hover, &:active {
        &:not([disabled]) {
          background-color: darken($orange, 15);
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-22 22:10

    Why not using attribute "disabled" in css. This must works on all browsers.

    button[disabled]:hover {
        background: red;
    }
    button:hover {
        background: lime;
    }
    
    0 讨论(0)
提交回复
热议问题