user3531149
user3531149

Reputation: 1539

How to set a css class value from variable

I'm trying to set the color of all 'button' and 'a' tags to a color coming from a variable. Like this, which obviously doesn't work.

@Component({
styles: [`    
    a, button {
        color: {{ color_from_variable }};
    }
`]

Upvotes: 1

Views: 805

Answers (2)

br.julien
br.julien

Reputation: 3460

This worked for me :

export const mainColor = 'blue';

@Component({
  ...
  styles: [`    
    p {
      color: `+mainColor+`;
    }
 `]
})

Here is a StackBlitz example I made for this : https://stackblitz.com/edit/angular-vpaarz

Upvotes: 1

Adam Pery
Adam Pery

Reputation: 2102

You can use ngStyle:

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

<some-element [ngStyle]="{'max-width.px': widthExp}">...</some-element>

<some-element [ngStyle]="objExp">...</some-element>

Upvotes: 0

Related Questions