Is the attribute a selector of the modern CSS3 way and way forward?
- Attribute
- is newer and better
No; in fact, attribute selectors were around with CSS2 , and the disabled attribute itself existed with HTML 4 . As far as I know, the :disabled class pseudo-class was introduced in Selectors 3 , which makes the pseudo-class more recent.
- there is a technical reason to use one over the other
Yes, to some extent.
With the attribute selector, you rely on knowing that in the document style you use the disabled attribute to indicate disabled fields. Theoretically, if you were developing something that wasn't HTML, disabled fields might not display using the disabled attribute, for example. it could be enabled="false" or something like that. Even future releases of HTML may introduce new elements that use different attributes to represent on / off status; these elements will not match the [disabled] attribute selector.
The :disabled pseudo- :disabled separates the selector from the document you are working with. The specification simply states that it targets elements that are disabled, and whether the element is enabled, disabled or not, defined by the language of the document :
What constitutes an on state, an off state, and a user interface element is language dependent. In a typical document, most elements will be neither :enabled nor :disabled .
In other words, when you use a pseudo-class, UA automatically determines which elements should match based on your style, so you don't need to talk about it.
In terms of the DOM, I believe that setting the disabled property on the DOM element also changes the HTML attribute disabled , which means there is no difference between the selector with the DOM manipulation. I'm not sure if this depends on the browser, but here is a fiddle that shows it in the latest versions of all major browsers:
// The following statement removes the disabled attribute from the first input document.querySelector('input:first-child').disabled = false;
Most likely, you will develop HTML code, so none of this can affect you, but if browser compatibility is not a problem, I would choose :enabled and :disabled over :not([disabled]) and [disabled] just because pseudo-classes carry semantics that are not in the attribute selector. I'm such a purist.