I’m currently having an issue with CSS variables not being set, I’m using electron and im trying to set it like this:
JavaScript
x
12
12
1
for (let Button of ThemeButtons){
2
Button.addEventListener("click", e =>{
3
let Color = Button.style.backgroundColor;
4
let root = document.documentElement
5
6
console.log(Color)
7
root.style.setProperty('--CurrentA', Color);
8
root.style.setProperty('--CurrentB', Color);
9
root.style.setProperty('--CurrentC', Color);
10
});
11
}
12
And i’ve my variables defined like so:
JavaScript
1
15
15
1
:root {
2
--red:#923333;
3
--orange:#d68a00;
4
--green: #0d840d;
5
--blue: #1c4a90;
6
--purple: #6a1d8a;
7
--dark: #313131;
8
--gray: rgb(68, 68, 68);
9
--light: #b7b7b7;
10
11
--CurrentC: rgb(85, 85, 85);
12
--currentA: rgb(68, 68, 68);
13
--currentB: rgb(53, 53, 53);
14
}
15
The color is printed, but the variable is not being set. I’ve seen ‘BlinkFeatures’ and i’ve enabled those too, i still can’t get it to work. Anyone know what’s going on?
Advertisement
Answer
I think this is a problem of css specificity. :root points to the document trees root (HTML tag) and so does document.documentElement
except :root has higher specificity (as seen here)
To solve this problem you should probably set the variable overrides to e.g. body tag or change :root {}
to html {}