CSS = Cascading Style Sheets... this means the sequence of definition matters, the most recent and more specific takes precedence. If you switched test1 and test2 over in your html you'd get a different result because the variable is defined as the more recent value.

I recommend you use a different variable name for your different .css files if you require them to not share this value.

Answer from SazooCat on Stack Overflow
🌐
W3Schools
w3schools.com › css › css3_variables_overriding.asp
CSS Overriding Variables
We see that the local --primary-bg-color variable will override the global --primary-bg-color variable for the .note selector:
Discussions

Override CSS variable using inline style property?
I'm using a CSS class to animate an element, it has a CSS variable in it, like so: .psc-pump-animation { --animation-duration: 0.5s; } I'm using the class in the style for the element I want to animate and it works great. I can see that additional properties added to the "style" section will ... More on forum.inductiveautomation.com
🌐 forum.inductiveautomation.com
2
0
July 24, 2024
Overriding :root CSS variables from inner scopes - Stack Overflow
In our design system at Stack Overflow, we use Less to compile CSS color values. We have global Less variables like @orange-500 that are frequently modified for hover states, building border styli... More on stackoverflow.com
🌐 stackoverflow.com
overriding the custom css variables only works for the first dom element on the page
tldr: http://plnkr.co/edit/PxEdsV?p=preview I was playing around different material design themes, and it is not completely clear to me in which way I can override the custom css style variables. F... More on github.com
🌐 github.com
7
June 4, 2015
Override PrimeVue css variable
See the “presets” section. https://primevue.org/theming/styled You should be defining your own preset (can base it off of a provided preset), and in there you can override the tab padding with whatever value you would like. You should not be assigning a value to the css variable directly. More on reddit.com
🌐 r/vuejs
11
3
July 26, 2024
🌐
PatternFly
patternfly.org › training › html-css-variables-and-overrides-training
PatternFly • HTML CSS variables and overrides training
Overriding the success variation’s title color means overriding its custom property. In the style.css file, in the .pf-v6-c-alert{} block, write the custom property name. In reference to the formula described in Part 1, this should be: --pf-v6-c-alert · Add the modifier to the custom property name. As displayed in the CSS variables of PatternFly's alert component documentation, the success variation modifier class pf-m-success applies to pf-v6-c-alert.
🌐
DEV Community
dev.to › atulcodex › chapter-two-how-to-override-css-variable-5djd
CSS variable chapter two "how to override CSS variable" 🤹‍♂️ - DEV Community
March 13, 2021 - In some cases, we need to make changes in CSS variable values at a certain level of our project, that's why we use this override feature.
🌐
YouTube
youtube.com › watch
How to override a CSS variable | Custom property - YouTube
🎓 View our courses: https://scrimba.com/links/all-coursesOverriding CSS variables is handy if you want modify a variable inside a certain scope of your app....
Published   June 16, 2022
Find elsewhere
🌐
Tutorial Reference
tutorialreference.com › css › advanced › css3-variables-overriding
CSS Overriding Variables | Tutorial Reference
When we use var(--blue) inside this selector, it will use the local --blue variable value declared here.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › CSS › Guides › Cascading_variables › Using_custom_properties
Using CSS custom properties (variables) - MDN Web Docs
Custom properties (sometimes referred to as CSS variables or cascading variables) are entities defined by CSS authors that represent specific values to be reused throughout a document. They are set using the @property at-rule or by custom property syntax (e.g., --primary-color: blue;). Custom properties are accessed using the CSS var() function (e.g., color: var(--primary-color);).
🌐
CodePen
codepen.io › shibinragh › pen › XjGeab
CSS Variables override
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
🌐
King Rosales
kingrosales.com › home › blog › css: how to override global css variables
CSS: How to Override Global CSS Variables - King Rosales
August 10, 2023 - Many WordPress theme developers use global CSS variables because its easy to control the theme styles. To override any of these CSS variables, create your CSS like normal by stating which selector you want to edit, then instead of specifying the attribute name, you’re going to use the CSS variable name.
🌐
GitHub
github.com › twbs › bootstrap › discussions › 36369
How to override component-level CSS variables? · Discussion #36369 · twbs/bootstrap
May 16, 2022 - OK, I was forgetting how CSS worked. This does indeed override Bootstrap component-level variables: :root * { --bs-navbar-color: blue !important; } - the question is, is that considered bad practice?
Author   twbs
🌐
SamanthaMing
samanthaming.com › tidbits › 4-declaring-css-variables
CSS Variables | SamanthaMing.com
And to use it. You use pass your variable name into var(). ... You can declare your CSS variable in the global scope, meaning it can be available throughout your entire app.
Top answer
1 of 3
27

This is a scoping issue. The way you're doing it, you're inheriting --orange from the :root, and --orange in the :root has a lightness of 68%.

In order to change it, you'll want to re-scope the --orange variable to an element that will look up the new --lightness value. There's a few ways to pull this off:

Option 1: duplicate the --orange variable on the element:

:root {
  --lightness: 68%;
  --orange: hsl(255, 72%, var(--lightness));
}
.card {
  background: var(--orange);
  --orange: hsl(255, 72%, var(--lightness));
}
.card:hover {

  --lightness: 45%;
}
<div class="card">
  Hello world
</div>

Obviously this kinda stinks, because you're going to have to duplicate that --orange variable.

Option 2: You could abstract the other parameters of --orange so that it's not as duplicative. I'd be a fan of this approach despite the fact that it's more text:

:root {
  --lightness: 68%;
  --orangeHue: 255;
  --orangeSat: 72%;
  --orange: hsl(var(--orangeHue), var(--orangeSat), var(--lightness));
}
.card {
  background: var(--orange);
  --orange: hsl(var(--orangeHue), var(--orangeSat), var(--lightness));
}
.card:hover {

  --lightness: 45%;
}
<div class="card">
  Hello world
</div>

What you could also do is scope this specifically to a .darkMode class that might be applied to the HTML element or the body. This could also make sense because it's clear what the intent is from the code:

Option 3

:root {
  --lightness: 68%;
  --orangeHue: 255;
  --orangeSat: 72%;
  --orange: hsl(var(--orangeHue), var(--orangeSat), var(--lightness));
}

.card {
  background: var(--orange);

}
.card:hover {
  --lightness: 45%;
}
.darkMode .card {
  --orange: hsl(var(--orangeHue), var(--orangeSat), var(--lightness));
}
  <div class="darkMode">
    <div class="card">
      Hello world
    </div>
  </div>

Regardless of how you go, the issue is that the --orange variable is inheriting from its original scope where --lightness is set. Think of it as "inheriting a computed value".

In order to get --orange to get the new lightness, you need a new --orange somewhere.

Option 4

I'm not sure what your theme pattern is, but I can explain how I created a dark mode on my own blog . If you look at the CSS What you'll see is that I've created two complete themes that follow the same naming convention:

--themeLightTextColor: rgb(55, 55, 55);
--themeLightBGColor: rgb(255, 255, 255);
--themeLightAccentColor: rgb(248, 248, 248);
--themeLightTrimColor: rgb(238, 238, 238);
--themeDarkTextColor: rgb(220, 220, 220);
--themeDarkBGColor: rgb(23, 23, 23);
--themeDarkAccentColor: rgb(55, 55, 55);
--themeDarkTrimColor: rgb(40, 40, 40);

What I then do is create a third set of variables whose job it is to be the "active" managers:

--themeActiveLinkColor: var(--linkColor);
--themeActiveLinkColorHover: var(--linkColorHover);
--themeActiveTextColor: var(--themeLightTextColor);
--themeActiveEditorialTextColor: var(--themeLightPltNLow);
--themeActiveBGColor: var(--themeLightBGColor);
--themeActiveAccentColor: var(--themeLightAccentColor);
--themeActiveTrimColor: var(--themeLightTrimColor);

Then, I scope the active theme settings under a single class:

.theme--dark {
   --themeActiveTextColor: var(--themeDarkTextColor);
   --themeActiveEditorialTextColor: var(--themeDarkPltNLow);
   --themeActiveBGColor: var(--themeDarkBGColor);
   --themeActiveAccentColor: var(--themeDarkAccentColor);
   --themeActiveTrimColor: var(--themeDarkTrimColor);
}

It seems like maybe your intent is to not have to explicitly declare a theme, but rather tweak some "root variables" to adjust it. But I would suggest that maybe you have a pattern in place where a single class can change an active theme. The advantage to this pattern is that you would be able to also adjust any "root variables" on the class name.

2 of 3
7

I would be interested to learn if there is anything more ideal than this solution but as a possible workaround, you can break up your CSS variables a bit further and build the values inside the element style definitions like so:

:root {
  --orangeColor: 37,72%;
  --redColor: 1,72%;
  --blueColor: 215,72%;
  --greenColor: 126,72%;
  
  --LumDefault: 68%;
  --LumDark: 45%;
  --LumLight: 80%;
}
.card {
  background: hsl(var(--orangeColor), var(--LumDefault));
}
.card:hover {
  background: hsl(var(--orangeColor), var(--LumDark));
}
.card:active {
  background: hsl(var(--redColor), var(--LumDark));
  color: hsl(var(--greenColor), var(--LumLight));
}
<div class="card">
  Hello world
</div>

I do realize that this does not override as you wanted to accomplish but from the business case you stated, it will give you a way to manage elements at a global level...just a bit more work in defining your CSS on the front end.

🌐
GitHub
github.com › Polymer › polymer › issues › 1752
overriding the custom css variables only works for the first dom element on the page · Issue #1752 · Polymer/polymer
June 4, 2015 - Now if a dom element has such a class, and if it contains paper-elements, I would think that those paper-elements would drop their default styling and instead uses the variables defined in the class:
Author   kasperpeulen
🌐
YouTube
youtube.com › watch
How to Override CSS Variable Values within a Container Query - YouTube
Learn how to effectively `override CSS variable values` to enhance the design flexibility of your web pages using container queries.---This video is based on...
Published   August 7, 2025
Views   0
🌐
Patternfly
v4-archive.patternfly.org › v4 › training › html-css-variables-and-overrides-training
PatternFly 4 • HTML CSS variables and overrides training
Overriding the success variation’s title color means overriding its custom property. In the style.css file, in the .pf-c-alert{} block, write the custom property name. In reference to the formula described in Part 1, this should be: --pf-c-alert · Add the modifier to the custom property name. As displayed in the CSS variables of PatternFly's alert component documentation, the success variation modifier class pf-m-success applies to pf-c-alert.
🌐
Bootstrap
blog.getbootstrap.com › 2022 › 05 › 16 › using-bootstrap-css-vars
Using CSS variables in Bootstrap | Bootstrap Blog
May 16, 2022 - Each of them uses CSS variables to customize the alpha transparency value of rgba() colors. Consider our background color utilities, .bg-*. By default each utility class has a local variable, --bs-bg-opacity with a default value of 1. To change the background utility alpha value, you can override that value with your own styles, or some new .bg-opacity-* utilities.