Skip to content
Tutorials Center
My learning

Topics

Lesson 13 of 74 ยท Templates and Data Binding

Animations in Angular

Animate elements as they enter, leave and change state in Angular with animate.enter and animate.leave, the classic trigger API, route transitions and reduced-motion support.

Motion tells users what just happened: a row slides in after you add it, a dialog fades out when you close it, a menu grows from its button. Good animation is short and purposeful. Bad animation is slow and gets in the way, so this lesson also covers how to switch it off for people who ask for that.

Angular has two ways to animate. The modern one, added in Angular 20.2, uses plain CSS and two template attributes, animate.enter and animate.leave. The classic one is the @angular/animations package with trigger(), state() and transition(). The classic package still works in a lot of existing code, but the Angular team now recommends CSS, so we start there. You should know Control Flow and Event Binding first.

Real versus stand-in. The typescript, html and css blocks are real Angular code to read and copy into a project made with ng new; the site cannot compile Angular. The runnable examples are stand-ins: a TypeScript program that works out what a transition would do, and a plain JavaScript page that uses the same CSS-class idea in your browser. Check the Angular animation guide for the exact API of the version you install, because this area changed recently.

Animating entering and leaving with CSS

Elements that Angular adds or removes (an @if block turning on, an @for row appearing) have no "before" or "after" in the DOM, which is what makes them hard to animate. animate.enter adds a CSS class when the element is inserted; animate.leave adds a class when it is about to be removed and delays the removal until the animation finishes.

CSS
/* styles.css or the component's styles */
.fade-in {
  animation: fade-in 200ms ease-out;
}
.fade-out {
  animation: fade-out 150ms ease-in forwards;
}
@keyframes fade-in {
  from { opacity: 0; transform: translateY(-8px); }
}
@keyframes fade-out {
  to { opacity: 0; transform: translateY(-8px); }
}
TypeScript
import { Component, signal } from '@angular/core';

@Component({
  selector: 'app-notice',
  template: `
    <button type="button" (click)="open.set(!open())">Toggle notice</button>

    @if (open()) {
      <p class="notice" animate.enter="fade-in" animate.leave="fade-out">
        Saved. You can close this at any time.
      </p>
    }
  `,
})
export class Notice {
  protected readonly open = signal(false);
}

Angular waits for the CSS animationend or transitionend event on the leaving element and its animated children, then removes it. You can also use a CSS transition instead of keyframes: put the starting values on the enter class and the final values in the element's normal style. Both attributes accept several classes separated by spaces, and you can bind them to an expression such as [animate.enter]="enterClass()".

If you need to run code, for example to drive a JavaScript animation library, bind an event: (animate.leave)="onLeave($event)". The handler receives an event whose target is the element, and you tell Angular you are done by calling event.animationComplete().

An animation that never ends blocks removal. If the leave class does not define an animation or transition that actually runs (a typo in the class name, or display: none), Angular has nothing to wait for. Keep leave durations short and test the class in the browser's dev tools.

Animating a change of state with plain CSS

Many animations do not involve adding or removing anything: a panel opens, a card is selected, a button shows progress. For those, bind a class or style to a signal and let a CSS transition do the work.

TypeScript
@Component({
  selector: 'app-panel',
  template: `
    <button type="button" (click)="expanded.set(!expanded())"
            [attr.aria-expanded]="expanded()">Details</button>
    <div class="body" [class.open]="expanded()">
      <p>Hidden until the panel opens.</p>
    </div>
  `,
  styles: `
    .body { display: grid; grid-template-rows: 0fr; transition: grid-template-rows 250ms ease; }
    .body > p { overflow: hidden; margin: 0; }
    .body.open { grid-template-rows: 1fr; }
  `,
})
export class Panel {
  protected readonly expanded = signal(false);
}

Transitioning a grid row from 0fr to 1fr is a well-known trick for animating height without knowing the height. The browser does the interpolation, so Angular has no extra work.

The classic trigger API

Older Angular code uses @angular/animations. You enable it with provideAnimationsAsync() in app.config.ts, declare a trigger in the component, and bind it to an element in the template. A trigger is a small state machine: named states hold styles, and transitions say how to animate between them.

TypeScript
import { Component, signal } from '@angular/core';
import { animate, state, style, transition, trigger } from '@angular/animations';

@Component({
  selector: 'app-card',
  template: `
    <div [@selected]="isSelected() ? 'on' : 'off'" (click)="isSelected.set(!isSelected())">
      Click me
    </div>
    @if (visible()) {
      <p @fade>I fade in and out.</p>
    }
  `,
  animations: [
    trigger('selected', [
      state('off', style({ background: 'white', transform: 'scale(1)' })),
      state('on', style({ background: 'lightblue', transform: 'scale(1.05)' })),
      transition('off <=> on', animate('150ms ease-in-out')),
    ]),
    trigger('fade', [
      transition(':enter', [style({ opacity: 0 }), animate('200ms', style({ opacity: 1 }))]),
      transition(':leave', [animate('150ms', style({ opacity: 0 }))]),
    ]),
  ],
})
export class Card {
  protected readonly isSelected = signal(false);
  protected readonly visible = signal(true);
}

:enter and :leave are aliases for void => * and * => void. The same API also offers keyframes() for multi-step motion, query() to animate children and stagger() to delay each child a little more than the previous one, which is how list items slide in one after another.

If your project already uses it, you do not have to rewrite everything today. For new work, prefer CSS and animate.enter/animate.leave: no extra package, no extra JavaScript, and the browser can run CSS animations off the main thread.

Route transitions with the View Transitions API

Animating between pages is the hardest case, because the old page and the new page exist at different moments. Browsers now have a View Transitions API that takes a snapshot of the old view, lets the DOM change, and cross-fades to the new one. The router can start these transitions for you:

TypeScript
import { provideRouter, withViewTransitions } from '@angular/router';

export const appConfig = {
  providers: [provideRouter(routes, withViewTransitions())],
};

With that single feature you get a default cross-fade on every navigation in browsers that support the API; other browsers just navigate as before. You can customize it in global CSS with the ::view-transition-old(root) and ::view-transition-new(root) pseudo-elements, or give an element a view-transition-name so it morphs between pages (a thumbnail that grows into a hero image). See Router Basics for where provideRouter lives.

Respect reduced motion

Some users get dizzy or distracted from animation and turn on "reduce motion" in their operating system. Browsers expose that as a media query, and one rule respects it everywhere:

CSS
@media (prefers-reduced-motion: reduce) {
  .fade-in, .fade-out { animation-duration: 1ms; }
  .body { transition: none; }
}

Use a duration of about 1 ms rather than animation: none, so animationend still fires and animate.leave can finish its work. This is part of Security and Accessibility: motion should add information, never be the only information.

Stand-in 1: what does a transition do?

The first stand-in is a tiny state machine in the spirit of trigger(). You declare states and transitions, and it tells you which transition applies for a state change (including the <=> and :enter/:leave shorthands) and what the intermediate style values would be at a few moments of the animation.

TypeScript A stand-in: trigger states and transitions

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or readFileSync(0)), it gets the next line from this box.

The two-way arrow produces both directions, a change to the same state has no transition at all, and every number moves smoothly from one state's value to the other's. That is the whole idea of the classic API; CSS gives you the same result with less code.

Stand-in 2: enter and leave with classes

The second stand-in runs in your browser and uses the same recipe as animate.enter and animate.leave: add an "enter" class when the element appears, and when it should go, add a "leave" class and remove the element only after animationend. Angular does the waiting for you; here we write the three lines ourselves so you can see them.

Enter and leave classes, removal after animationend

Add a few items and click one: it slides away, and only then does it leave the DOM. The log line appears at the end of the animation, which is exactly the moment Angular removes the element for you.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does animate.leave do that a plain CSS class on the element cannot?

    When Angular removes an element, it is gone at once. animate.leave adds your classes and waits for the animation or transition to finish before the removal happens.
  2. 2. Which approach does Angular now recommend for new animations?

    CSS animations need no extra package and can run off the main thread. The older trigger API still works for existing code.
  3. 3. How do you turn on default cross-fades between routes?

    withViewTransitions() makes the router use the browser's View Transitions API. Browsers without the API simply navigate without the effect.
  4. 4. Why use animation-duration: 1ms instead of animation: none for reduced motion?

    The leave animation is considered finished when animationend fires. With animation none no event fires, so a leaving element might never be removed.

Summary

  • animate.enter adds CSS classes when an element is inserted; animate.leave adds classes and waits for the animation to end before removing the element.
  • For changes of state, bind a class or style to a signal and use a CSS transition.
  • The classic trigger(), state(), transition(), keyframes(), query() and stagger() API in @angular/animations remains in many projects, but new code should prefer CSS.
  • withViewTransitions() gives route changes a cross-fade through the browser's View Transitions API.
  • Honor prefers-reduced-motion, and keep animations short.

Next: change what users can see without a full page change in Router Basics, or learn how components talk to each other in Inputs and Outputs.