The surprising behavior of !important in CSS custom property values

Huh! I did not realize that CSS custom properties had their own resolution behavior for how !important works in their values. Uh, despite writing a guide about them. 😬 But hey it’s now updated.

Stefan Judis documents it clearly. …


This content originally appeared on CSS-Tricks and was authored by Chris Coyier

Huh! I did not realize that CSS custom properties had their own resolution behavior for how !important works in their values. Uh, despite writing a guide about them. 😬 But hey it’s now updated.

Stefan Judis documents it clearly. The deal is that !important is not at all used in the final value. So:

div {
  --color: red !important;
  color: var(--color);
  color: yellow;
}

It kinda feels like red should win, but because !important is ultimately stripped from the custom property value, yellow wins it out. And it’s not because the color declaration comes last — if color: red !important; was the first declaration, then red would win.

But it’s not like !important is just stripped and ignored; it’s used in a scoped way, affecting which custom property value wins. But then !important is gone when that value is applied. Stefan’s example:

div {
  /*
    `!important` overrules the
    other `--color` definitions
  */
  --color: red !important;
  color: var(--color);
}

.class {
  --color: blue;
}

#id {
  --color: yellow;
}

This feels weird (to me) as you’d think yellow declared on #id would win because it has the highest specificity. But that’s the deal with the scoped behavior — !important makes red the winner, then is applied to the color as such.

To Shared LinkPermalink on CSS-Tricks


This content originally appeared on CSS-Tricks and was authored by Chris Coyier


Print Share Comment Cite Upload Translate Updates
APA

Chris Coyier | Sciencx (2021-12-17T15:19:08+00:00) The surprising behavior of !important in CSS custom property values. Retrieved from https://www.scien.cx/2021/12/17/the-surprising-behavior-of-important-in-css-custom-property-values/

MLA
" » The surprising behavior of !important in CSS custom property values." Chris Coyier | Sciencx - Friday December 17, 2021, https://www.scien.cx/2021/12/17/the-surprising-behavior-of-important-in-css-custom-property-values/
HARVARD
Chris Coyier | Sciencx Friday December 17, 2021 » The surprising behavior of !important in CSS custom property values., viewed ,<https://www.scien.cx/2021/12/17/the-surprising-behavior-of-important-in-css-custom-property-values/>
VANCOUVER
Chris Coyier | Sciencx - » The surprising behavior of !important in CSS custom property values. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/17/the-surprising-behavior-of-important-in-css-custom-property-values/
CHICAGO
" » The surprising behavior of !important in CSS custom property values." Chris Coyier | Sciencx - Accessed . https://www.scien.cx/2021/12/17/the-surprising-behavior-of-important-in-css-custom-property-values/
IEEE
" » The surprising behavior of !important in CSS custom property values." Chris Coyier | Sciencx [Online]. Available: https://www.scien.cx/2021/12/17/the-surprising-behavior-of-important-in-css-custom-property-values/. [Accessed: ]
rf:citation
» The surprising behavior of !important in CSS custom property values | Chris Coyier | Sciencx | https://www.scien.cx/2021/12/17/the-surprising-behavior-of-important-in-css-custom-property-values/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.