22

CSS:

button:active {
/* active css */
}

button:disabled {
  opacity: 0.5;
}

HTML:

<button disabled="disabled">Ok</button>

When I click the button the browser adds the button:active state making it look like it was clicked (even though it is disabled). I swear I thought :active was only added if the button was enabled. What have I missed?

4
  • in which browser have you tested it? Commented Sep 25, 2012 at 22:24
  • Here's a test page: jsfiddle.net/Blender/LRvra Commented Sep 25, 2012 at 22:25
  • Nope. It appears that :active will be applied to disabled buttons. Commented Sep 25, 2012 at 22:27
  • Actually the spec never states that a disabled control cannot be active. It only says A form control that is disabled must prevent any click events that are queued on the user interaction task source from being dispatched on the element. Commented Sep 25, 2012 at 22:27

3 Answers 3

52

From what I can tell, :active doesn't exclude :disabled elements. You can read the spec if you'd like.

To solve your problem, you could exclude :disabled elements by targeting only :enabled elements with your :active selector:

button:enabled:active {
/* active css */
}

button:disabled {
  opacity: 0.5;
}

Demo: http://jsfiddle.net/Blender/LRvra/1/

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

5 Comments

Dang; you beat me by 5 seconds.
:disabled wasn't in the CSS2.1 spec.
@BoltClock: Thanks, I've included a link to the CSS3 spec.
This appears to work in Firefox/latest Chrome. We don't support CSS2.1 anyway :P
@Justin Thomas: Ah, but you are supporting CSS2.1 when you support CSS3, since CSS3 is essentially CSS2.1 with bells and whistles.
6

You could also use the :not()-descriptor of css:

button:active:not(:disabled) {
/* active css */
}

button:disabled {
  opacity: 0.5;
}

Wish y'all the best, Patric

1 Comment

this works nicely, and same for :hover
5

According to the CSS3 specification (:disabled is not in CSS2.1) there is no mention that :active and :disabled are mutually exclusive. It's possible that this part of the specification needs clarification so UAs are free to apply the pseudo-classes in combination.

I suggest you modify your selectors to be more explicit:

button:enabled:active {
    /* active css */
}
button:disabled {
    opacity: 0.5;
}

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.