CSS variables have access to the DOM, which means that you can change them with JavaScript.
The getComputedStyle() method gives an object which includes all the styles applied to the target element. getPropertyValue() method is used to obtain the desired property from the computed styles. setProperty() is used to change the value of CSS variable.
Just the standard way:
getComputedStyle
getPropertyValue
to get the value of the desired propertygetComputedStyle(element).getPropertyValue('--color-font-general');
Example:
var style = getComputedStyle(document.body)
console.log( style.getPropertyValue('--bar') ) // #336699
console.log( style.getPropertyValue('--baz') ) // calc(2px*2)
:root { --foo:#336699; --bar:var(--foo); --baz:calc(2px*2); }
Use this:
window.getComputedStyle(document.documentElement).getPropertyValue('--color-font-general');
And you can change it like this:
document.documentElement.style.setProperty('--color-font-general', '#000');
source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With