What is the specificity of the attribute selector?
Asked Answered
F

3

26

I'm wondering what the specificity of the attribute selector is. For example:

  • Id = 100 points
  • Class = 10 points
  • Html Tag= 1 point

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

Foretop answered 10/7, 2012 at 5:9 Comment(4)
Specificity isn't counted in "points" the way you think it is: https://mcmap.net/q/81290/-how-are-the-points-in-css-specificity-calculatedUrease
check to demo tinkerbin.com/IaZW8jbIForetop
you might like to read the start of this article by smashing magazine which explains how specificity worksTibbs
.selectform input[type="text"] has a specificity of 2-1 (a class 1-0, a tag 1 and an attribute 1-0), while .select-form .inputbg has a specificty of 2-0 (two classes). A 2-0 can't override a 2-1. If they were equal (i.e: .selectform input.inputbg), the last one would have been applied.Emilie
U
31

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 { }
Urease answered 10/7, 2012 at 5:13 Comment(2)
Interesting, as #foo {} has a specificity of 1-0-0 while [id="foo"] {} selects the same element but has a much lower specificity of 1-0.Egocentric
@chharvey: Correct - that's because CSS itself does not have any mappings from ID/class selectors to any particular attribute, so any attribute selector will have the same specificity regardless of attribute name. This mapping is performed by the browser according to document semantics, not CSS semantics. It just so happens that attribute and class selectors share the same specificity (most likely to keep things simple).Urease
B
0

In general, all types of selectors are the same; what matters is the number of selectors in the expression. So ID = 1, class = 1, and HTML tag = 1.

In the event that two selectors have the same specificity, the one that is further down in the CSS file "wins". So make sure you order your CSS references from the general to the specific; libraries like jquery-ui.css go first, while your CSS files go after those.

Bullshit answered 10/7, 2012 at 5:23 Comment(1)
Just to add: these are mostly known as "simple selectors". In a selector, only simple selectors and pseudo-elements are considered for specificity, not combinators. In a comma-separated group, the specificity is calculated separately for each part of the selector group.Urease
P
0

As someone said earlier in this post "attribute selectors have the same specificity as classes"...i find that not to be true from my experiences... i have used a class name after say an input[type="text"] and it would not override the previous CSS. It's counter-intuitive since input[type="text"] sounds quite broad . You have to use an ID to override which if you are doing inputs for forms you should have an ID on there anyways to properly connect labels.

Priming answered 23/12, 2019 at 23:12 Comment(1)
A class selector has a 1-0 specificity, equal to an attribute selector. But input[type="text"] has a specificity of 1-1 (1-0 from the attribute and 1 from the tag), and it can't be overridden by a 1-0 specificity. To override it, you need at least 1-1 (i.e: input.your-class).Emilie

© 2022 - 2024 — McMap. All rights reserved.