DEV Community

Clinton N. Dreisbach for Momentum

Posted on

Get the value of a CSS variable in JavaScript

I use CSS variables for color values all the time, and I often want to replicate them in JavaScript for charts and other graphics. Instead of copying the colors, I use this function:

function getCssVar (varName) {
  return getComputedStyle(document.body)
   .getPropertyValue(`--${varName}`)
}

Top comments (0)