20

I have this CSS:

html.darkBlue .btn1 button:hover:not(.nohover) {
  background: #0007d5;
  border: 1px solid #0007d5;
  color: white;
}

I am rather confused about disabled. How can I make it so that this CSS does not work if the button is disabled?

0

6 Answers 6

40

If you don't need to support IE < 9 you could use the :enabled pseudo class.

html.darkBlue .btn1 button:hover:enabled {
    background: #0007d5;
    border: 1px solid #0007d5;
    color: white;
}
Sign up to request clarification or add additional context in comments.

2 Comments

For folks concerned with IE < 9 support, :disabled has the same browser compatibility than :enabled.
For folks concerned with IE < 9 support, :disabled has the same browser compatibility than :enabled.
3

It worked for me after adding the ":enabled" selector as following :

element-class:hover:enabled {
  properties...
}

Comments

2

Try with (demo http://jsfiddle.net/JDNLk/)

HTML

<button class="btn1" disabled="disabled">disabled</button>
<button class="btn1">enabled</button>

CSS

html.darkBlue .btn1 button:hover:not([enabled="enabled"]) {
  background: #0007d5;
  border: 1px solid #0007d5;
  color: white;   
}

Comments

2

You can use the negation pseudo class selector (CSS 3). I am not sure if there is also a solution using attribute selectors (CSS 2.1).

Given this html

  <div class="darkBlue">
    <h2>disable hover for disabled buttons</h2>
    <div class="btn2">
      <button>hover enabled</button>    <br/>     
      <button disabled="disabled">hover disabled</button>         
    </div>  
  </div>

and this css

.darkBlue .btn2 button:hover:not([disabled="disabled"]) {
  background: #0007d5;
  border: 1px solid #0007d5;
  color: white;   
}

you can achive that every button inside the matiching selector has no hover-style applied. See this example.

At caniuse.com you can find tables that compare which browser supports which selector

Update using a hack to be able to use css2 selectors

This is a hack and is yet not exactly the same but in case you are restricted to css 2.1 this may be a starting point. If you define a seperate style-rule for disabled buttons and use the color that you picked for disabled buttons you can fake a disabled hover-style:

.btn3 button[disabled="disabled"]:hover
{
  background-color: rgb(212, 208, 200);
  color: rgb(128, 128, 128);  
}

Comments

1

Use the CSS :Not attribute selector to only target elements that don't have the disabled attribute.

html.darkBlue .btn1 button:not([disabled]):hover

That way your hover style will only be applied to buttons that are not disabled.

Comments

0
.btn:disabled:hover{color:default-color;background:default prop}

. or .btn:disabled{pointer-events:none}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.