← Back to Angular, page 3

angular-0058 · Easy fresher

How do you change CSS classes and styles dynamically in Angular, and how do [class.x], [class] and [style] bindings compare with ngClass and ngStyle?

Use [class.name]="condition" for one class, [class]="expression" for several (a string, array, or object), and [style.prop.unit]="value" for styles. ngClass and ngStyle still work but are no longer needed, and Angular recommends the native bindings in new code.

Full answer

There are three levels of class and style binding.

  1. Single class or style: [class.active]="isActive()" adds the class when the value is truthy. [style.width.px]="width()" sets one style, and the unit goes after the property name.
  2. Many classes: [class]="classes()" accepts a string ('a b'), an array (['a', 'b']), or an object ({ a: true, b: false }). Static classes in the same element stay in place, because Angular merges them.
  3. Many styles: [style]="styles()" accepts a string or an object such as { color: 'red', 'font-size.px': 16 }.

ngClass and ngStyle are older directives from CommonModule (now NgClass and NgStyle, standalone). They did what [class] and [style] do today, but need an import and add a small cost. The Angular team recommends the native bindings in new code, and the ngClass and ngStyle directives remain supported for existing templates.

Choose by readability:

  • One or two conditional classes: use [class.x].
  • A computed set: put the logic in a computed and bind [class]="classes()". This keeps the template simple and testable.

Useful details: bindings do not remove classes that are not part of the binding, so unrelated classes from other sources stay. A style value of null removes the style. Class names with special characters such as md:flex work with [class] or [class.md:flex].

Do not build style strings by joining user input, since that can create broken or unsafe CSS.

typescript
import { Component, computed, signal } from '@angular/core';

@Component({
  selector: 'app-progress',
  template: `
    <input type="range" min="0" max="100" [value]="percent()" (input)="update($event)" aria-label="Progress" />

    <div class="bar" [class]="barClasses()" [style.width.%]="percent()" [style.min-width.px]="20">
      {{ percent() }}%
    </div>

    <p [class.warn]="percent() < 40" [class.done]="percent() === 100">{{ message() }}</p>
  `,
  styles: `
    .bar { background: #ccc; padding: 4px; }
    .good { background: #9fd49f; }
    .low { background: #f0b4b4; }
    .warn { color: darkorange; }
    .done { color: green; font-weight: bold; }
  `,
})
export class ProgressComponent {
  readonly percent = signal(30);

  readonly barClasses = computed(() => ({ good: this.percent() >= 60, low: this.percent() < 40 }));
  readonly message = computed(() => (this.percent() === 100 ? 'Completed' : 'Keep going'));

  update(event: Event): void {
    this.percent.set(Number((event.target as HTMLInputElement).value));
  }
}

Angular 20.3, strict template check passes. Moving the slider changes the bar width and the status class.

Key points

  • `[class.x]` toggles one class. `[class]` takes a string, array, or object.
  • `[style.prop.unit]` sets one style. `[style]` takes an object.
  • `ngClass` and `ngStyle` are legacy-style directives that still work.
  • A `computed` is the clean place for class logic.

Common mistakes

  • Importing `NgClass` only to toggle one class.
  • Putting long ternary expressions in the template instead of a `computed`.
  • Writing `[style.width]="50"` without the unit, so the browser ignores it.

Follow-ups

  • What happens to static classes when you use `[class]`?
  • How do you remove a style with a binding?
  • Why is `computed` better than a function call in a class binding?

Tips

  • Say which you use today and why, and that you can read `ngClass` code.
  • Remember the unit rule for numeric styles.
  • Keep the logic out of the template.

Evaluation rubric

  • Knows the class and style binding forms.
  • Describes ngClass and ngStyle as older but valid.
  • Uses a computed for complex logic.