7

I am using Angular 6 and I have a simple div and want to set the background color of this div from inside the template. This works fine when passing normal colors. But this does not work with CSS Variables.

This example works

<div [style.background]="'red'">...</div>

This example does not work

<div [style.background]="'var(--some-css-var)'">...</div>

3 Answers 3

18

You have to use ngStyle

<some-element [ngStyle]="{'background-color': styleExp}">...</some-element>

https://angular.io/api/common/NgStyle

Sign up to request clarification or add additional context in comments.

Comments

8

In order to bind a style property to a CSS variable in the HTML template, the CSS variable expression var(...) must be sanitized. You can define a custom pipe:

import { Pipe, PipeTransform } from '@angular/core';
import { DomSanitizer, SafeStyle } from '@angular/platform-browser';

@Pipe({
  name: 'safeStyle'
})
export class SafeStylePipe implements PipeTransform {

  constructor(private sanitizer: DomSanitizer) { }

  transform(value: string): SafeStyle {
    return this.sanitizer.bypassSecurityTrustStyle(value);
  }
}

and use it in the HTML template:

<div [style.background-color]="'var(--some-css-var)' | safeStyle"></div>
<div [style.background-color]="bkColor | safeStyle"></div>
bkColor = "var(--some-css-var)";

See this stackblitz for a demo.

6 Comments

This works! But as @Gurvinder Guraya responded down below this will also work without the pipe. Should I use the pipe or ngStyle instead?
Your question was about setting the color with a CSS variable. The only way to make it work is to sanitize the value (with a pipe or in code). Other answers give alternatives to avoid setting the color with a CSS variable. You can obviously set the color in several ways but I tried to answer the question as it was asked, assuming that you had reasons to use CSS variables.
strangely this worked: [ngStyle]="{ 'background-color': 'var(--dy-color-2)' }"
Or would you recommend to do it with sanitizing the value?
Ah, OK. Sorry, I hadn't tried ngStyle with a CSS variable (I thought @GurvinderGuraya gave it as a workaround). You can pick the solution that you prefer then. If sanitizing is not needed with ngStyle, then that would be a simpler solution.
|
5

You can do it in 2 ways

  1. use pipe that get a string and return some color code code.

    <div [style.background-color]="someString | colorSetter"></div>
    
  2. add to the html tag a dynamic class for example:

    <div class="my-div" [class]="someClassName"></div>

and in the scss add the options

scss:

.my-div{
    &.optoin1{
       background-color:red;
     }
&.optoin2{
       background-color:black;
     }
&.optoin3{
       background-color:green;
     }
&.optoin4{
       background-color:yellow;
     }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.