Is there a way to access a css variable from javascript? Here my css variable declaration.
:root {
--color-font-general: #336699;
}
Just the standard way:
getComputedStylegetPropertyValue to get the value of the desired propertywindow.getComputedStyle(element).getPropertyValue('--color-font-general');
Example:
var style = window.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); }
window is the global object.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');
document.documentElement.style.setProperty('--color-font-general', '#000'); if you wanted to change it.element.style.getPropertyValue()element.style.getPropertyValue() will only work if the variable has been set on the inline style of the element (not through a CSS rule)
@mediaquery defines them, you save yourself even more work and duplicate code.