0

I have html like:

header {
  width: 100%;
  height: 50px;
}

a {
  color: red;
}

.my-a {
  color: green;
}
<header class="my-header">
  <div>
    <a class="my-a" href="https://www.google.com">clickme</a>
  </div>
</header>

Then 'clickme' will be green as expected. And it follows the rule that class selectors has a greater specificity than type selectors.

But when css becomes:

header {
  width: 100%;
  height: 50px;
}

.my-header a {
  color: red;
}

.my-a {
  color: green;
}
<header class="my-header">
  <div>
    <a class="my-a" href="https://www.google.com">clickme</a>
  </div>
</header>

The result shows that 'clickme' is red which is out of my expectation. I cannot find any corresponding rule in MDN specificity.

Am i missing any rule here? JSFiddle

Turnip
  • 35,836
  • 15
  • 89
  • 111
39ecneret
  • 661
  • 3
  • 7
  • 17

1 Answers1

6

.my-header a is a type selector AND a class selector which is more specific than .my-a which is a class selector and nothing else.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I'm confused that MDN says that universal selector (*), combinators (+, >, ~, ' ') and negation pseudo-class (:not()) have no effect on specificity. What does it actually mean by saying combinators have no effect on specificity? – 39ecneret Sep 28 '18 at 10:04
  • @39ecneret — `.my-header a` / `.my-header + a` / `.my-header :not(a)` / `:not(.my-header) > a` — all have a class selector and a type selector and have the same specificity. The combinators don't change the specificity. Just the selectors. – Quentin Sep 28 '18 at 10:08