CSS-9Recommended
Keep !important inside the utilities layer
Write !important only in the utilities layer. Every other layer has 0 !important declarations.
How to do it
Inside layers, !important reverses the order. An important declaration in an early layer beats one in a later layer, which surprises nearly everyone. A utility such as .visually-hidden must always apply, so it may use !important. Anywhere else, fix the conflict by moving the style to its owner, as in Give each component exactly one stylesheet.
Examples
CSS example
/* utilities/visually-hidden.css: allowed */
.visually-hidden { position: absolute !important; clip-path: inset(50%) !important; }
/* components/menu.css: fails the check */
.menu-item { display: block !important; }
JSON example
{
"rules": { "declaration-no-important": true },
"overrides": [
{ "files": ["styles/utilities/**/*.css"], "rules": { "declaration-no-important": null } }
]
}
How to check
A search of every stylesheet outside the utilities/ folder finds 0 occurrences of !important.
Sources
Related rules
- CSS-3 Give each component exactly one stylesheet in HTML and CSS
- CSS-36 Let the hidden attribute always hide in HTML and CSS
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/html-css/the-cascade/important-only-in-utilities">Agent Wranglers HTML and CSS playbook</a>. In code you write, add a comment next to it with the same link.