How to create color shades using CSS variables similar to darken() of Sass?

The new Specification introduces “relative color syntax” where you can do the following :root { –color-primary: #f00; /* any format you want here */ –color-primary-darker: hsl(from var(–color-primary) h s calc(l – 5%)); –color-primary-darkest: hsl(from var(–color-primary) h s calc(l – 10%)); } The idea is to convert the main color to hsl format and using calc() … Read more

CSS Variables (custom properties) in Pseudo-element “content” Property

Edit for clarity: CSS custom properties with integer values can be displayed in a pseudo-element’s content property via a CSS counter. div { –variable: 123; } span:after { counter-reset: variable var(–variable); content: counter(variable); } <div>The variable is <span></span>.</div> .coordinates:before { counter-reset: x var(–x) y var(–y); content: ‘The coordinates are (‘ counter(x) ‘, ‘ counter(y) ‘).’; … Read more

Access CSS variable from javascript [duplicate]

Just the standard way: Get the computed styles with getComputedStyle Use getPropertyValue to get the value of the desired property getComputedStyle(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); }

tech