DEV Community

Clinton N. Dreisbach for Momentum

Posted on

1 1

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)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay