Using attr to set css variable inline that work with CSP

Not yet. In the same link you can read: Note: The attr() function can be used with any CSS property, but support for properties other than content is experimental, and support for the type-or-unit parameter is sparse. Still no browser support the attr() for properties different than content and also no support for the type-or-unit. … Read more

Avoiding repeated constants in CSS

Recently, variables have been added to the official CSS specs. Variables allow you to so something like this : body, html { margin: 0; height: 100%; } .theme-default { –page-background-color: #cec; –page-color: #333; –button-border-width: 1px; –button-border-color: #333; –button-background-color: #f55; –button-color: #fff; –gutter-width: 1em; float: left; height: 100%; width: 100%; background-color: var(–page-background-color); color: var(–page-color); } button … Read more

Why can’t I animate custom properties (aka CSS variables)?

When this question was asked, it wasn’t possible to animate custom properties, as @temani afif correctly pointed out – since the UA has no way to interpret their contents Since then, CSS Houdini have put together the CSS Properties and Values API specification This specification extends [css-variables], allowing the registration of properties that have a … 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); }

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