Angular Templates [style] • [class] • ngClass
Angular lets you change CSS styles and classes dynamically from your component. You can use [style] bindings for inline styles and [class] or [ngClass] for class-based styling. This makes your UI react to component state (variables, conditions, events) in real time.
[style.property] or the shorthand [style].[class.className] to toggle a single CSS class.[ngClass] to apply multiple classes using an object, array, or string.isActive, fontSize) directly control how the element looks.[style] for conditional one-off changes.[style.color]="isActive ? 'green' : 'red'"[style.fontSize.px]="fontSize"[class.highlight]="isHighlighted"[ngClass]="{ highlight: isHighlighted, warning: isWarning }"[ngClass]="['highlight', dynamicClass]"
// app.component.ts - dynamic style binding
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
isActive = true;
fontSize = 20;
}
// app.component.html - dynamic inline styles
<p
[style.color]="isActive ? 'green' : 'red'"
[style.fontSize.px]="fontSize"
>
Dynamic styled text
</p>
When isActive is true, the text appears in green; when it is false, it appears in red. The fontSize property controls the font size in pixels through [style.fontSize.px].
Changing isActive or fontSize in the component (e.g., on button click or slider change) immediately updates the element styles in the template.
// app.component.ts - class binding
export class AppComponent {
isHighlighted = true;
isWarning = false;
}
// app.component.html - single class binding
<p [class.highlight]="isHighlighted">This is highlighted</p>
<p [class.warning]="isWarning">This is a warning</p>
The highlight class is added to the first paragraph only when isHighlighted is true. Similarly, the warning class is added to the second paragraph when isWarning is true.
This is great for toggling a single class based on a boolean condition, such as active state, error, or success.
// app.component.ts - ngClass with object
export class AppComponent {
classes = {
highlight: true,
warning: false
};
}
// app.component.html - multiple classes with ngClass
<p [ngClass]="classes">This text uses ngClass</p>
// app.component.ts - ngClass with method
export class AppComponent {
isHighlighted = true;
isWarning = true;
getClasses() {
return {
highlight: this.isHighlighted,
warning: this.isWarning
};
}
}
// app.component.html - bind to getClasses()
<p [ngClass]="getClasses()">
This text can be both highlighted and warning
</p>
With [ngClass], you can:
getClasses() to compute classes dynamically.[style] for simple, dynamic styles like color, width, and font size.[class] when toggling a single CSS class based on a boolean.[ngClass] for multiple classes or when logic gets more complex.[style.color] and a boolean property.fontSize and use [style.fontSize.px] to dynamically resize a paragraph.highlight and warning classes in your CSS and use [ngClass] to apply one or both based on component properties.[class.active] for styling.