+ 4

Problem with setting css variables with javascript. [solved]

I've a code, where I used a css variable for a specific color, a blue, and used it for every title and many elements. I created a dark mode, and if we click the dark mode button I would like to change the variable. I tried using this in the function: document.body.style.setProperty('--blue', '#1976D2'); Here's how I used the variable: :root { --blue:... ; } But it seems that I'm doing something wrong, because nothing happens, but I don't know what. Can anyone please help me fixing it? Thanks in advance :) https://code.sololearn.com/WdnjNR74ydO0/?ref=app

12th Mar 2021, 6:28 AM
Matthew
Matthew - avatar
6 Respostas
+ 2
Major シ it was not working because you had defined the variable in all Like this *{ --blue:#something; } But variables should only be defined in :root in css
12th Mar 2021, 6:49 AM
Arnav Kumar [Less/Not Active]
Arnav Kumar [Less/Not Active] - avatar
+ 7
actually blue in ilus is a color variable for getting the ilus blue colour so changing it would be necessary ✌️
12th Mar 2021, 7:01 AM
Atoms~⚛
Atoms~⚛ - avatar
+ 2
Root in css can be accessed in js by document.querySelector(":root") or document.documentElement Then you can use set property on style o change the properties or you can change full set of variables by style.cssText
12th Mar 2021, 6:33 AM
Arnav Kumar [Less/Not Active]
Arnav Kumar [Less/Not Active] - avatar
12th Mar 2021, 6:35 AM
Arnav Kumar [Less/Not Active]
Arnav Kumar [Less/Not Active] - avatar
+ 2
Arnav Kumar [📕Schools📚] if we click the button nothing happens, Idk why, i mean the colors of the blue should be changed...
12th Mar 2021, 6:38 AM
Matthew
Matthew - avatar
12th Mar 2021, 6:32 AM
Sharique Khan
Sharique Khan - avatar