Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the specificity of the attribute selector?

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

like image 221
Rohit Azad Malik Avatar asked Jul 10 '12 05:07

Rohit Azad Malik


People also ask

What is the specificity of selector?

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.

What CSS selector is the most specific?

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 .

Which selector does not increase specificity?

The :not() pseudo class does not add to the selector specificity, unlike other pseudo-classes. So the specificity of .


Video Answer


1 Answers

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 { } 
like image 189
BoltClock Avatar answered Sep 23 '22 16:09

BoltClock