Manipulate CSS Custom Properties Using JavaScript

function readCssVariable (elementNode, customProperty){
  const elementStyles = getComputedStyle(elementNode);

  return elementStyles.getPropertyValue(`--${customProperty}`).trim();
}


function writeCssVariable (elementNode, customProperty, value){
  return elementNode.style.setProperty(`--${customProperty}`, value);
}

Resources

It’s Time To Start Using CSS Custom Properties by Serg Hospodarets | Smashing Magazine

Back to top Back to Home