Skip to content

noDescendingSpecificity

Disallow a lower specificity selector from coming after a higher specificity selector.

Source order is important in CSS, and when two selectors have the same specificity, the one that occurs last will take priority. However, the situation is different when one of the selectors has a higher specificity. In that case, source order does not matter: the selector with higher specificity will win out even if it comes first.

The clashes of these two mechanisms for prioritization, source order and specificity, can cause some confusion when reading stylesheets. If a selector with higher specificity comes before the selector it overrides, we have to think harder to understand it, because it violates the source order expectation. Stylesheets are most legible when overriding selectors always come after the selectors they override. That way both mechanisms, source order and specificity, work together nicely.

This rule enforces that practice as best it can, reporting fewer errors than it should. It cannot catch every actual overriding selector, but it can catch certain common mistakes.

b a { color: red; }
a { color: red; }
code-block.css:2:1 lint/style/noDescendingSpecificity ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Descending specificity selector found. This selector specificity is (0, 0, 1)

1 │ b a { color: red; }
> 2 │ a { color: red; }
^
3 │

This selector specificity is (0, 0, 2)

> 1 │ b a { color: red; }
^^^
2 │ a { color: red; }
3 │

Descending specificity selector may not applied. Consider rearranging the order of the selectors. See MDN web docs for more details.

a {
& > b { color: red; }
}
b { color: red; }
code-block.css:4:1 lint/style/noDescendingSpecificity ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Descending specificity selector found. This selector specificity is (0, 0, 1)

2 │ & > b { color: red; }
3 │ }
> 4 │ b { color: red; }
^
5 │

This selector specificity is (0, 0, 2)

1 │ a {
> 2 │ & > b { color: red; }
^^^^^
3 │ }
4 │ b { color: red; }

Descending specificity selector may not applied. Consider rearranging the order of the selectors. See MDN web docs for more details.

:root input {
color: red;
}
html input {
color: red;
}
code-block.css:4:1 lint/style/noDescendingSpecificity ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Descending specificity selector found. This selector specificity is (0, 0, 2)

2 │ color: red;
3 │ }
> 4 │ html input {
^^^^^^^^^^
5 │ color: red;
6 │ }

This selector specificity is (0, 1, 1)

> 1 │ :root input {
^^^^^^^^^^^
2 │ color: red;
3 │ }

Descending specificity selector may not applied. Consider rearranging the order of the selectors. See MDN web docs for more details.

a { color: red; }
b a { color: red; }
b { color: red; }
a {
& > b { color: red; }
}
a:hover { color: red; }
a { color: red; }
a b {
color: red;
}
/* This selector is overwritten by the one above it, but this is not an error because the rule only evaluates it as a compound selector */
:where(a) :is(b) {
color: blue;
}
biome.json
{
"linter": {
"rules": {
"style": {
"noDescendingSpecificity": "error"
}
}
}
}