I'm trying to style a disabled input. I can use:
.myInput[disabled] { }
or
.myInput:disabled { }
Is the attribute selector the modern CSS3 way and the way to go forward? I used to use the pseudo-class, but I can't find any info on whether they are the old way and won't be supported or whether they're both equal and you can use whatever you like best.
I have no need to support older browsers (it's an intranet application), so is it:
- attribute is newer and better
- pseudo-class is still the way to go
- whichever you like best
- there's a technical reason to use one over the other
It turns out that Internet Explorer 10 and 11 fail to recognize the
:disabled
pseudoclass on some elements and only works fine with the attribute selector syntax.The code snipped above renders in IE like this:
As long as you're only styling
input
elements, you should be fine either way. Still it's a good advice to test the final result in all browsers you wish to support.No; actually, attribute selectors have been around since CSS2, and the
disabled
attribute itself has existed since HTML 4. As far as I know, the:disabled
pseudo-class was introduced in Selectors 3, which makes the pseudo-class newer.Yes, to some extent.
With an attribute selector, you're relying on the knowledge that the document you're styling makes use of a
disabled
attribute to indicate disabled fields. Theoretically, if you were styling something that wasn't HTML, disabled fields might not be represented using adisabled
attribute, e.g. it might beenabled="false"
or something like that. Even future editions of HTML could introduce new elements that make use of different attributes to represent enabled/disabled state; those elements wouldn't match the[disabled]
attribute selector.The
:disabled
pseudo-class decouples the selector from the document you're working with. The spec simply states that it targets elements that are disabled, and that whether an element is enabled, disabled, or neither, is defined by the document language instead:In other words, when you use the pseudo-class, the UA automatically figures out which elements to match based on the document you're styling, so you don't have to tell it how.
In terms of the DOM, I believe setting the
disabled
property on a DOM element also modifies the HTML element'sdisabled
attribute, which means there's no difference between either selector with DOM manipulation. I'm not sure if this is browser-dependent, but here's a fiddle that demonstrates it in the latest versions of all major browsers:You're most likely going to be styling HTML, so none of this may make any difference to you, but if browser compatibility isn't an issue I would choose
:enabled
and:disabled
over:not([disabled])
and[disabled]
simply because the pseudo-classes carry semantics that the attribute selector does not. I'm a purist like that.