Skip to content

Instantly share code, notes, and snippets.

@ryanmorr
Last active February 15, 2024 19:19
Show Gist options
  • Save ryanmorr/bf37586750e963d3fa6e8aa0a4f71304 to your computer and use it in GitHub Desktop.
Save ryanmorr/bf37586750e963d3fa6e8aa0a4f71304 to your computer and use it in GitHub Desktop.
Get, set, and remove a CSS variable in JavaScript
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}`);
}
Copy link

ghost commented Feb 7, 2023

great works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment