Is it possible to add inline style css variable using Renderer2
?
I tried the following but it doesn\'t work.
import { Component, OnChang
Angular sanitizes CSS variables set in property bindings. You can bypass this behavior with DomSanitizer.
@Component({
selector: 'my-app',
template: `
My height is set by CSS Variable
`,
styles: [`
div {
height: var(--height);
}
`
]
})
export class AppComponent {
height = 50;
get style() {
return this.sanitizer.bypassSecurityTrustStyle(`--height: ${this.height}px`);
}
constructor(private sanitizer: DomSanitizer) { }
inc() {
this.height += 10;
}
dec() {
this.height -= 10;
if (this.height <= 0) {
this.height = 0;
}
}
}
Live demo
You might find this article interesting. It goes into details on theming Angular components with CSS variables.