# Built-in transitions and animations

- Published: 2026-11-29
- Tags: Why Svelte, Svelte
- URL: https://devtrillo.com/writing/why-svelte-10-built-in-transitions-and-animations/

> Part 10 of a series on Svelte in 2026. [Part 9](/writing/why-svelte-9-svelte-best-practices-from-the-docs/) tested the official best-practices page. This part covers the animation features Svelte ships in the box: `transition:`, `in:` and `out:`, custom transitions, and `animate:`.

Most sites I've worked on have some version of this in the global stylesheet:

```css title="reset.css"
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
```

I loaded it in Chromium with reduced motion turned on, mounted a component with `transition:fade={{ duration: 1000 }}`, and hid it. 500ms later the element was still in the DOM at opacity 0.52. The reset did nothing.

The docs admit this in their accessibility section. Transitions are "driven by the Web Animations API rather than by CSS," so a rule that zeroes `animation-duration` has "no effect on them."[^transition] That sentence explains most of how Svelte transitions behave, so this part starts from it.

## A transition is a function of `t`

Using a built-in one is a directive and an import:

```svelte
<script>
  import { fade } from "svelte/transition";
  let visible = $state(false);
</script>

<button onclick={() => (visible = !visible)}>toggle</button>

{#if visible}
  <div transition:fade={{ duration: 1000 }}>fades in and out</div>
{/if}
```

`svelte/transition` has seven: `blur`, `crossfade`, `draw`, `fade`, `fly`, `scale` and `slide`.[^module]

A custom one is a function that gets the node and your parameters and returns a config. The field that matters is `css`. It takes `t`, from 0 to 1 after easing, and `u`, which is `1 - t`, and returns a style string. Here's the one I tested, with a line that records every call:

```svelte title="Grow.svelte"
<script>
  let visible = $state(false);

  function grow(node, { duration = 1000 } = {}) {
    return {
      duration,
      css: (t, u) => {
        window.calls.push(t);
        return `transform: scale(${t}); opacity: ${t}`;
      },
    };
  }
</script>

{#if visible}
  <div transition:grow>grow</div>
{/if}
```

The docs say this function "is called repeatedly _before_ the transition begins."[^transition] I wanted to know how repeatedly. Right after I set `visible = true` there was one call, with `t` at 0, for the starting frame. By 100ms in there were 62. At the end of the second there were still 62.

The element's Web Animation had 61 keyframes. Svelte 5.57.2 samples your function about 60 times per second of duration, hands the list to `element.animate()`, and never calls `css` again.[^source] Your function runs before the animation, not during it.

The other option, `tick`, gets the same `t` but runs on every animation frame. You need it for things CSS can't express, like the docs' typewriter that slices `textContent`. My `tick` version had been called 7 times by 100ms and 62 by the end.

Then I blocked the main thread for 600ms in the middle of each one. The `tick` transition got no calls during the block, and its next call jumped from `t` = 0.033 to 0.667. The text skipped two-thirds of its letters in one frame. The `css` transition got zero calls too, but it had none left to make, and the animation's clock read 650ms when the thread came back. That's why the docs say to use `css` "if it's possible": the keyframes already exist, so the browser can play them "off the main thread."[^transition]

## The reset, and the fix

So the reduced-motion rule fails because Svelte never creates a CSS animation for it to override. The animation comes from `element.animate()` with explicit keyframes and an explicit duration, and the stylesheet doesn't get a vote.

The fix is `prefersReducedMotion` from `svelte/motion`, added in 5.7.0.[^changelog] It's a reactive media query, so you can read it in the transition parameters:

```svelte
<script>
  import { fade } from "svelte/transition";
  import { prefersReducedMotion } from "svelte/motion";
</script>

{#if visible}
  <div transition:fade={{ duration: prefersReducedMotion.current ? 0 : 1000 }}>…</div>
{/if}
```

With reduced motion on, that element was gone 50ms after I hid it. With it off, it was still fading at 500ms. A duration of 0 skips the animation entirely, so nothing gets sampled.[^source]

My complaint is that you have to remember this on every transition. The other way is to wrap the built-ins once, in your own module, and import from there. I'd do the wrapper.

**Quiz:** Your global CSS zeroes animation-duration under prefers-reduced-motion: reduce. A user with reduced motion on closes a dialog that has out:fly={{ y: 200, duration: 400 }}. What do they see?

- The dialog disappears at once, because the reset zeroes the duration
- The full 400ms fly-out
- A fade, because Svelte swaps motion for opacity under reduced motion

**Answer:** The full 400ms fly-out — Svelte passes 400ms straight to the Web Animations API. To honor the setting, read prefersReducedMotion.current from svelte/motion and pass a duration of 0.

- Not "The dialog disappears at once, because the reset zeroes the duration": The reset only reaches CSS animations and transitions. Svelte plays its transitions with element.animate() and passes the duration itself, so the stylesheet never sees it.
- Not "A fade, because Svelte swaps motion for opacity under reduced motion": Svelte doesn't change the transition for you. Whatever you pass is what plays.

## Leaving takes as long as the outro

When a block transitions out, everything inside it stays in the DOM "until every transition in the block has been completed."[^transition] That includes elements with no transition of their own. I checked with a 1000ms fade: the element was still there 500ms after I hid it, and gone at 1100ms.

Then I put two paragraphs in one block, one plain and one fading. The plain one sat there at full opacity for a second, then vanished with its sibling.

You can't fade an element you've already removed, so this is how outros have to work. It also breaks a test that does `expect(queryByText("Saved")).toBeNull()` right after a state change, because the node is still there. Wait for the `outroend` event, or give transitions a duration of 0 in tests.

## `transition:` reverses, `in:` and `out:` don't

`transition:` is bidirectional: if you hide an element in the middle of its intro, it turns around from where it is. `in:` and `out:` are separate, so the intro keeps playing "alongside the `out` transition" instead of reversing.[^inout] Using both on one element is a compile error, `transition_conflict`.

I gave one element `transition:fly={{ y: 200, duration: 1000 }}` and another `in:fly` plus `out:fade`, both 1000ms, and hid them 300ms into the intro. The `in:`/`out:` one took the full 1000ms to fade out.

The bidirectional one was gone after about 650ms. I expected 300. The source explains it: the reverse lasts `duration * Math.abs(delta)`, where `delta` is how far the eased `t` got.[^source] `fly` defaults to `cubicOut`, which covers 66% of the distance in the first 30% of the time. The way back is priced in distance, not time, so an ease-out transition takes longer to reverse than the clock suggests.

## Local by default, and the first render

Since Svelte 4, transitions are local: they play when their own block is created or destroyed, not when a block above it is.[^v4] Add `|global` to change that.

```svelte
{#if user}
  {#if open}
    <div transition:fade>local: only when open changes</div>
    <div transition:fade|global>global: when user or open changes</div>
  {/if}
{/if}
```

The same rule decides what plays on the first render. `mount()` plays intros by default, unless you pass `intro: false`.[^mount] I mounted a component with four fading paragraphs, two at the top level and two inside an `{#if}`. At 300ms three of them were at opacity 0.28. The local one inside the `{#if}` was at 1, because its block was created along with the component.

`hydrate()` goes the other way and sets `intro` to `false` unless you pass it, so server-rendered markup doesn't fade in on load.[^mount]

**Quiz:** A toast is rendered inside {#if toast} with transition:fly. That block sits inside {#if user}. The user signs out, so user becomes null. Does the toast fly out?

- Yes, every transition plays when its element leaves the DOM
- No, it disappears with the outer block
- Svelte waits for the toast's timeout before removing the outer block

**Answer:** No, it disappears with the outer block — The {#if toast} block didn't change; the {#if user} block above it was destroyed. A local transition skips that. Write transition:fly|global if you want it to play.

- Not "Yes, every transition plays when its element leaves the DOM": That was Svelte 3's default. Since Svelte 4, transitions are local, so they only play when their own block changes.
- Not "Svelte waits for the toast's timeout before removing the outer block": Svelte waits for transitions, not timers, and this local transition doesn't run at all.

## `animate:` is for moving, not arriving

`animate:` runs when items in a keyed each block change position. "Animations do not run when an element is added or removed."[^animate] The built-in is `flip`, from `svelte/animate`.

I reordered `[1, 2, 3]` to `[3, 1, 2]` with `animate:flip={{ duration: 1000 }}` on each `<li>`, and every item got a Web Animation. One frame after the reorder, item 3 was first in the DOM but still drawn 36px lower, where it used to be.

The compiler is strict about placement. An unkeyed each block fails with `animation_missing_key`. Put the `animate:` element inside a wrapper, or next to a sibling, and you get `animation_invalid_placement`: it "must be the only child of a keyed `{#each ...}` block." Part 9 already argued for keying every list, so this costs you nothing.

For removals, add an `out:` transition to the same element. With `out:fade` on the `<li>`, I removed the first item. Halfway through, it was still in the DOM at opacity 0.52 with `position: absolute`, and `flip` had slid the other two most of the way up into its place.

## React has this now, with conditions

For years the React answer to "fade this out when it unmounts" was a library. React removes the node, and then there's nothing left to animate. React 19.3, out in September 2026, made `<ViewTransition>` stable, and it does animate enters and exits.[^react]

The catch is in the release post: updates that aren't marked as Transitions "don't trigger animations."[^react] I wrapped a paragraph in `<ViewTransition>` in React 19.3.1 and put a counter on `document.startViewTransition`. A plain `setShow(false)` removed the paragraph with zero calls. The same update inside `startTransition` made one.

The designs differ more than the APIs do. React hands the work to the browser's View Transition API, which animates snapshots of the old and new page. Svelte animates the real element and keeps it in the DOM until it's done. In Svelte you write the frames yourself, and the reduced-motion setting is your job too.

## What I'd take from this

Svelte's transition API is small: you write `t => css`, and Svelte does the sampling and the bookkeeping for nodes that enter and leave. Everything that surprised me in the lab came from that one choice. Precomputed keyframes keep going when the main thread is busy, and `tick` skips frames. Your stylesheet can't shorten them. An element that's leaving is still really there.

Read the [`transition:`](https://svelte.dev/docs/svelte/transition) page, which is short, then the [`svelte/transition`](https://svelte.dev/docs/svelte/svelte-transition) reference for each built-in's parameters. And add `prefersReducedMotion` before you ship your first fade.

Next: scoped styles, and what the compiler does to your CSS.

[^transition]: Svelte docs, [`transition:`](https://svelte.dev/docs/svelte/transition).

[^module]: Svelte docs, [`svelte/transition`](https://svelte.dev/docs/svelte/svelte-transition).

[^source]: Svelte 5.57.2, [`transitions.js`](https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/internal/client/dom/elements/transitions.js), the `animate` function.

[^changelog]: [Svelte changelog](https://github.com/sveltejs/svelte/blob/main/packages/svelte/CHANGELOG.md), 5.7.0: "add reactive `MediaQuery` class, and a `prefersReducedMotion` class instance" ([#14422](https://github.com/sveltejs/svelte/pull/14422)). See also [`svelte/motion`](https://svelte.dev/docs/svelte/svelte-motion#prefersReducedMotion).

[^inout]: Svelte docs, [`in:` and `out:`](https://svelte.dev/docs/svelte/in-and-out).

[^v4]: Svelte docs, [Svelte 4 migration guide: transitions are local by default](https://svelte.dev/docs/svelte/v4-migration-guide#Transitions-are-local-by-default).

[^mount]: Svelte 5.57.2, [`render.js`](https://github.com/sveltejs/svelte/blob/main/packages/svelte/src/internal/client/render.js): `mount` defaults `intro` to `true` ("Transitions will play during the initial render unless the `intro` option is set to `false`"), `hydrate` to `false`. See also [Imperative component API](https://svelte.dev/docs/svelte/imperative-component-api).

[^animate]: Svelte docs, [`animate:`](https://svelte.dev/docs/svelte/animate).

[^react]: React blog, [React 19.3](https://react.dev/blog/2026/09/09/react-19-3) (September 9, 2026); React docs, [`<ViewTransition>`](https://react.dev/reference/react/ViewTransition).

- **Part 11 — Scoped styles: CSS that stays put**: What the compiler does to a component's <style> block: the scoping class, deleted selectors, the specificity bump, and when :global is the right way out
