3 ms·
Not sure if it's a well known trick, I usually have a JS function in a util library for setting CSS variable. // Set value to the variable at the element,
by ww520 5y ago
Not sure if it's a well known trick, I usually have a JS function in a util library for setting CSS variable.
// Set value to the variable at the element, default to :root.
// e.g. setCssVar("--varFoo", 123)
function setCssVar(cssVar, value, elemSelector) {
let elem = document.querySelector(elemSelector || ":root");
elem.style.setProperty(cssVar, value);
}