Sub topic 4
Conditional styling with class and style bindings
This chapter assumes you can write a standalone Angular component, use property binding in templates, and read basic CSS selectors. You should also know what a signal is. The goal is to change a component's appearance from its state without touching the DOM yourself.
Why bind instead of manipulating the DOM
You could set element.style.width in code. That bypasses Angular's change detection and makes the template harder to read. Class and style bindings keep the rule in the template and recompute it whenever the bound value changes.
Both kinds of binding work with component styles. The class names you toggle must exist in the component's own styles, or in a global stylesheet. Under Emulated encapsulation, the component's rules apply to elements in its template, so a toggled class works as you expect.
Class bindings
A single class binding has the form [class.name]="condition". Angular adds the class when the expression is truthy and removes it when it is falsy. Other classes on the element are left alone.
You can also bind several classes at once with [class]. It accepts a string, an array, or an object whose keys are class names and whose values are booleans.
<div [class]="'card ' + sizeClass()"></div>
<div [class]="{ active: isActive(), disabled: isDisabled() }"></div>Prefer the single [class.name] form for one or two conditions. Use the object form when many classes depend on state.
Style bindings
A style binding has the form [style.property]="value". Use the CSS property name in kebab case, such as [style.background-color]. Angular also accepts camelCase, for example [style.backgroundColor].
For properties that take a unit, add the unit to the binding name: [style.width.px]="120" or [style.width.%]="percent()". The value is then a plain number. A falsy value of null or undefined removes the style, so the element falls back to its stylesheet rule.
CSS custom properties also work: [style.--accent]="color()". That sets a variable that the component's CSS can read with var(--accent).
A worked example
The component below shows a progress bar. The done class changes the color at 100 percent, and the width follows the signal.
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-progress',
template: `
<div class="bar"
[class.done]="percent() === 100"
[style.width.%]="percent()"></div>
<button (click)="advance()">Advance</button>
`,
styles: `
.bar { height: 8px; background: steelblue; }
.bar.done { background: seagreen; }
`,
})
export class ProgressComponent {
percent = signal(40);
advance() {
this.percent.update(p => Math.min(100, p + 20));
}
}Each click updates the signal. Angular re-evaluates both bindings and patches only the class list and the width style.
Specificity and precedence
Style bindings write inline styles. Inline styles beat any selector in a stylesheet, except rules marked !important. If a bound style seems to ignore your CSS, this is usually why. When you need a CSS rule to win, use a class binding instead.
If the same property is set in several places, Angular resolves it in a fixed order. Bindings on the element in the template win over host bindings, and host bindings win over those on the directive's own host. You rarely need the full order. Avoid setting the same property in two places.
Common errors
| Mistake | What happens | Fix |
|---|---|---|
| `[style.width]="percent()"` with a number | Value has no unit and is ignored | Use `[style.width.%]` or `[style.width.px]` |
| `[class.is-done]` toggled, but no `.is-done` rule | The class changes, but nothing looks different | Define the rule in `styles` |
| `class="a"` plus `[class]="'b'"` | Both appear in modern Angular, but order of updates can confuse readers | Use `[class.name]` for toggles |
| Using `ngClass` or `ngStyle` for one condition | More code and an extra import | Use a native binding |
Exercise
A template has <div class="box" [style.height]="size()"></div> with size = signal(50), but the box does not change height. What is wrong, and how do you fix it?
Show answer
The value 50 has no unit, so the browser treats it as invalid and ignores it. Use [style.height.px]="size()", or make the signal hold a string such as '50px'.
Exercise
A bar uses [style.background]="'steelblue'" and a CSS rule .bar.done { background: seagreen; }. The done class is on the element, but the bar stays blue. Why?
Show answer
The bound inline style has higher precedence than the stylesheet rule. Remove the style binding and use a class for the color, or bind the color value from state.