Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unusual CSS specificity behaviour

I've worked with CSS for ages and I thought I understood specificity well, but this example baffles me; it's late at night so I might be missing something obvious:

.class span {
  color: blue;
}

section#id {
  color: beige;
}
<div class="class">
  <section id="id">
    <span>Test</span>
  </section>
</div>

Specificity for section#id is 101, while for .class span is 11 and, on top of that, the second rule is even specified after the first one.

What obvious thing am I missing?

like image 305
NinGen ShinRa Avatar asked Jun 22 '26 20:06

NinGen ShinRa


2 Answers

The first rule is narrowed to the span tag. The second rule is a higher level, at the parent section. So, yes, .class span will take precedence because it's hitting the actual tag.

like image 198
AlienWebguy Avatar answered Jun 25 '26 11:06

AlienWebguy


You aren't targeting the span with the second selector. The color will only cascade to elements where the color property is set to inherit (default).

like image 27
Christian Avatar answered Jun 25 '26 10:06

Christian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!