Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the CSS direct decendant (>) not have any value in selectivity?

Given the following class declarations and code...

.foo > a { color:green; }
.bar a { color:red; }
<div class="bar">
    <div class="foo">
        <a href="#">SOME LINK</a>
    </div>
</div>

... I thought that the link would be green because, while both declarations have a class (010) and an element (001), .foo has the direct descendant selector. But alas, the link is red. Why?

like image 269
oucil Avatar asked Nov 29 '14 14:11

oucil


People also ask

How do I select direct descendants in CSS?

The child combinator ( > ) is placed between two CSS selectors. It matches only those elements matched by the second selector that are the direct children of elements matched by the first. Elements matched by the second selector must be the immediate children of the elements matched by the first selector.

Which CSS property values has the highest specificity?

Inline styles have the highest specificity. In our specificity weight system, they have a value of 1000. Let's try to make sense of it. The property values of selectors with a higher weight will always be applied over a selector with a lower weight.

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.


3 Answers

There's no value for > for css specificity.

Both case have 11 value for specificity:

.foo > a { color:green; }/*specificity value is 11*/
.bar a { color:red; }/*specificity value is 11*/

In your case you may use like this to have greater specificty:

.bar .foo > a { color:green; }/*greater specificity value is 21*/
.foo a { color:red; }/*specificity value is 11*/

Ok, I'm going add here how specificity works:

Selector                          Specificity         Specificity in large base
inline-style                      1 0 0 0             1000
id selector                       0 1 0 0              100
class,pseudo,attribute selector   0 0 1 0               10
type selector and pseudo elements 0 0 0 1                1                     
like image 174
Bhojendra Rauniyar Avatar answered Oct 08 '22 10:10

Bhojendra Rauniyar


You have two conflicting CSS rules. Direct descendant selector has the same specificity as descendant selector if I am not mistaken, therefore the rule parsed later will override the formerly parsed rule, so if your rules are:

.foo > a { color:green; }
.bar a { color:red; }

then the color will be red. If your rules are:

.bar a { color:red; }
.foo > a { color:green; }

then the color will be green for any anchors which fulfill the selector of both rules.

like image 32
Lajos Arpad Avatar answered Oct 08 '22 09:10

Lajos Arpad


Combinators don't have any value in specificity only selectors have :id, class, tags , pseudo elements , pseudo classes, attribute selectors.

like image 45
marius Avatar answered Oct 08 '22 08:10

marius