I'm running loops between my understanding of specificity and targeting an element. I understand the base concept of specificity and read numerous articles on how to calculate specificity using a,b,c,d, where d is the lowest specificity and a is the highest.
I'm losing scope of where specificity comes into practice. In the snippet below, even though some specificity rules are "higher" than others, lower specificities are applied, e.g.
/* 0001 */
span {
color: red;
}
/* 0001 */
p {
color: blue;
}
/* 0010 */
.main {
color: orange;
}
/* 0100 */
#h4-id {
color: limegreen;
}
/* 0002 */
div h4 {
color: purple;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body class='main'>
<h3>this should be orange</h3>
<div id='h4-id'>
<h4>ID Selector</h4>
</div>
<p>
text inside paragraph
<span>hello world</span>
</p>
</body>
</html>
There is a div
with the ID h4-id
. The rule sets all text to limegreen.
There is another selector with lower specificity div h4
, this rule "wins" and the declarations in that rule is applied.
I believe the reason is that it is a "direct" target.
Here is another example:
/* 0002 */
p span {
/* font-size: 200px; */
text-decoration: underline dotted red;
font-style: normal;
color: teal;
background-color: yellow;
}
/* 0001 */
span {
background-color: teal;
}
/* 0010 */
.main {
font-family: monospace;
color: blue;
font-size: 22px;
background-color: lightgrey;
}
/* 0001 */
p {
font-family: cursive;
color: indigo;
font-size: 100px;
background-color: limegreen;
font-style: italic;
text-decoration: underline;
<p class='main'>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do <span>eiusmod tempor incididunt</span> ut Tristique magna sit amet purus gravida quis blandit turpis. Tellus elementum sagittis vitae et. Faucibus pulvinar <span>elementum integer enim neque</span> volutpat ac tincidunt vitae. At tempor commodo ullamcorper a.
</p>https://stackoverflow.com/questions/ask#
In this example, once again, the lower specificity wins, but these are "direct targets" (I believe), which is why they win. The class "main" does not have higher priority over the descendant selector p span
.
- the
.main
class score is 10, all the text inside it should have the ruleset applied unless there is an ID - the
p span
selector is 2, the reason this wins is that it is directly targeted (even though the specificity score is lower? this is where my confusion is).
Here is one of the many types of articles and references I've been reading to solidify my knowledge of specificity
Is it correct to say, before applying specificity rules and calculating specificity, if the element is directly targeted, specificity rules do not apply and the direct target always takes precedence(like the examples I posted)? If this is the case, just for my own mental model, I'd prefer using the CSS diagram to calculate specificity as so:
false, 0, 0, 0, 0
(false means it is not a direct target, this could always be a 1 or 0 as well). Is this valid reasoning?