JAVASCRIPT
how to change a css variable with javascript
var root = document.querySelector(':root');
root.style.setProperty('--variable', 'lightblue');
/or/
root.style.setProperty('--variable', myColor);
how to change css variable in javascript
document.documentElement.style.setProperty("--main-background-color", "green");
set css variable from javascript
document.documentElement.style.cssText = "--tab-count: 3";
/or/
document.documentElement.style.setProperty("--tab-count", 5);
/or/
document.documentElement.setAttribute("style", "--tab-count: 5");
how to change css variable in javascript
document.documentElement.setAttribute("style", "--main-background-color: green");
change css variable with javascript
document.documentElement.style.setProperty("--tab-count", 5);
js set css variable
document.documentElement.style.setProperty("--main-background-color", "green");
how to set css variables in javascript
const docStyles = document.documentElement.style;
docStyles.setProperty('--accent', '#ffffff');
docStyles.setProperty('--textColor', '#ffffff');
how to change css variable in javascript
document.documentElement.style.cssText = "--main-background-color: red";
css variable value changing with javascript
const cssVar = ( name, value ) => {
if(name.substr(0, 2) !== "--") {
name = "--" + name;
}
if(value) {
document.documentElement.style.setProperty(name, value)
}
return getComputedStyle(document.documentElement).getPropertyValue(name);
}
change css variable with javascript
let root = document.documentElement;
root.addEventListener("mousemove", e => {
root.style.setProperty('--mouse-x', e.clientX + "px");
root.style.setProperty('--mouse-y', e.clientY + "px");
});
change css variable with javascript
element.style.setProperty("--my-var", jsVar + 4);