Skip to content

noDescendingSpecificity (CSS)

Language CSS
biome.json
{
"linter": {
"rules": {
"style": {
"noDescendingSpecificity": "error"
}
}
}
}

Disallow lower-specificity selectors after higher-specificity selectors.

Specificity is the priority score CSS calculates from a selector. When two selectors have the same specificity, the later declaration wins. A selector with higher specificity wins regardless of source order.

A lower-specificity selector placed later can therefore look like an override even though it cannot replace the earlier style. Ordering selectors from lower to higher specificity makes the cascade easier to read.

The rule reports likely conflicts between selectors that end with the same target under the same surrounding rules, such as @media or @layer. It cannot determine every case where two selectors match the same element. Nested selectors are combined with their parent selectors before comparison, so nav { a { &:hover {} } } behaves like nav a:hover {}.

Rules without declarations, such as a rule that only contains a comment or nested rules, are ignored. These rules set no styles, so their position doesn’t matter.

This rule does not evaluate SCSS. It compares statically written selectors within the same SCSS block, but not selectors across mixin or include boundaries, or across mutually exclusive control-flow branches. Selectors containing interpolation or placeholders are ignored because their emitted selector and specificity depend on SCSS evaluation.

In SCSS, an @include inserts the contents of a mixin. The rule can’t see what the mixin contains, so it treats every @include as a declaration. Stylelint does this only for an @include with a { ... } block, so this rule can report selectors that Stylelint doesn’t.

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 be 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 be 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 be applied. Consider rearranging the order of the selectors. See MDN web docs for more details.

.a th {
color: red;
}
.a .b .c th {
color: green;
}
.a .b th {
color: blue;
}
code-block.css:9:1 lint/style/noDescendingSpecificity ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

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

7 │ }
8 │
> 9 │ .a .b th {
│ ^^^^^^^^
10 │ color: blue;
11 │ }

ℹ This selector specificity is (0, 3, 1)

3 │ }
4 │
> 5 │ .a .b .c th {
│ ^^^^^^^^^^^
6 │ color: green;
7 │ }

ℹ Descending specificity selector may not be 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 { color: red; }
a:hover { color: red; }
a b {
color: red;
}
/* The rule cannot determine that these selectors target the same elements. */
:where(a) :is(b) {
color: blue;
}
.a th {
color: red;
}
@media print {
.a .b .c th {
color: green;
}
}
@layer one {
b a { color: green; }
}
@layer two {
a { color: blue; }
}