Last active
February 15, 2024 19:19
-
-
Save ryanmorr/bf37586750e963d3fa6e8aa0a4f71304 to your computer and use it in GitHub Desktop.
Get, set, and remove a CSS variable in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getCSSVar(name, element = document.documentElement) { | |
return getComputedStyle(element).getPropertyValue(`--${name}`); | |
} | |
function setCSSVar(name, value, element = document.documentElement) { | |
element.style.setProperty(`--${name}`, value); | |
} | |
function removeCSSVar(name, element = document.documentElement) { | |
element.style.removeProperty(`--${name}`); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wonderful, Thanks