TIL targetting CSS variables with Javascript
POSTED ON:
TAGS: css javascript codepen webdev
You can target CSS variables with JS, to change design elements without hacking the element itself.
This is super cool because now, you can actually focus on targetting CSS variables, rather than each individual JS element, and have it ripple effect to all your other CSS elements.
:root {
--pagebackground: rebeccapurple;
}
body {
background: var(--pagebackground);
}
let bg = getComputedStyle(document.documentElement).getPropertyValue('--pagebackground');
document.documentElement.style.setProperty('--pagebackground', 'yellow');
See the Pen Targetting CSS Variables with JS by rockykev (@rockykev) on CodePen.
Related TILs
Tagged: css