I have the following HTML & CSS:
<div class="p-12 border both-have">
<div class="p-12 border both-have my-inner">
@import "tailwindcss";
.both-have, .foo.bar {
& .my-inner {
background-color: red;
}
}
.both-have {
& .my-inner {
background-color: aqua;
}
}
I can't see how the background-color isn't set to aqua, but when inspecting in the browser, the specificity is (0,2,1) for the applied CSS. However, the .foo.bar class doesn't match anything here. Why would non-matching CSS still contribute to the specificity of a selector? Is this normal?
I know I can "fix" this issue in a number of different ways, more just curious about this seemingly odd behavior.
Note: only using Tailwind since they have a nice playground that doesn't require logging in & it was quick to apply some padding so i could showcase the background-color)
It might not last forever, but here is a tailwind playground link: https://play.tailwindcss.com/wZUsI94YPg?file=css, try removing the .bar from the first selector and see how it affects the background-color being applied.
Tried to investigate a bit, maybe it's part of the CSS spec, but it didn't make much sense, i read https://www.w3.org/TR/selectors-4/#specificity-rules, and it states the following:
If the selector is a selector list, this number is calculated for each selector in the list. For a given matching process against the list, the specificity in effect is that of the most specific selector in the list that matches.
This lead me to think that it's not intended behaviour since the selector .foo.bar doesn't match? (maybe it's different for when a selector list has nesting inside it?)
my-inner, then it checks if this element also has the classboth-haveorfooandbar. It does not check