Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Disabled Button getting :active CSS Pseudo Class

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?

like image 374
Justin Thomas Avatar asked Sep 25 '12 22:09

Justin Thomas


3 Answers

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/

like image 200
Blender Avatar answered Sep 25 '22 11:09

Blender


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;
}
like image 25
Dai Avatar answered Sep 25 '22 11:09

Dai


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

like image 36
Patric S. Avatar answered Sep 24 '22 11:09

Patric S.