Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why CSS direct child selector applies style to indirect children

Tags:

html

css

I have the following html code:

<div id="test">
<span>
    test 
    <span>test</span>
</span>        
</div>  

And the following CSS code:

#test > span {
color: red;
}

Isn't the above code supposed to select only the direct child span of test div? So only the first 'test' word should be red but the second 'test' word inside the child span should not be selected. Or I'm getting it all wrong??

like image 766
Michael Samuel Avatar asked Dec 26 '22 02:12

Michael Samuel


1 Answers

All else being equal, the default style for the span will be:

span { color: inherit; }

So, while the rule color: red; won't apply to it directly, it will take its color from the parent element's color.

For comparison, see what happens if you explicitly say span { color: blue; }.

like image 115
Quentin Avatar answered Jan 14 '23 15:01

Quentin