Built-in transitions and animations

By Esteban Trillo8 min readWhy SvelteSvelte

Svelte turns your function into a reel of keyframes up front and hands it to the browser. The CSS reset on the wall isn't wired to that projector.
Svelte turns your function into a reel of keyframes up front and hands it to the browser. The CSS reset on the wall isn't wired to that projector.

Part 10 of a series on Svelte in 2026. Part 9 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:

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.”1 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:

<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.2

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:

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.”1 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.3 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.”1

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.4 It’s a reactive media query, so you can read it in the transition parameters:

<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.3

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.

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?

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.”1 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.5 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.3 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.6 Add |global to change that.

{#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.7 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.7

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?

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.”8 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.9

The catch is in the release post: updates that aren’t marked as Transitions “don’t trigger animations.”9 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: page, which is short, then the 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.

  • 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

Footnotes

  1. Svelte docs, transition:. ↩ ↩2 ↩3 ↩4

  2. Svelte docs, svelte/transition. ↩

  3. Svelte 5.57.2, transitions.js, the animate function. ↩ ↩2 ↩3

  4. Svelte changelog, 5.7.0: “add reactive MediaQuery class, and a prefersReducedMotion class instance” (#14422). See also svelte/motion. ↩

  5. Svelte docs, in: and out:. ↩

  6. Svelte docs, Svelte 4 migration guide: transitions are local by default. ↩

  7. Svelte 5.57.2, 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. ↩ ↩2

  8. Svelte docs, animate:. ↩

  9. React blog, React 19.3 (September 9, 2026); React docs, <ViewTransition>. ↩ ↩2

Read next

  1. Svelte best practices from the docs, testedNov 29, 2026 Why Svelte · 7 min
  2. Shared state in .svelte.ts modulesNov 29, 2026 Why Svelte · 7 min
  3. Scoped styles: CSS that stays putNov 30, 2026 Why Svelte · 9 min
  4. SvelteKit fundamentals: routes and layoutsNov 30, 2026 Why Svelte · 8 min
  5. Form actions and progressive enhancementNov 30, 2026 Why Svelte · 9 min
Piwi hands a single letter to the user at a mailbox; a small note reads no noise.

Get the next one in your inbox

One email when a post goes out, with its sketch. Nothing in between, and one link to leave.

Sign in faster next time?

Add a passkey and skip the email link. Your fingerprint, face or device PIN is enough.