Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How element selector is more specific than id selector?

As I understand elements are least specific. (element vs id). Please help me in understanding the specificity of selectors generally.

<div id="container">
    <div id="firstDiv">FIRST Div inside CONTAINER</div>
    <div id="secondDiv">SECOND Div inside CONTAINER</div>
</div>
body{
    width: 780px;
    margin: 20px auto;
}
#container > div:not(:last-of-type){
    margin-bottom: 0px; /*How its more specific than ID selector below? */
}
#container {
    border: 15px solid orange;
    padding: 10px;
}
#firstDiv{
    margin: 50px; /*This is not taking effect*/
    border: 5px solid blueviolet;
}
#secondDiv{
    border: 5px solid brown;    
}

http://jsfiddle.net/t2RRq/

like image 245
Shawn Taylor Avatar asked Feb 16 '12 12:02

Shawn Taylor


People also ask

Which is more specific as a selector class or id?

CSS Specificity Rules Say an element is targeted by an ID selector and a class selector. In that case, because the ID selector has the higher specificity, the CSS style of the ID selector will be applied to the element over the style of the class selector.

Which selector has the most specificity?

ID selectors are used to target an element using the element's ID. ID selectors have higher specificity than classes and elements.

Which CSS rule has the most specificity?

Inline styles added to an element (e.g., style="font-weight: bold;" ) always overwrite any normal styles in author stylesheets, and therefore, can be thought of as having the highest specificity.

Which CSS selector has the highest precedence?

1) Inline style: Inline style has highest priority among all. 2) Id Selector: It has second highest priority. 3) Classes, pseudo-classes and attributes: These selectors has lowest priority.


2 Answers

To understand CSS specificity, read The Specificity Wars. There’s a handy reference sheet, too:

So, a selector like #foo would have 1,0,0 specificity, while an element selector like p would have 0,0,1 specificity. Out of these two, the ID selector would “win” as 100 is greater than 1.

A more specific (heh) version which also includes pseudo-elements and pseudo-classes can be found here: http://www.standardista.com/css3/css-specificity/

like image 175
Mathias Bynens Avatar answered Oct 20 '22 04:10

Mathias Bynens


When applying rules, selector specificity is calculated by counting all simple selectors (linked by any combinators), and not just the key selector. That means you're not just comparing these two selectors:

div
#firstDiv

But you're comparing these two selectors:

#container > div:not(:last-of-type)
#firstDiv

Here, the first selector is more specific because it has:

  • An ID selector, #container

  • A type (element) selector, div; and

  • A pseudo-class, which in this case is :last-of-type; the :not() pseudo-class itself doesn't count toward selector specificity

Whereas the second selector only consists of an ID. Note that combinators themselves like > in your first example don't count toward selector specificity.

There is an entire section in the Selectors spec covering how to calculate selector specificity.

like image 2
BoltClock Avatar answered Oct 20 '22 02:10

BoltClock