noExcessiveSelectorClasses (CSS)
Summary
Section titled “Summary”- Rule available since:
v2.4.12 - Diagnostic Category:
lint/complexity/noExcessiveSelectorClasses - This rule isn’t recommended, so you need to enable it.
- This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Same as
selector-max-class
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "complexity": { "noExcessiveSelectorClasses": "error" } } }}Description
Section titled “Description”Limit the number of classes in a selector.
Selectors with too many chained classes are harder to read, harder to override, and often signal overly specific styling. This rule enforces an upper bound on how many class selectors can appear in one selector.
Each selector in a selector list is evaluated separately.
For example, .foo, .bar.baz is treated as two selectors, and only .bar.baz
contributes two class selectors.
Nested selectors are checked as written instead of being combined with their parent. For
example, in .foo { &.bar {} }, the nested selector &.bar contributes one class.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”This example reports a diagnostic when maxClasses is 1:
{ "linter": { "rules": { "complexity": { "noExcessiveSelectorClasses": { "level": "on", "options": { "maxClasses": 1 } } } } }}.foo .bar {}code-block.css:1:1 lint/complexity/noExcessiveSelectorClasses ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected this selector to have no more than 1 class selector, but found 2.
> 1 │ .foo .bar {}
│ ^^^^^^^^^
2 │
ℹ Selectors with too many chained classes are harder to read, override, and reuse.
ℹ Reduce the number of class selectors in this selector, or split it into simpler selectors.
:is(.foo, .bar.baz) {}code-block.css:1:1 lint/complexity/noExcessiveSelectorClasses ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected this selector to have no more than 1 class selector, but found 3.
> 1 │ :is(.foo, .bar.baz) {}
│ ^^^^^^^^^^^^^^^^^^^
2 │
ℹ Selectors with too many chained classes are harder to read, override, and reuse.
ℹ Reduce the number of class selectors in this selector, or split it into simpler selectors.
The following examples are valid when maxClasses is set to 1:
{ "linter": { "rules": { "complexity": { "noExcessiveSelectorClasses": { "level": "on", "options": { "maxClasses": 1 } } } } }}.foo {}.foo, div {}Options
Section titled “Options”maxClasses
Section titled “maxClasses”The maximum number of class selectors allowed in a single selector.
This option has no default value. Configure it explicitly to enable the rule.
A value of 0 disallows class selectors entirely.
{ "linter": { "rules": { "complexity": { "noExcessiveSelectorClasses": { "level": "on", "options": { "maxClasses": 2 } } } } }}The following selector exceeds the configured limit because it contains three class selectors:
.foo .bar.baz {}code-block.css:1:1 lint/complexity/noExcessiveSelectorClasses ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ Expected this selector to have no more than 2 class selectors, but found 3.
> 1 │ .foo .bar.baz {}
│ ^^^^^^^^^^^^^
2 │
ℹ Selectors with too many chained classes are harder to read, override, and reuse.
ℹ Reduce the number of class selectors in this selector, or split it into simpler selectors.
Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.