The surprising behavior of "important CSS custom properties"
- Published at
- Updated at
- Reading time
- 3min
CSS custom properties are flexible, make code DRY ("don't repeat yourself"), and can keep a codebase maintainable. The larger a CSS codebase, the more critical your CSS is easy to handle. CSS code becomes messy when you add new code without having a strategy, but if you throw !important into the mix, a maintenance nightmare is often unavoidable.
But have you tried using !important with custom properties? You'll find Interesting things!
Be warned, the !important keyword in custom properties declaration behaves differently than you think!
The syntax for custom property values is permissive. The specification allows everything but a few specific tokens as the custom property value. These prohibited tokens include unmatched ] or }, semi-colons or the ! character.
Even though !important obviously includes a !, it's considered a special case that is still valid!