Hover and Active only when not disabled

Css

Css Problem Overview


I use hover, active and disabled to style Buttons.

But the problem is when the button is disabled the hover and active styles still applies.

How to apply hover and active only on enabled buttons?

Css Solutions


Solution 1 - Css

You can use :enabled pseudo-class, but notice IE<9 does not support it:

button:hover:enabled{
    /*your styles*/
}
button:active:enabled{
    /*your styles*/
}

Solution 2 - Css

.button:active:hover:not([disabled]) {
    /*your styles*/
}

You can try this..

Solution 3 - Css

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

button[disabled]:hover {
    background: red;
}
button:hover {
    background: lime;
}

Solution 4 - Css

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

Solution 5 - Css

Use the lightest touch: overriding via rule order.

.btn {
  /* base styles */
}

.btn:hover {
  color: red;
}

.btn:active {
  color: green;
}

.btn:disabled {
  color: #aaa;
}

The trick is the order -- apply all the non-disabled states first, makes sure they all have the same specificity, and do disabled last, with the same specificity.

This won't work for a class added to links, or non-interactive elements, which don't have the disabled property.

(Edited to remove higher-specificity rules, and messing with pointer-events)

Solution 6 - Css

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

Solution 7 - Css

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.

Solution 8 - Css

I use the one below in my project.

.bg-brand-3 {
  background-color: black;
  &[type="button"]:enabled {
    &:hover {
      background-color: orange;
    }
    &:active {
      background-color: green;
    }
  }
  &[type="submit"] {
    // css
  }
}

The :enable has the same meaning as :not(:disabled)

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
QuestionAliView Question on Stackoverflow
Solution 1 - CssEngineerView Answer on Stackoverflow
Solution 2 - CssVelayutham AnjamaniView Answer on Stackoverflow
Solution 3 - CssMadefView Answer on Stackoverflow
Solution 4 - CssfatlinesofcodeView Answer on Stackoverflow
Solution 5 - CssmichaiView Answer on Stackoverflow
Solution 6 - CssjakeforakerView Answer on Stackoverflow
Solution 7 - CsstechfoobarView Answer on Stackoverflow
Solution 8 - CssQuang DongView Answer on Stackoverflow