The question itself is pretty self-explanatory. I have the following code:
:root {
--brand-color: blueviolet;
--button-background-color: var(--brand-color);
}
body {
--brand-color: blue;
}
button {
background-color: var(--button-background-color);
border: none;
color: #fff;
border-radius: 4px;
padding: 4px 10px;
}
<html>
<body>
<button>Button</button>
</body>
</html>
In the body {...}
selector, I have overridden the --brand-color
variable. The button is in the body, but the button's background color is still set to the --brand-color
defined in the root. I understand that this is the default behavior of CSS variables, but can someone explain to me why this is happening and what is the correct way to override the variable here? Provided, I don't want to override it in the button {...}
selector.