So I've been trying to wrap my head around CSS specificity and it seems to me that the way the formula works it's:
10^0 * (# of element + pseudo element selectors) + 10^1 * (# of classes + attributes + pseudo class selectors) + 10^2 * (# of id selectors) + 10^3 * (inline selectors)
So, for an experiment, I created an HTML page where there are 12 nested element selectors applying to a bit of text, and one class selector. In this case, it seems like the many element selectors should override the single class selector, yet they do not. This example renders the text in red if the element selector wins, and green if the class selector wins.
What's going on? Have I misunderstood the specificity formula? Does a single class selector always win over an arbitrary number of element selectors? Is this why id selectors are considered a code smell, because they will override an arbitrary number of attribute and class selectors?
html > body > main > article > section > form > div > figcaption > div > p > span > em {
color: red;
}
html body main article section form div figcaption div p span em {
color: red;
}
.test {
color: green;
}
<!doctype html>
<html>
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/5.0.0/normalize.min.css">
</head>
<body>
<main>
<article>
<section>
<form>
<div>
<figcaption>
<div>
<p>
<span>
<em class="test">TESTING 123</em>
</span>
</p>
</div>
</figcaption>
</div>
</form>
</section>
</article>
</main>
</body>
</html>