We are going to explain CSS specificity, which is the rules that the browser uses to determine which CSS property values are applied to an element when multiple rules could apply.
Specificity is a weight that is applied to a given CSS declaration, determined by the number of each selector type in the matching selector. When multiple declarations have equal specificity, the last one found in the CSS is applied. However, if one has higher specificity, it will be applied regardless of order.
The specificity is usually represented as a 4-part value: (a, b, c, d)
a: Inline styles (style attribute) - this is the highest and has a value of 1 for each inline style, and 0 otherwise in this representation.
b: Number of ID selectors in the selector.
c: Number of class selectors, attribute selectors, and pseudo-classes in the selector.
d: Number of type selectors (element names) and pseudo-elements in the selector.
We can also think of it as a single number by concatenating these values (like 0,1,0,0 is higher than 0,0,10,0 because 1 in the b column beats 10 in the c column). But note that in the actual calculation, each column is independent and compared from left to right.
Example:
Inline style: 1,0,0,0
ID selector: 0,1,0,0
Class selector: 0,0,1,0
Element selector: 0,0,0,1
So, if we have:
p { color: red; } -> 0,0,0,1
.class { color: blue; } -> 0,0,1,0
#id { color: green; } -> 0,1,0,0
The order of specificity (from highest to lowest) would be:
Inline style (yellow)
ID selector (green)
Class selector (blue)
Element selector (red)
But note that if we have a selector with multiple parts, we add them up.
Example:
#nav .item a:hover { ... }
1 ID (#nav) -> b=1
1 class (.item) -> c=1
1 element (a) -> d=1
1 pseudo-class (:hover) -> c increases by 1? Actually, note that :hover is a pseudo-class, so it counts as a class for specificity.
So: b=1, c=2 (one class and one pseudo-class), d=1 -> (0,1,2,1)
Let's break down:
ID: #nav -> 1
Class: .item -> 1
Pseudo-class: :hover -> 1
Element: a -> 1
So the specificity is (0,1,2,1)
Now, if we have another rule:
#nav a { ... } -> (0,1,0,1) [1 ID, 1 element]
Which one is higher? (0,1,2,1) vs (0,1,0,1):
Compare a: both 0.
Compare b: both 1.
Compare c: 2 vs 0 -> so (0,1,2,1) wins.
Important note: The universal selector (*), combinators (+, >, ~, ' '), and negation pseudo-class (:not()) do not add to specificity. However, the selectors inside :not() do. For example, :not(.class) has the same specificity as .class (because the class inside not is counted).
Example:
{ color: gray; } -> specificity 0,0,0,0 (it's the lowest, but note that it doesn't compete with any other selector because it's universal and only applies if no other rule applies? Actually, it does compete but with 0 specificity, so any other selector will override it unless they are also 0 and then order matters.)
Also, note that the !important rule overrides everything except another !important with higher specificity. But using !important is generally discouraged because it breaks the natural cascade.
Let's summarize the steps:
Count the number of ID selectors in the selector (b).
Count the number of class selectors, attribute selectors, and pseudo-classes in the selector (c).
Count the number of type selectors and pseudo-elements in the selector (d).
Inline styles (style attribute) are considered to have a specificity of 1,0,0,0 (so they are the highest, unless there is an !important in a rule with higher specificity? Actually, inline style with !important is still 1,0,0,0 but then we compare with other !important rules? The !important flag doesn't change the specificity value, but it changes the order of application. The cascade order is: importance (important declarations) then specificity, then source order.
But note: an !important declaration without an inline style can override an inline style without !important? Actually, the cascade order:
First, find all declarations that apply to the element and property.
Sort by importance: declarations with !important are sorted above those without.
Then, within each importance level, sort by specificity.
Then, within the same specificity, by source order (later overrides earlier).
So, an important rule with lower specificity can override an inline style without important? Let's see:
Example:
CSS:
div { color: blue !important; }
The div rule is important and has specificity (0,0,0,1). The inline style is (1,0,0,0). But the important rule is in the important layer, and the inline style is not important. So the important rule wins because it's in the important layer, even though its specificity is lower?
Actually, the cascade order is:
Find all declarations that apply to the element for the given property.
Sort by importance (important vs not important). Important declarations are considered first.
Then, within the important declarations, sort by specificity.
Then, within the same specificity, by source order.
So in the example above, the important declaration (div with blue) is in the important layer and has specificity (0,0,0,1). The inline style (red) is in the non-important layer. Since the important layer is considered first, the blue will be applied.
But if we have:
CSS:
div { color: blue !important; }
Now both are important. Then we compare specificity: (1,0,0,0) vs (0,0,0,1). The inline style wins because 1,0,0,0 is higher.
So, the presence of !important changes the layer, but within the same layer (important or not) we use specificity.
However, note that the inline style (with or without important) is considered to have a specificity of 1,0,0,0. But in the case of the inline style without important, it is in the non-important layer and can be overridden by an important rule with lower specificity.
So, to summarize the cascade order:
Importance (important vs normal)
Specificity (within the same importance)
Source order (within the same importance and specificity)
But note: the inline style is just a declaration with a specificity of 1,0,0,0. It doesn't have a special layer unless it's marked important.
Now, let's talk about the universal selector and combinators:
The universal selector (*) has no specificity (0,0,0,0).
Combinators (+, >, ~, ' ') do not add to specificity. They are just relationships.
Example:
div > p { color: red; } -> 0,0,0,2 (two element selectors: div and p)
Also, note that the :not() pseudo-class does not add to specificity, but the selector inside it does.
Example:
div:not(.class) { color: red; }
div: 1 element -> d=1
:not(.class) -> the .class inside adds 1 to c (class) -> c=1
So total (0,0,1,1)
But if we had: :not(#id) then the ID inside would count as an ID.
Example:
div:not(#id) { ... } -> 0,1,0,1
Now, let's look at a more complex example:
nav#nav ul li.current a { color: red; }
Breakdown:
ID: #nav -> 1 -> b=1
Classes: .current -> 1 -> c=1
Elements: nav, ul, li, a -> 4 -> d=4
So specificity: (0,1,1,4)
Another example with attribute selectors:
input[type="text"]:focus { ... }
Attribute selector: [type="text"] -> counts as a class -> c=1
Pseudo-class: :focus -> counts as a class -> c=2? Wait, note: both attribute and pseudo-class are in the c column.
Element: input -> d=1
So: (0,0,2,1)
Now, let's consider the case of multiple selectors in one rule:
.a, .b { color: red; }
This rule has two selectors. Both selectors have the same specificity? Actually, we calculate the specificity for each selector separately. Then, when applying, we take the highest specificity among the selectors in that rule?
No, the rule as a whole doesn't have a single specificity. Instead, each selector in the group is considered independently. So if one of the selectors has higher specificity, then for elements matching that selector, the rule will have that higher specificity.
But note: when we have a rule with a selector list, the specificity of the rule for a given element is the specificity of the selector that matched the element. So if an element matches two selectors in the same rule, then we take the higher of the two? Actually, the same rule, so the same declaration. But the specificity for that element is the specificity of the selector that matched. However, if the element matches multiple selectors in the same rule, we don't add the specificities, we take the maximum of the specificities of the selectors that matched.
But in practice, when we write a rule with a selector list, we are essentially writing two separate rules with the same declarations. So the specificity for an element matching .a is (0,0,1,0) and for .b is (0,0,1,0). So same.
But if we have:
.a, #b { color: red; }
Then for an element with class "a" and id "b", it matches both selectors. But the rule is the same. The specificity for that element when considering this rule would be the maximum of the two selectors that matched? Actually, the rule doesn't have a single specificity. The element will get the declaration from this rule, and the specificity of that declaration for that element is the specificity of the selector that matched. But note: the element matches two selectors in the same rule. We don't add them. The specificity is the one of the selector that matched? But both matched. Then we take the higher?
The spec says: "The specificity of a selector is calculated for a given element as follows: count the number of ID selectors in the selector (= b), the number of class selectors, attribute selectors, and pseudo-classes in the selector (= c), and the number of type selectors and pseudo-elements in the selector (= d)."
For a rule with a selector list, each selector is considered separately. So for a given element, we look at each selector in the list and if it matches, we use that selector's specificity. But note that the same declaration block is applied. So the element gets the declaration from the rule, and the specificity of that declaration for that element is the specificity of the selector that matched. However, if multiple selectors in the same rule match the same element, then we have multiple ways to match, but the declaration is the same. The specificity for that element from that rule is the maximum of the specificities of the matching selectors?
Actually, the cascade doesn't work by having a single specificity for a rule. Instead, for each element and each property, we look at all the declarations that apply. Each declaration comes from a rule, and that rule has a selector. The specificity of that declaration is the specificity of the selector that matched the element. So if a rule has a selector list and an element matches two of them, then that rule contributes two declarations? No, it's the same declaration. But the specificity of that declaration for that element is the specificity of the selector that matched? But which one?
The spec says: "A selector’s specificity is calculated for a given element as follows: ..." So for a given element and a given rule, we calculate the specificity of the selector that matched the element. But if the rule has a selector list, then for the element we find the selector in the list that matches and use its specificity. However, if multiple selectors in the list match, we use the one with the highest specificity?
Actually, the cascade algorithm doesn't require us to choose one selector for the rule. Instead, for each declaration (which comes from a rule and a property), we assign a specificity that is the specificity of the selector that matched the element. But note: a rule with a selector list is equivalent to multiple rules with the same declarations. So if an element matches two selectors in the same rule, it's as if there were two separate rules (with the same declarations) and the element gets two declarations that are identical. Then, when we sort by specificity, we take the one with the higher specificity? But they are the same declaration, so it doesn't matter.
However, the problem is: what is the specificity of the declaration from that rule for that element? It's the specificity of the selector that matched. But if two matched, then we have two declarations with the same value but possibly different specificities? Then the one with the higher specificity would win, but they are the same value so it doesn't matter.
But in practice, we don't have to worry because the declaration is the same. However, if the rule had !important, then both would be important and then we compare the specificities of the selectors that matched? Again, same declaration.
So, in summary, when a rule has a selector list, for each element that matches, we consider the specificity of the selector in the list that matched the element. If multiple selectors match, we take the maximum of the specificities of those selectors? Actually, the cascade will consider each declaration separately? But it's the same declaration from the same rule. The spec doesn't explicitly say, but the common understanding is that the specificity of a rule for an element is the specificity of the selector that matched the element. If multiple selectors in the rule match the element, then the rule's declaration for that element has the specificity of the most specific selector that matched.
But note: the rule is one declaration block. So we don't duplicate the declaration. So the element gets the declaration from the rule, and the specificity is the maximum of the specificities of the selectors in the rule that match the element.
Example:
.a, #b { color: red; }
For an element that has both class "a" and id "b", it matches both selectors. The specificity of .a is (0,0,1,0) and #b is (0,1,0,0). Then the declaration from this rule for that element would have specificity (0,1,0,0) because that's the highest among the matching selectors.
But note: if we have two separate rules:
.a { color: red; }
#b { color: red; }
And the element has both, then we have two declarations. Then we compare the specificities and the #b wins. So it's the same.
So, in the case of a selector list, we can think of it as the rule having the highest specificity among the selectors that match the element.
But note: when we write the rule, we don't know which element will match which selector. So for the cascade, we have to consider each element separately.
However, in practice, when we calculate the specificity of a rule, we often calculate for each selector in the list and then say the rule's specificity is the highest among the selectors? But that's not accurate because an element might only match one of the selectors and then the rule's specificity for that element is that of the matching selector.
But for the purpose of understanding, we can calculate the specificity of each selector in the rule and then when comparing two rules, we compare the specificities of the selectors that matched the element.
But note: when two rules have the same specificity and apply to the same element, then the one that comes later in the CSS wins.
The element matches both. The blue wins because of higher specificity.
Now, with a combined rule:
.a, #b { color: green; }
This rule has two selectors. The element matches both. The rule's declaration for the element has specificity (0,1,0,0) because the highest among the matching selectors.
Now, if we have:
.a { color: red; }
.a, #b { color: green; }
The element matches both rules. The first rule: (0,0,1,0)
The second rule: for the element, it matches .a and #b, so the highest is (0,1,0,0).
So the second rule wins because (0,1,0,0) > (0,0,1,0).
But note: the second rule also has the same declaration (green) for both selectors. So it's consistent.
What if the order is reversed?
.a, #b { color: green; }
.a { color: red; }
Now, the first rule has (0,1,0,0) for the element (because it matches #b) and the second rule has (0,0,1,0). So the first rule wins.
But note: the second rule only matches .a, and the element has class "a", so it matches. But the first rule matches with higher specificity.
So, the order of the rules doesn't matter when the specificities are different? Actually, the cascade order is: first by importance, then by specificity, then by source order. So if two declarations have different specificity, the one with higher specificity wins regardless of order.
In the example above, the first rule has higher specificity, so it wins even though it comes first.
But if we had:
.a { color: red; }
.a { color: green; }
Then they have the same specificity, and the second one wins because it comes later.
So, to recap:
Calculate specificity for each declaration (which comes from a rule and matches the element). The specificity is based on the selector that matched the element. If a rule has a selector list, then for the element, we take the maximum specificity among the selectors in the rule that match the element.
Then, we sort the declarations by:
a. Importance (important vs normal)
b. Specificity (higher wins)
c. Source order (later wins if same importance and specificity)
But note: the universal selector and combinators do not add to specificity.
Let's look at an example with a combinator:
div > p { color: red; }
This has two element selectors, so specificity (0,0,0,2).
Now, if we have:
div p { color: blue; }
This also has two element selectors, so same specificity (0,0,0,2). Then source order decides.
But note that the first one (child combinator) is more specific in terms of relationship, but in CSS specificity, the combinator doesn't count. So they have the same specificity.
Now, what about the :not() pseudo-class? It doesn't add to specificity, but the argument does.
Example:
div:not(.class) { color: red; }
Breakdown:
div: element -> d=1
:not(.class) -> the .class inside adds 1 to c -> c=1
So (0,0,1,1)
But if we had:
div:not(#id) { ... } -> (0,1,0,1)
Now, let's consider the case of multiple :not():
div:not(.a):not(.b) { ... }
div: d=1
first :not(.a): c increases by 1 (for .a) -> c=1
second :not(.b): c increases by 1 (for .b) -> c=2
So (0,0,2,1)
But note: the :not() pseudo-class itself doesn't count, only the selectors inside.
Now, let's talk about the overall concept:
CSS specificity is a key part of the cascade. It helps resolve conflicts when multiple rules apply to the same element.
The idea is that more specific selectors (like IDs) should override less specific ones (like element selectors).
But note: specificity is not about the "importance" of the style, but about the specificity of the selector.
Also, note that the specificity is calculated only for the selector that matches the element. So if a rule has a selector that doesn't match the element, then that rule doesn't apply at all.
Therefore, when we write CSS, we should be aware of specificity to avoid unexpected overrides.
Common advice: use classes for styling and avoid IDs for styling because IDs have high specificity and are hard to override. Also, avoid using inline styles because they have the highest specificity (unless with !important, but then you have to use !important to override).
But note: sometimes we need to override a style, and we can increase specificity by adding more selectors (like making it more specific by adding a parent element) or by using !important (but use sparingly).
Example of increasing specificity:
.button { background: blue; }
.special .button { background: red; } /* more specific because it has two classes? Actually, one class and one element? Wait, it's two classes? No, .special is a class and .button is a class? Actually, it's two class selectors? Let's break down:
.special .button ->
.special: class -> c=1
.button: class -> c=2
No element selectors? Actually, there is an implicit combinator (space) but no element selector. So d=0.
So (0,0,2,0)
Which is higher than (0,0,1,0) for .button.
But note: if we had:
.button { ... }
div .button { ... }
Then:
.button: (0,0,1,0)
div .button: (0,0,1,1) because div is an element (d=1) and .button is a class (c=1). So (0,0,1,1) is higher than (0,0,1,0) because we compare c first? Actually, we compare a, then b, then c, then d. So (0,0,1,1) has the same c (1) and then d=1 vs 0, so (0,0,1,1) wins.
But note: the selector "div .button" has two simple selectors: one element (div) and one class (.button). The space is a combinator and doesn't count.
So, to increase specificity, we can add more classes or IDs or element selectors, but note that adding element selectors only increases the d column, which is the least significant.
So, the best way to increase specificity is to add IDs (b) or classes (c). But adding too many can lead to overly specific selectors that are hard to maintain.
Now, let's look at the actual representation: we usually write specificity as (a,b,c,d) but sometimes we see it as a single number by concatenating: a1000 + b100 + c10 + d. But note: this works only if we assume that each column is a single digit? Actually, the columns can be more than 9. But in practice, we rarely have more than 9 in any column? Not necessarily, we can have more than 9 element selectors? But then the concatenation would break because 10 in the c column would be 1010=100, but if we have 10 classes and 1 ID, then:
So they would be equal? But they are not because the ID (b=1) should beat 20 classes (c=20). So the concatenation method fails when any column exceeds 9.
But in reality, we rarely have more than 9 of any type in a selector. However, it's not guaranteed. So the proper way is to compare as 4 separate numbers, from left to right.
In practice, we can think of it as:
Inline styles (a) are the most important, then IDs (b), then classes (c), then elements (d).
So, when comparing two specificities:
If a is different, the one with higher a wins.
Else, if b is different, the one with higher b wins.
Else, if c is different, the one with higher c wins.
Else, if d is different, the one with higher d wins.
Else, same specificity, then source order.
Now, let's consider the case of the same element being matched by multiple rules. We take the declaration with the highest specificity. If same, then the later one.
But note: the same rule can have multiple declarations for the same property? No, each rule has one declaration block, but it can have multiple properties. But for a given property, if the same rule has multiple declarations for the same property (which is invalid and the last one in the rule wins? Actually, in a single rule, if you write the same property twice, the last one wins, but that's within the rule. Then that declaration has the specificity of the rule's selector.
But note: the cascade is about multiple rules. Within a rule, the normal CSS parsing applies (the last property with the same name wins). Then the resulting declaration block has one value per property. Then that declaration (from the rule) has the specificity of the rule's selector.
So, to summarize:
Each element has a set of declarations that apply to it for each property.
Each declaration comes from a rule and has a specificity (based on the selector that matched the element).
We sort the declarations by importance (important vs normal), then by specificity (higher wins), then by source order (later wins).
Compare:
rule 3 and 4 have b=1, which is higher than rule 1 and 2.
Between rule 3 and 4:
rule 3: (0,1,0,0)
rule 4: (0,1,0,1) -> same b and c, but d=1 > 0, so rule 4 wins.
So the color will be yellow.
But note: rule 4 has the same ID as rule 3, but also adds the element selector. So it's more specific.
Now, what if we add an inline style?
Now, we have an inline style: (1,0,0,0). This is higher than any of the above. So orange.
But if the inline style had !important, then it would be (1,0,0,0) in the important layer. But if we had an important rule with higher specificity? Actually, the inline style with important is (1,0,0,0) and any other important rule with lower specificity (like (0,1,0,0)) would lose. But if we had an important rule with (1,0,0,0) or higher? Actually, we cannot have higher than (1,0,0,0) because a can only be 0 or 1. So an inline style with important is the highest.
But note: the a column is only for inline styles. So a rule with an ID and 10 classes and 10 elements is still (0,1,10,10) which is less than (1,0,0,0).
So, in summary:
Inline styles (with or without important) have the highest specificity in their layer.
Within the same layer, we compare b, c, d.
Now, let's consider the case of the same specificity and source order:
.a { color: red; }
.a { color: blue; }
The second one wins because same specificity and later.
But if we have:
.a { color: red; }
.b { color: blue; }
And an element with both classes? Then it matches both. The specificities are the same (0,0,1,0). Then the later one (blue) wins.
But note: if the element has both classes, it matches both. So the blue wins.
But if we have:
.a { color: red; }
.a.b { color: blue; }
For an element with both classes, the second rule has (0,0,2,0) because two classes. So it wins.
Now, let's look at the effect of the universal selector:
For a div, the second rule wins because (0,0,0,1) > (0,0,0,0).
But for a span, only the first rule applies? Actually, the second rule doesn't match, so the first rule applies.
Now, what about:
{ color: gray; }
*:hover { color: red; }
The second selector: *:hover -> the universal selector (0,0,0,0) and :hover (pseudo-class) -> c=1, so (0,0,1,0). So when hovering, the second rule wins.
But note: the universal selector doesn't add to specificity, but the pseudo-class does.
Now, let's consider the effect of the :not() pseudo-class with a universal selector:
div:not(*) { color: red; }
This selector:
div: element -> d=1
:not(*) -> the universal selector inside not doesn't add to specificity (because * has 0). So the :not() doesn't add anything? Actually, the :not() pseudo-class itself doesn't add, and the * inside adds 0. So total (0,0,0,1).
But note: the :not() pseudo-class requires an argument, and that argument is a simple selector (in CSS3) or a selector list (in CSS4). In CSS3, the argument must be a simple selector (like .class, #id, element, etc.). In CSS4, it can be a complex selector? But the specificity calculation for :not() is that the specificity of the :not() is the specificity of its argument. So if we have :not(.a), it adds the specificity of .a. But if we have :not() then the argument is * which has 0 specificity, so :not() adds 0.
But note: the :not() pseudo-class itself is a pseudo-class, so it would normally add to c? Actually, no: the spec says that the specificity of :not() is the specificity of its argument. And the :not() pseudo-class does not add to the specificity. Only the argument does.
So:
:not(.a) -> the argument is .a (0,0,1,0) so the whole is (0,0,1,0). But note: the :not() is a pseudo-class and we might think it adds 1 to c? But the spec says: "The specificity of the :not() pseudo-class is the specificity of its argument." So the :not() itself doesn't count, only the argument.
Similarly, :not(#id) -> (0,1,0,0).
But what about:
:not(div) -> the argument is div (0,0,0,1) so the whole is (0,0,0,1). But note: the :not() is a pseudo-class, but we don't count it. So it's the same as just div.
But wait, the selector :not(div) matches any element that is not a div. So it's a different set of elements, but the specificity is the same as the argument.
Now, let's look at an example with multiple arguments in :not() (CSS4):
:not(.a, #b) -> the argument is a selector list. The specificity of a selector list is the maximum of the specificities of the selectors in the list? Actually, the spec for :not() in CSS4 says that the specificity of :not() is the specificity of the most specific selector in its argument list. So:
:not(.a, #b) ->
.a: (0,0,1,0)
#b: (0,1,0,0)
So the maximum is (0,1,0,0). So the whole :not() has (0,1,0,0).
But note: the :not() itself is a pseudo-class, but we don't add an extra one because we take the maximum of the arguments.
So, in CSS4, :not() can take a list and the specificity is that of the most specific selector in the list.
Now, let's consider the case of the :has() pseudo-class (which is a relative selector) but note that :has() is a pseudo-class that takes a relative selector list. The specificity of :has() is the specificity of its argument. But note: the argument is a relative selector, which can contain combinators and multiple selectors. We calculate the specificity of the argument as we normally would (ignoring the :has() itself). Then the :has() pseudo-class does not add to the specificity. So:
div:has(> .a) ->
div: element -> d=1
:has(> .a) -> the argument is the selector "> .a". But note: the > is a combinator and doesn't count. The .a is a class -> c=1. So the argument has specificity (0,0,1,0). Then the whole selector has:
div: (0,0,0,1)
plus the :has() argument's specificity: (0,0,1,0)
But wait, how do we combine? The :has() is a pseudo-class, but we are taking the specificity of the argument. So we add the argument's specificity to the rest?
Actually, the selector is: div:has(> .a)
We break it down:
div: element -> d=1
:has(> .a): the argument is the selector "> .a". But note: the > is a combinator and doesn't count. The .a is a class -> c=1. So the argument has (0,0,1,0). Then, because :has() is a pseudo-class, we might think we add 1 to c? But the spec says: the specificity of :has() is the specificity of its argument. So the :has() part contributes (0,0,1,0). Then we also have the div. So total:
d: 1 (from div)
c: 1 (from the argument of :has())
So (0,0,1,1)
But wait, the :has() itself is a pseudo-class, but we are not counting it as a pseudo-class, we are counting the argument. So it's like we have two simple selectors:
div (element)
.a (class) from inside :has()
So the total is (0,0,1,1).
But note: the :has() pseudo-class is a pseudo-class, but we don't count it as a pseudo-class because we use the argument's specificity. So the pseudo-class itself (the :has()) doesn't add to the count. Only the selectors inside.
So, in general, for pseudo-classes that take a selector argument (like :not(), :has(), :is(), :where(), etc.), we use the specificity of the argument, and the pseudo-class itself doesn't add.
But note: :is() and :where() are special because they have 0 specificity for their argument? Actually, :is() takes the specificity of the most specific selector in its argument list. And :where() always has 0 specificity, regardless of the argument.
So:
:is(.a, #b) -> (0,1,0,0) because the most specific in the argument is #b.
:where(.a, #b) -> (0,0,0,0) because :where() has 0 specificity.
Now, let's look at an example with :is() and :where():
div:is(.a, #b) { ... }
div: element -> d=1
:is(.a, #b) -> the argument has a maximum specificity of (0,1,0,0) (from #b). So the :is() contributes (0,1,0,0).
Total: (0,1,0,1)
div:where(.a, #b) { ... }
div: element -> d=1
:where(.a, #b) -> 0
Total: (0,0,0,1)
So :where() is used to avoid increasing specificity.
Now, let's consider the effect of the :is() and :where() in a selector list:
.a, :is(.b, #c) { ... }
For an element with class "a", it matches the first selector -> (0,0,1,0)
For an element with class "b", it matches the second selector (the .b inside :is) -> the :is(.b, #c) has maximum (0,1,0,0) from #c, but the element only has class "b", so does it match #c? No. So for the element with class "b", the selector :is(.b, #c) matches because .b matches. The specificity of that match is the specificity of the argument that matched? Actually, the :is() pseudo-class has the specificity of the most specific argument, but that doesn't mean that every element matched by :is() gets that maximum specificity.
Wait, this is important: the specificity of :is() is the specificity of the most specific selector in its argument list. But when we match an element, we don't necessarily use the maximum?
Let me clarify:
The selector :is(.a, #b) has a fixed specificity of (0,1,0,0) because the most specific argument is #b. But when we match an element, if the element has class "a", it matches the .a argument. The specificity of the entire selector for that element is (0,1,0,0) because that's the specificity of the :is() pseudo-class, which is fixed to the maximum of the arguments. It doesn't matter which argument matched, the whole :is() has the same specificity.
So:
.a, :is(.b, #c) { ... }
For an element with class "a":
The first selector .a has (0,0,1,0).
The second selector :is(.b, #c) has (0,1,0,0) regardless of whether it matches .b or #c? But if the element doesn't have id "c", it only matches .b. But the specificity of the :is() is still (0,1,0,0) because that's the maximum of the arguments.
So for the element with class "a", it matches the first selector with (0,0,1,0) and the second selector with (0,1,0,0) (if it also matches the :is()? Actually, the element with class "a" doesn't have class "b" or id "c", so it doesn't match the second selector. So only the first selector matches.
But for an element with class "b", it matches the second selector with specificity (0,1,0,0). And if it also has class "a", then it matches both, and the second selector has higher specificity.
So the rule's specificity for an element is the maximum of the specificities of the selectors in the list that match the element.
But note: the :is() itself has a fixed specificity (the maximum of its arguments) regardless of which one matched. So in the above, the second selector always has (0,1,0,0) if it matches, because the :is() has that specificity.
But what if we have:
:is(.a, .b) { ... }
This has specificity (0,0,1,0) because the maximum of .a and .b is (0,0,1,0). So it's the same as .a or .b.
But:
:is(.a, #b) { ... } -> (0,1,0,0)
So, the :is() allows us to group selectors without increasing the specificity beyond the most specific in the group. But note that the group's specificity is the maximum, so it can be high.
Now, :where() always has 0, so:
:where(.a, #b) { ... } -> (0,0,0,0)
So it's a way to have a selector that has no specificity.
But note: the :where() pseudo-class itself is a pseudo-class, but it has 0 specificity. So it doesn't add to c.
So, in summary, the specificity calculation for selectors that contain :is(), :where(), :not(), :has() is:
For :is(selector-list) and :has(selector-list): take the maximum specificity of the selectors in the argument list.
For :not(selector): take the specificity of the argument (which is a single selector in CSS3, but in CSS4 it can be a list? Actually, :not() in CSS4 also takes a list? But then we take the maximum of the list? The spec says for :not() the specificity is the specificity of its argument. If the argument is a list, then the specificity is the maximum of the list? I think so, because in CSS4, :not() can take a list of selectors. So then it's the same as :is()? Not exactly: :is() and :not() are similar but :not() negates. But for specificity, we only care about the selectors inside. So for :not(.a, #b), the specificity is the maximum of .a and #b, which is (0,1,0,0). But note: the :not() itself doesn't add.
But wait, the :not() is a pseudo-class, but we don't count it as a pseudo-class? The spec says: "The specificity of the :not() pseudo-class is the specificity of its argument." So we ignore the :not() and only use the argument's specificity.
Similarly, for :has() and :is(), we use the argument's specificity (the maximum for a list).
But note: the :is() and :where() are pseudo-classes, but we don't count the pseudo-class itself. We only count the argument.
So, in the selector:
div:is(.a, #b)
We have:
div: element -> d=1
:is(.a, #b): the argument has max (0,1,0,0) -> so we add that? Actually, we break the selector into simple selectors and pseudo-classes. The :is() pseudo-class is a pseudo-class, but we don't count it as one. Instead, we treat the argument as if it were part of the selector in place of the :is(). So it's as if we have:
div, and then either .a or #b? But that's not exactly because the :is() is a single pseudo-class that can match one of the arguments. But for specificity, we take the maximum of the arguments.
So the total specificity is the sum of:
The element selector: d=1
The maximum of the arguments: (0,1,0,0)
So we get (0,1,0,1).
But note: we are not adding the pseudo-class (the :is()) as a pseudo-class. We are adding the specificity of the argument.
Similarly, for :not() and :has().
Now, what about a selector like:
div:is(.a):has(.b)
Breakdown:
div: element -> d=1
:is(.a): argument .a -> (0,0,1,0) -> so we add c=1
:has(.b): argument .b -> (0,0,1,0) -> so we add c=1? But wait, we already have c=1 from :is(). Now we add another?
We have two pseudo-classes, each with an argument. We take the specificity of each argument and then add them? Or do we take the maximum?
The selector has two pseudo-classes: :is() and :has(). Each has its own argument. We don't combine the arguments. We calculate the specificity of each part separately and then add.
So:
div: (0,0,0,1)
:is(.a): (0,0,1,0) -> because the argument .a has (0,0,1,0)
:has(.b): (0,0,1,0) -> because the argument .b has (0,0,1,0)
Then we sum the columns? Actually, we don't sum the columns, we count the number of each type in the entire selector.
But note: the entire selector is a chain of simple selectors and pseudo-classes. We break it down:
The div is a type selector -> d=1
The :is(.a) is a pseudo-class, but we don't count the pseudo-class itself, we count the argument .a which is a class -> c=1
The :has(.b) is a pseudo-class, but we count the argument .b which is a class -> c=1 (so now c=2)
So total: (0,0,2,1)
But wait, we have two pseudo-classes? Actually, we are not counting the pseudo-classes (:is and :has) as pseudo-classes, we are counting the selectors inside them. So we have two class selectors (one from each argument). So c=2.
Therefore, the specificity is (0,0,2,1).
But note: if we had:
div:is(.a, .b)
Then the :is() has two arguments, and we take the maximum? Actually, the argument of :is() is a list, and we take the maximum of the list? But in this case, both are classes so maximum is (0,0,1,0). So the :is() contributes (0,0,1,0). Then with div, we get (0,0,1,1).
But if we had:
div:is(.a, #b)
Then the maximum of the argument is (0,1,0,0). So the :is() contributes (0,1,0,0). Then with div, we get (0,1,0,1).
So, the rule for :is(), :has(), :not() (when they have a list) is: take the maximum specificity of the selectors in the list. And then we add that to the rest of the selector.
But note: for :not() with a list, it's the same. But :not() is a pseudo-class that negates, but for specificity we ignore the negation and take the maximum of the arguments.
Now, what about:
div:is(.a, #b):has(.c)
We have:
div: d=1
:is(.a, #b): max of (0,0,1,0) and (0,1,0,0) -> (0,1,0,0) -> so we add b=1? Actually, we don't add the columns separately, we count the number of each type in the entire selector.
But let's think of it as:
The selector has:
- One type selector: div -> d=1
- One pseudo-class: :is() but we don't count it. Instead, we look at the argument of :is() and take the maximum. The maximum argument has 1 ID and 0 classes? Actually, the maximum is #b which has 1 ID and 0 classes? Wait, #b: (0,1,0,0) -> 1 ID, 0 classes, 0 elements? Wait, an ID selector is counted in the b column.
So from the :is() we get 1 in the b column.
Then we have another pseudo-class: :has(.c) -> the argument .c has (0,0,1,0) -> so we get 1 in the c column.
But note: we are not adding the pseudo-classes themselves, we are adding the selectors from the arguments.
So total:
b: 1 (from the ID in the :is() argument)
c: 1 (from the class in the :has() argument)
d: 1 (from div)
So (0,1,1,1)
But wait, we have two pseudo-classes? We are not counting the pseudo-classes, only the selectors inside. So we have one ID and one class and one element.
But note: the :is() argument maximum is an ID, so we get an ID. The :has() argument is a class, so we get a class.
So the total is (0,1,1,1).
But what if the :is() had two arguments that are both classes? Then we would get 1 in the c column from :is()? Actually, no: if the maximum of the arguments is a class, then we get 1 in c. But if there are two classes in the list, the maximum is still 1 class? Because the maximum is computed by comparing the specificities of the selectors in the list. If we have two classes, each has (0,0,1,0). The maximum is (0,0,1,0). So we get 1 in c.
But note: we are not adding the classes from the list, we are taking the maximum. So even if there are 10 classes in the :is() argument, we only get 1 in c because the maximum of 10 classes is still (0,0,1,0) (since each class is 1 in c, and 10 classes would be (0,0,10,0) but we take the maximum, which is 10? Wait, no: the maximum of (0,0,1,0) and (0,0,1,0) is (0,0,1,0). But if we have (0,0,1,0) and (0,0,2,0) then the maximum is (0,0,2,0). So we take the highest specificity among the arguments.
But note: in a selector list, each selector has its own specificity. So if we have :is(.a, .b.c) then:
.a: (0,0,1,0)
.b.c: (0,0,2,0)
So the maximum is (0,0,2,0). So the :is() would contribute (0,0,2,0).
So, for :is(), we take the maximum of the specificities of the selectors in the list.
Similarly for :has() and :not() (with a list).
But for :where(), we always take 0.
So, to summarize the rules for these pseudo-classes:
:is(selector-list) -> specificity = max(specificity of each selector in the list)
:where(selector-list) -> specificity = 0
:not(selector) (or selector-list in CSS4) -> specificity = specificity of the argument (if a list, then max of the list)
:has(selector-list) -> specificity = max(specificity of each selector in the list)
And note that these pseudo-classes themselves do not add to the specificity. We only use the specificity of the argument(s).
Now, let's look at an example with :is() and multiple arguments of different types:
div:is(#a, .b) { ... }
The :is() argument has two selectors:
#a: (0,1,0,0)
.b: (0,0,1,0)
So the maximum is (0,1,0,0).
Then the entire selector:
div: (0,0,0,1)
:is(): (0,1,0,0)
Total: (0,1,0,1)
But note: we are adding the specificity of the :is() argument to the rest? Actually, we are counting the selectors in the entire selector. The div is one element (d=1). The :is() argument maximum has 1 ID (b=1). So we have b=1 and d=1.
So (0,1,0,1).
But what if we had:
div:is(#a, .b):is(.c, #d)
Then:
First :is() max: (0,1,0,0) (from #a or #d? Actually, the first :is() has #a and .b, max is (0,1,0,0). The second :is() has .c and #d, max is (0,1,0,0). Then we have two IDs? But note: we are taking the maximum of each :is() separately. Then we add them? Or we take the maximum of both?
Actually, the entire selector has two :is() pseudo-classes. Each contributes the maximum of its own argument list. So we have:
div: d=1
First :is(): max of (#a, .b) -> (0,1,0,0) -> so we get b=1
Second :is(): max of (.c, #d) -> (0,1,0,0) -> so we get another b=1?
But wait, the specificity is calculated by counting the number of each type in the entire selector. So we have:
One element: div -> d=1
From the first :is(): the argument with the maximum specificity is #a, which has 1 ID -> so we count 1 ID.
From the second :is(): the argument with the maximum specificity is #d, which has 1 ID -> so we count another ID.
So total: b=2, d=1 -> (0,2,0,1)
But note: we are counting the selectors inside the :is() as if they were in the main selector. So we have two IDs (one from each :is()).
But what if the same ID appears in both? We still count it twice? Yes, because they are in different :is() groups. But note: the element only has one ID, but the selector has two ID selectors. That's allowed? Yes, the selector can have multiple ID selectors? Actually, an element can only have one ID, but a selector can have multiple ID selectors if they are in different parts? But note: if we write #a#b, that would require the element to have two IDs, which is impossible. But in this case, the two IDs are in different :is() groups, so the selector requires the element to match at least one of the arguments in each :is(). So the element must have an ID that matches either #a or .b in the first :is(), and an ID that matches either .c or #d in the second :is(). But since an element can only have one ID, it can only match one of the ID selectors in each :is(). But the selector doesn't require the same ID for both. It could have ID #a and then in the second :is() it must match either .c or #d. Since it has ID #a, it doesn't have ID #d, so it must match .c? But it doesn't have class .c. So the element would need to have both an ID that is either #a or .b? But .b is a class, so if the element has ID #a, then in the first :is() it matches #a. In the second :is(), it must match either .c or #d. Since it doesn't have class .c or ID #d, it fails. So the element must have an ID that is either #a or .b (but .b is a class, so if it has class .b, it matches) and also have either class .c or ID #d. So it's possible for an element to have two IDs? No, but it can have one ID and one class? For example, an element with ID #a and class .c would match. Then the selector has two ID selectors? But the element only has one ID. The selector is: div:is(#a, .b):is(.c, #d). For the element with ID #a and class .c, it matches:
First :is(): matches #a (ID)
Second :is(): matches .c (class)
So the selector is valid and the element matches. And the specificity: we have two :is() groups. The first group's maximum is #a (ID) -> 1 ID. The second group's maximum is #d (ID) -> 1 ID. So we count two IDs? But the element only has one ID. However, the selector has two ID selectors (one in each :is()) and the element matches both? Actually, it matches the first :is() by the ID #a, and the second :is() by the class .c. So the element does not match the #d in the second :is(). But the specificity of the second :is() is the maximum of its arguments, which is #d (ID) even though the element matches .c. So the specificity of the second :is() is (0,1,0,0) because the maximum argument is #d. So the entire selector has two IDs? That seems odd because the element only has one ID. But the specificity is about the selector, not the element. The selector has two ID selectors (one in each :is()) and we count them both because they are in the selector. Even if the element doesn't match the ID in the second :is(), the selector still has that ID in it. But wait, the specificity is calculated for the selector, and then for each element we check if the selector matches. The specificity of the selector is fixed regardless of the element. So the selector has two IDs? Actually, the selector has:
div: element
:is(#a, .b) -> the argument list has #a (ID) and .b (class). The maximum is #a (ID) -> so we count 1 ID from this :is().
:is(.c, #d) -> the argument list has .c (class) and #d (ID). The maximum is #d (ID) -> so we count 1 ID from this :is().
So total: 2 IDs, 1 element -> (0,2,0,1).
But note: the element doesn't need to have two IDs. The selector is saying: an element that is a div, and that matches either #a or .b, and that also matches either .c or #d. So an element with ID #a and class .c matches. The selector has two ID selectors, but the element only uses one of them. The specificity is still (0,2,0,1) because the selector contains two ID selectors.
So, in summary, when we have multiple :is(), :has(), :not() with lists, we take the maximum of each list separately and then add them together? Actually, we are counting the number of each type in the entire selector. The entire selector has:
The div: one element -> d=1
The first :is() contributes the maximum of its argument list. If the maximum has 1 ID, then we add 1 to b.
The second :is() contributes the maximum of its argument list. If the maximum has 1 ID, then we add 1 to b.
So we are adding the counts from each part.
But note: if the same type appears in multiple :is(), we count them separately.
So, the specificity of a selector with multiple :is() groups is the sum of the specificity of the fixed parts (like element selectors) and the maximum of each :is() group.
But wait, what about:
div:is(.a, .b):is(.c, .d)
Then:
div: d=1
First :is(): max of two classes -> (0,0,1,0) -> c=1
Second :is(): max of two classes -> (0,0,1,0) -> c=1
So total: (0,0,2,1)
Even though each :is() has two classes, we only take the maximum from each, which is 1 class per :is().
But if we had:
div:is(.a.b, .c.d)
Then the first :is() has two selectors:
.a.b: (0,0,2,0)
.c.d: (0,0,2,0)
So the maximum is (0,0,2,0) -> c=2
The second :is() has two selectors, but if they are the same? Actually, the example has only one :is() with two arguments? Wait, the example: :is(.a.b, .c.d) -> two arguments, each has two classes. The maximum of the two is (0,0,2,0). So the :is() contributes c=2.
Then with div, we get (0,0,2,1).
But if we had:
div:is(.a.b, .c):is(.d, .e.f)
Then:
First :is(): max of (.a.b -> (0,0,2,0) and .c -> (0,0,1,0)) -> (0,0,2,0) -> c=2
Second :is(): max of (.d -> (0,0,1,0) and .e.f -> (0,0,2,0)) -> (0,0,2,0) -> c=2
So total c=4, d=1 -> (0,0,4,1)
But note: the element doesn't need to have 4 classes. The selector requires the element to have at least two classes (to match either .a.b or .c in the first :is(), and either .d or .e.f in the second). But the specificity is 4 because the selector has four class selectors? Actually, the selector has:
In the first :is(): the maximum argument has two classes (from .a.b). So we count two classes.
In the second :is(): the maximum argument has two classes (from .e.f). So we count two classes.
So total 4 classes.
But the element might only have, say, .a and .e and .f? Actually, to match the first :is(), it must match either .a.b (two classes) or .c (one class). If it has .a and .e and .f, then it doesn't have .b, so it doesn't match .a.b. And it doesn't have .c. So it fails the first :is(). So it must have at least two classes to match the first :is() if it doesn't have .c? Actually, it could have .c and then only one class for the first :is(). But then for the second :is(), it must match either .d or .e.f. If it has .e and .f, then it matches .e.f. So it could have classes: .c, .e, .f. Then it matches the first :is() by .c (one class) and the second by .e.f (two classes). But the specificity of the selector is still 4 because we are taking the maximum from each :is(). The first :is() maximum is .a.b which has 2 classes, even though the element matches .c which has 1 class. So the specificity of the first :is() is 2, and the second is 2, total 4.
So the specificity is determined by the selector, not by the element's classes.
This is important: the specificity is a property of the selector, not of the element. So for a given selector, we calculate its specificity once, and that applies to every element that matches that selector.
Therefore, when we have :is() with a list, we take the maximum specificity of the selectors in the list, and that becomes the specificity contributed by that :is().
So, for the selector:
div:is(.a, .b.c)
The :is() has two arguments: .a (0,0,1,0) and .b.c (0,0,2,0). The maximum is (0,0,2,0). So the :is() contributes 2 to c.
Then the entire selector: (0,0,2,1) (because div adds 1 to d).
Now, what about:
div:is(.a, .b):is(.c, .d)
We have two :is(). The first: max of .a (1) and .b (1) -> 1? But .a is (0,0,1,0) and .b is (0,0,1,0). The maximum is (0,0,1,0) -> c=1.
The second: similarly, c=1.
So total c=2, d=1 -> (0,0,2,1).
But if we had:
div:is(.a.b, .c.d)
Then the first :is() has two arguments: .a.b (2) and .c.d (2). The maximum is 2 -> c=2.
Then the entire selector: (0,0,2,1).
But note: we are not adding the two arguments of the same :is(). We take the maximum.
Now, let's consider a complex example:
div:is(.a, #b):has(.c, [type="text"])
Breakdown:
div: d=1
First :is(): arguments .a (0,0,1,0) and #b (0,1,0,0) -> max is #b -> (0,1,0,0) -> so we add 1 to b.
:has(): arguments .c (0,0,1,0) and [type="text"] (0,0,1,0) -> max is (0,0,1,0) -> so we add 1 to c.
Total: b=1, c=1, d=1 -> (0,1,1,1)
Now, what about the :not() with a list:
div:not(.a, #b)
The :not() has a list: .a (0,0,1,0) and #b (0,1,0,0). The maximum is #b -> (0,1,0,0). So the :not() contributes 1 to b.
Then the entire selector: div (d=1) and :not() (b=1) -> (0,1,0,1)
But note: the :not() is a pseudo-class, but we don't count it as a pseudo-class. We count the maximum of the arguments.
So, in summary, for these pseudo-classes that take a selector list, we treat the entire pseudo-class as having the specificity of the most specific selector in the list.
Now, let's look at the :where() pseudo-class:
div:where(.a, #b)
The :where() always has 0 specificity. So it contributes nothing. Then the entire selector is just div: (0,0,0,1).
So, :where() is a way to have a selector that doesn't increase specificity.
Now, let's consider the effect of the :is() and :where() in a selector list:
.a, :where(.b) { ... }
For an element with class "a", it matches the first selector with (0,0,1,0).
For an element with class "b", it matches the second selector with (0,0,0,0) because :where() has 0.
So for an element with both classes, it matches both. The first selector has (0,0,1,0) and the second has (0,0,0,0). So the first wins.
But if we have:
.a, :is(.b) { ... }
Then the second selector has (0,0,1,0) because :is(.b) has the same as .b. So for an element with both classes, both selectors have the same specificity (0,0,1,0) and then the later one wins.
But note: the second selector is :is(.b) which is equivalent to .b. So it's the same as writing:
.a, .b { ... }
So :is() doesn't change the specificity compared to just writing the selectors separately? Actually, it can group them without increasing the specificity beyond the maximum of the group. But if the group has an ID, then it increases.
Now, let's consider the case of the same element being matched by multiple rules with different selectors.
We've already covered that.
Now, I think we have covered the main points.
To answer the user's question: "Explain the concept of CSS specificity and how it determines which styles are applied to an element"
We can summarize:
CSS specificity is a set of rules that determine which CSS rule is applied to an element when multiple rules could apply. It is calculated based on the selectors in the CSS rule. The specificity is represented as a 4-part value (a,b,c,d) where:
a: number of inline styles (1 if present, 0 otherwise) - but note: inline styles are considered as having a=1, but they are not part of the selector. They are declarations with a style attribute.
b: number of ID selectors
c: number of class selectors, attribute selectors, and pseudo-classes (except special pseudo-classes like :is(), :where(), etc. which have their own rules)
d: number of type selectors (element names) and pseudo-elements.
When comparing two specificities, we compare from left to right (a, then b, then c, then d). The higher value in the first differing column wins.
However, note that the :is(), :where(), :not(), and :has() pseudo-classes have special rules:
:is(selector-list) and :has(selector-list) contribute the maximum specificity of the selectors in the list.
:not(selector) (or selector-list in CSS4) contributes the specificity of its argument (or the maximum of the list).
:where(selector-list) always contributes 0.
Also, the universal selector (*), combinators (+, >, ~, ' '), and the negation pseudo-class (:not()) itself do not add to specificity. But the selectors inside :not() do.
The cascade works as follows:
All declarations that apply to the element are considered.
They are sorted by importance: declarations with !important are considered first, then normal declarations.
Within each importance level, sort by specificity (higher wins).
If two declarations have the same specificity and importance, the one that appears later in the CSS (source order) wins.
Therefore, the style with the highest specificity (and importance) will be applied.
However, note that if a property is not set by any rule, then the inherited value or the initial value is used.
Also, note that the specificity of a rule is fixed for that rule, but for a given element, we consider the specificity of the selector that matched the element. If a rule has a selector list, then for an element that matches multiple selectors in the list, the rule's specificity for that element is the maximum of the specificities of the matching selectors.
But since the rule's declaration is the same, it doesn't matter which selector matched for the value, but the specificity used in the cascade is the maximum of the matching selectors.
Example of a rule with a selector list:
.a, #b { color: red; }
For an element with class "a" and id "b", the rule's specificity for that element is (0,1,0,0) because the maximum of .a (0,0,1,0) and #b (0,1,0,0) is (0,1,0,0).
So, in practice, when writing CSS, we should be aware of specificity to avoid unexpected results. It is generally recommended to use classes for styling and avoid IDs and inline styles because they have high specificity and are hard to override.
I hope this explanation is clear.
CSS Specificity: The Rules That Determine Which Styles Win
CSS specificity is a weight-based system that browsers use to decide which CSS rule applies when multiple rules target the same element. It resolves conflicts by assigning "weights" to different types of selectors.
📊 Specificity Calculation (4-Part Value)
Specificity is represented as a tuple: (a, b, c, d)
Column
What it counts
Example
Value
a
Inline styles (style="" attribute)
style="color: red;"
1 (else 0)
b
ID selectors
#header, #main
Count of IDs
c
Class selectors, attribute selectors, pseudo-classes
.class, [type="text"], :hover
Count of these
d
Type selectors (elements), pseudo-elements
div, p, ::before
Count of these
Universal selector (*), combinators (+, >, ~, ), and negation pseudo-class (:not()) do not add specificity—though selectors inside:not()do count.
🔢 How to Calculate Specificity
Count each selector type in your compound selector.
Compare left-to-right—the first differing column wins.
Source Order
If everything else is equal, the later rule in the CSS wins.
Example:
css
.button { background: blue; } /* (0,0,1,0) */
#submit { background: red; } /* (0,1,0,0) */
.button { background: green; } /* (0,0,1,0) — same as first */
For <button class="button" id="submit">:
#submit wins because (0,1,0,0) > (0,0,1,0).
The second .button rule never applies because #submit overrides it.
🧪 Practical Examples
Complex Selector Specificity
css
nav#nav ul li.current a:hover { }
Breakdown:
#nav → 1 ID → b = 1
.current → 1 class → c = 1
:hover → 1 pseudo-class → c = 2 (total)
nav, ul, li, a → 4 type selectors → d = 4Specificity:(0,1,2,4)
Selector List in One Rule
css
.btn, .primary[type="submit"] { }
.btn → (0,0,1,0)
.primary[type="submit"] → 2 classes + 1 attribute → (0,0,3,0)For an element matching both: The rule’s effective specificity = max of matching selectors → (0,0,3,0).
⚠️ Common Pitfalls
Overly specific selectors (e.g., body #wrapper .content p) are hard to override.
!important abuse breaks the cascade—use sparingly.
Inline styles (style="") have high specificity but can be overridden by another inline style with !important or by higher-specificity selectors with !important.
Equal specificity → later rule wins. Order matters!
🎯 Best Practices
Prefer classes for styling—they’re reusable and have moderate specificity.
Avoid IDs and inline styles in CSS (high specificity, hard to override).
Use :where() for "zero-weight" selectors (e.g., resets).
Don’t fight specificity—if you need to override, match or exceed the existing specificity.
Use dev tools (e.g., Chrome’s Computed Styles panel) to inspect which rule wins and why.
💡 Quick Reference
Inline style: (1,0,0,0) (highest, but rarely used)
ID: (0,1,0,0)
Class/attribute/pseudo-class: (0,0,1,0)
Element/pseudo-element: (0,0,0,1)
Universal selector/combinators: (0,0,0,0)
Remember: Specificity is about selector design, not the "importance" of a style. Use it to write predictable, maintainable CSS! 🎨