Answers for "change css variable value"

54

css set variable

:root {
  --main-bg-color: coral;
}

#div1 {
  background-color: var(--main-bg-color);
}

#div2 {
  background-color: var(--main-bg-color);
}
Posted by: Guest on December-27-2019
4

how to change css variable in javascript

document.documentElement.setAttribute("style", "--main-background-color: green");
Posted by: Guest on September-11-2020
0

javascript use css custom properties

Values in JavaScript
To use the values of custom properties in JavaScript, it is just like standard properties.
// get variable from inline style
element.style.getPropertyValue("--my-var");

// get variable from wherever
getComputedStyle(element).getPropertyValue("--my-var");

// set variable on inline style
element.style.setProperty("--my-var", jsVar + 4);
Posted by: Guest on August-26-2020

Code answers related to "change css variable value"

Code answers related to "Javascript"

Browse Popular Code Answers by Language