Resolve competing rules and understand inherited values.
When a style surprises you, first decide which question you are answering:
This distinction matters. Do not compare the weight of a selector on a parent with a selector on its child: those declarations apply to different elements.
For the selectors covered so far, write specificity as three counts: ID – CLASS – TYPE. Count the parts of the matching selector, including any conditions on ancestors.
#note.:hover.p and ::before.| Selector | Specificity |
|---|---|
p | 0-0-1 |
.note | 0-1-0 |
p.note | 0-1-1 |
#note | 1-0-0 |
Compare from left to right. The first unequal column decides the winner; these are separate counts, not a single decimal score. The universal selector * and combinators add no weight.
Once the three counts are secure, these special cases are useful reference points. Some pseudo-classes take selectors inside parentheses:
:is(), :not(), and :has(): Use the highest specificity among the selectors in their argument list, without adding a CLASS count for the function itself. For example, :is(.note, #note) has specificity 1-0-0, even when an element matches only .note.:where(): Adds no specificity, including for its arguments. For example, :where(#note) p has specificity 0-0-1, contributed only by p.p.note {
color: purple;
}
.note {
color: green;
}Both rules match the same paragraph and set its color. Assume they are normal author rules in the same layer, so the earlier cascade checks are tied. Now trace the decision:
.note has 0-1-0: one CLASS count.p.note has 0-1-1: one CLASS count and one TYPE count.p.note wins and the paragraph is purple.The later .note rule cannot overturn that specificity difference. If both selectors were .note, their weights would tie and source order would decide instead. This is why you check the cascade before adding a stronger selector.
Complete this shorter trace before reading on: if you shortened the first selector from p.note to p, which rule would win? .note wins because its CLASS count is compared before p's TYPE count.
Consider a section containing text:
.article {
color: green;
border: 2px solid green;
}The section has a direct color and border. With no color declarations on the paragraph or em:
color is inherited, so green passes from the section to the paragraph p and then to the emphasized em text.border is not inherited, so it stays on the section rather than creating borders around the descendants.Inheritance follows each property's rules; it does not copy every declaration from the parent. The inherited value is only the fallback when the child has no winning value of its own.
Add this rule:
em {
color: purple;
}The emphasized text becomes purple while the rest stays green. The em rule supplies a direct value for em, so em does not need to inherit color from the paragraph. The parent's selector weight is not compared against the child's selector weight: the declarations target different elements.
If you remove the em rule, details becomes green again. That change is explained by inheritance, not by .article somehow having more specificity than em.
These keywords work with any CSS property. They are useful reference tools after you can already trace ordinary declarations and inheritance:
inherit: Use the parent's computed value, even for a property that does not normally inherit.initial: Use the property's initial value defined by CSS, rather than the parent's value or a browser's element-specific style.unset: Behave as inherit for an inherited property and initial for a non-inherited property.revert: Roll back the current cascade origin for the property so a lower-priority origin can supply the value.revert-layer: Roll back the current cascade layer for that property; a lower-priority layer, or then a lower-priority origin, can supply the value.For example, replace the direct purple color with:
em {
color: inherit;
}The emphasized text uses its parent's green color again. The CSS cascade specification defines these defaulting keywords.
Use the same short routine each time:
Try these predictions:
.note (0-1-0) and p.note (0-1-1), p.note wins because the first unequal count is the TYPE count..note, their weights tie, so source order decides instead.color: green and its child has no color of its own, the child inherits green. If the child sets color: purple, its direct value applies without comparing the parent's selector weight.border does not inherit by default.Try it yourself
HTML + CSS
inherit, initial, unset, revert, and revert-layer let you choose where a value comes from.