DEV Community

Discussion on: CSS Variables

Collapse
 
clozach profile image
Chris Lozac'h

Great article, Samantha! The only question I had at the end was how to do the CSS ↔️ JS round trip for a global variable, as opposed to (as I understand it) your final example, which shows how to do it for a variable in local scope?

Collapse
 
samanthaming profile image
Samantha Ming • Edited

Great question @clozach ! You will have to go with the getComputedStyle route, but instead of passing the element, you just pass in document.documentElement. So something like this:

:root {
  --color: red;
}
getComputedStyle(document.documentElement).getPropertyValue('--color');

(Let me update my official notes too 👏)