Macke
0
Q:

css custom properties

/* create */
:root {
	--variable-name: variable-property;
}
/* use */
selector {
	property: var(--variable-name);
}
4
:root {
  --main-bg-color: pink;
}
body {
  background-color: var(--main-bg-color);
}
1
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);
0
// 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);
0

New to Communities?

Join the community