by hanniabu · 10y ago · 0 comments
/* JS */ var styles = getComputedStyle(document.documentElement); var value = String(styles.getPropertyValue('--primary-color')).trim(); // value = 'red'
Rather than calling '--primary-color', you should really be able to call 'color' which points to the variable and will still return red. Otherwise you'll have to know the name of each variable being used, which is much more difficult than knowing the property.
color: var(--primary-color);
If care about that property, and not the variable, then get it as usual: let value = styles['color']; // 'red'So of course you have to know the name of the variables, they're an API for your CSS.
If you use an API that gives you the value at an earlier time, like el.style.color (which is "specified value"), you'll see the var().