This rule raises an issue when a CSS declaration uses the !important annotation.
Browsers decide which value applies to a property through the cascade: they compare the origin of the competing declarations, the specificity of
their selectors, and finally their order in the source. Within the same stylesheet origin, a declaration marked !important wins over
every normal declaration, whatever the specificity of the competing selector or its position in the source.
In application code, !important is usually a workaround for an unintended specificity conflict rather than a fix for it. This has
several consequences:
!important, and the escalation spreads through the
stylesheet.Remove the !important annotation and let the intended declaration win through the normal cascade: lower the specificity of the
competing selector, for example with the :where() pseudo-class, reorder the rules, or group the rules into cascade layers with the
@layer at-rule.
#sidebar a {
color: gray;
}
.active {
color: red !important; /* Noncompliant */
}
:where(#sidebar) a {
color: gray;
}
.active {
color: red;
}
With cascade layers, declarations in a layer declared later win over declarations in an earlier layer, whatever the specificity of their selectors:
@layer base, components;
@layer base {
#sidebar a {
color: gray;
}
}
@layer components {
.active {
color: red;
}
}
Once a declaration uses !important, any style that needs to override it must use !important too:
button {
color: blue !important; /* Noncompliant */
}
button:active {
color: red !important; /* Noncompliant: only needed to beat "button" */
}
Without !important, the more specific button:active selector wins on its own:
button {
color: blue;
}
button:active {
color: red;
}
Declarations inside @keyframes blocks are not reported by this rule. Rule {rule:css:S4655} covers them.
!important exception:where()@layer