I'm wondering what the specificity of the attribute selector is. For example:
Example:
/* this specificity value is 100 + 10 + 1 = 111 */
#hello .class h2 { }
With this HTML:
<div class="selectform">
<input type="text" value="inter text">
<input type="text" value="inter text" class="inputag">
</div>
Which of these 2 selectors is more specific?
.selectform input[type="text"] { }
.selectform .inputbg { }
Check to demo http://tinkerbin.com/IaZW8jbI
According to MDN, Specificity is the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied. Simply put, if two CSS selectors apply to the same element, the one with higher specificity is used.
ID selectors: ID selectors are the most specific. These select an element based on its ID attribute and have the syntax #idname. Class selectors, attribute selectors, and pseudo-class selectors: a) Class selectors select all elements in a CSS class and have the syntax .
The :not() pseudo class does not add to the selector specificity, unlike other pseudo-classes. So the specificity of .
Attribute selectors are equally specific to class selectors.
In your example, the first selector is more specific because there is an additional type selector input
that causes it to beat the second selector.
The specificity of each selector is calculated as follows:
/* 1 class, 1 attribute, 1 type -> specificity = 0-2-1 */ .selectform input[type="text"] { } /* 2 classes -> specificity = 0-2-0 */ .selectform .inputbg { }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With