---
title: "Animations"
description: "CSS-only enter and exit animations using @starting-style, allow-discrete transitions, and ::details-content."
slug: "animations"
---

import { DocPage, PageHeader, SkillPanel, Example, ExampleLabel, ExampleHint, ExampleCode, SourceFiles, SourceNote, StatesSection, CodeBlock, Demo, DemoCode, ChangelogEntries, ArchBody, StatsClaim, StatsCards, PageOverlay, DocText, DocLink, H2, SkillField, SkillText, SkillRow, SkillGrid, Variant, ApiPill, Dim, CodeWindow, CodeCard, TableCard } from "../../lib/components/index";

<DocPage meta={meta}>

<PageHeader slug="animations" title="Animations &amp; Transitions" introStyle="max-width:38rem;">
  All enter and exit animations are CSS-only. No JavaScript class toggling, no animation libraries - just <code>@starting-style</code>, <code>allow-discrete</code> transitions, and native CSS features.
</PageHeader>

<H2>The challenge</H2>

<p class="text-sm text-muted-foreground mb-5">Animating elements that go from <code>display: none</code> to visible (popovers, dialogs, accordions) has historically been impossible in CSS. The element either exists or doesn't - there was no in-between state to transition through. Libraries solved this with JS class toggling and <code>requestAnimationFrame</code> hacks.</p>

<p class="text-sm text-muted-foreground mb-5">Modern CSS solves this with two new features: <code>@starting-style</code> (defines the initial state before an element appears) and <code>allow-discrete</code> (lets <code>display</code> participate in transitions).</p>

<div class="sep" />

<H2>@starting-style</H2>

<p class="text-sm text-muted-foreground mb-3">The <code>@starting-style</code> at-rule defines the values a property should start from when the element first appears (enters the top layer, gains <code>display</code>, gets inserted into the DOM). The browser applies these values first, then transitions to the element's actual computed styles.</p>

<p class="text-sm text-muted-foreground mb-5">
  <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/@starting-style" target="_blank" rel="noopener" class="btn" data-variant="link">MDN: @starting-style</a>
</p>

<CodeWindow lang="css" title="how @starting-style works" mb="1.5rem" code={"/* 1. The element's normal (visible) state */\n.popover:popover-open {\n  opacity: 1;\n  translate: 0 0;\n}\n\n/* 2. The starting state - before the transition begins */\n@starting-style {\n  .popover:popover-open {\n    opacity: 0;\n    translate: 0 -4px;\n  }\n}\n\n/* Flow: @starting-style values → transition → :popover-open values\n   Result: a smooth fade-in + slide-up animation */"} />

<div class="sep" />

<H2>allow-discrete transitions</H2>

<p class="text-sm text-muted-foreground mb-3">The <code>display</code> property was historically not animatable - it snaps between <code>none</code> and <code>block</code> instantly. The <code>allow-discrete</code> keyword in the <code>transition</code> shorthand tells the browser to keep the element visible during the exit transition before hiding it.</p>

<p class="text-sm text-muted-foreground mb-5">
  <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/transition-behavior" target="_blank" rel="noopener" class="btn" data-variant="link">MDN: transition-behavior</a>
</p>

<CodeWindow lang="css" title="transition with allow-discrete" mb="1.5rem" code={".popover {\n  /* Animate opacity, translate, AND display */\n  transition:\n    opacity 150ms ease,\n    translate 150ms ease,\n    display 150ms allow-discrete;\n\n  /* Exit state: invisible */\n  opacity: 0;\n  translate: 0 -4px;\n}\n\n/* Without allow-discrete: element disappears instantly on close\n   With allow-discrete: element fades out, THEN display changes to none */"} />

<div class="sep" />

<H2>Popover &amp; dialog animation pattern</H2>

<p class="text-sm text-muted-foreground mb-5">Most overlay components (popover, dropdown, tooltip, dialog, sheet, toast) follow the same three-part animation recipe. Here's the complete pattern used by the Popover component:</p>

<CodeWindow lang="css" title="complete popover animation - CSS only" mb="1.5rem" code={"@layer components {\n  .popover {\n    /* 1. Base state (also the exit target) */\n    opacity: 0;\n    translate: 0 -4px;\n    transition:\n      opacity 150ms ease,\n      translate 150ms ease,\n      display 150ms allow-discrete;\n\n    /* 2. Open state - what it transitions TO */\n    &:popover-open {\n      opacity: 1;\n      translate: 0 0;\n    }\n  }\n\n  /* 3. Starting style - what it transitions FROM */\n  @starting-style {\n    .popover:popover-open {\n      opacity: 0;\n      translate: 0 -4px;\n    }\n  }\n}"} />

<div style="display:grid;grid-template-columns:auto 1fr;gap:0.5rem 1rem;margin-bottom:2.5rem;align-items:baseline;">
  <span class="def-term">Enter</span>
  <span class="text-sm text-muted-foreground"><code>@starting-style</code> values → transition → <code>:popover-open</code> values. The popover fades in and slides up.</span>
  <span class="def-term">Exit</span>
  <span class="text-sm text-muted-foreground"><code>:popover-open</code> values → transition → base state values. The <code>allow-discrete</code> on <code>display</code> keeps the element visible during the fade-out.</span>
</div>

<div class="sep" />

<H2>Dialog &amp; sheet animations</H2>

<p class="text-sm text-muted-foreground mb-5">Dialogs and sheets use the same pattern but with <code>dialog\[open\]</code> instead of <code>:popover-open</code>, and they also animate the <code>::backdrop</code> pseudo-element.</p>

<CodeWindow lang="css" title="dialog with backdrop animation" mb="1.5rem" code={"dialog.dialog {\n  opacity: 0;\n  translate: 0 -0.5rem;\n  scale: 0.98;\n  transition:\n    opacity 200ms ease,\n    translate 200ms ease,\n    scale 200ms ease,\n    display 200ms allow-discrete;\n\n  &[open] {\n    opacity: 1;\n    translate: 0 0;\n    scale: 1;\n  }\n\n  /* Backdrop fades in/out too */\n  &::backdrop {\n    background: oklch(0 0 0 / 0);\n    backdrop-filter: blur(0px);\n    transition: all 200ms ease,\n      display 200ms allow-discrete;\n  }\n\n  &[open]::backdrop {\n    background: oklch(0 0 0 / 0.45);\n    backdrop-filter: blur(3px);\n  }\n}\n\n@starting-style {\n  dialog.dialog[open] {\n    opacity: 0;\n    translate: 0 -0.5rem;\n    scale: 0.98;\n  }\n  dialog.dialog[open]::backdrop {\n    background: oklch(0 0 0 / 0);\n    backdrop-filter: blur(0px);\n  }\n}"} />

<p class="text-sm text-muted-foreground mb-5"><strong>Sheets</strong> use <code>translate</code> instead of <code>scale</code> - a sheet with <code>data-side="right"</code> slides in from <code>translateX(100%)</code>.</p>

<div class="sep" />

<H2>Accordion animations</H2>

<p class="text-sm text-muted-foreground mb-3">Accordions animate their height using the <code>::details-content</code> pseudo-element and the <code>block-size</code> property. This is one of the most-requested CSS features - animating <code>height: 0</code> to <code>height: auto</code> - now possible natively.</p>

<p class="text-sm text-muted-foreground mb-5">
  <a href="https://developer.mozilla.org/en-US/docs/Web/CSS/::details-content" target="_blank" rel="noopener" class="btn" data-variant="link">MDN: ::details-content</a>
</p>

<CodeWindow lang="css" title="accordion height animation - CSS only" mb="1.5rem" code={".accordion-item {\n  /* Target the collapsible content area */\n  &::details-content {\n    block-size: 0;\n    overflow-y: clip;\n    transition:\n      block-size 200ms ease,\n      content-visibility 200ms allow-discrete;\n  }\n\n  /* When open, expand to natural height */\n  &[open]::details-content {\n    block-size: auto;\n  }\n}\n\n/* Start from 0 when opening */\n@starting-style {\n  .accordion-item[open]::details-content {\n    block-size: 0;\n  }\n}"} />

<div style="display:grid;grid-template-columns:auto 1fr;gap:0.5rem 1rem;margin-bottom:2.5rem;align-items:baseline;">
  <span class="def-term">block-size: auto</span>
  <span class="text-sm text-muted-foreground">The browser calculates the natural height and smoothly transitions from <code>0</code> to that value. No JavaScript height measurement needed.</span>
  <span class="def-term">content-visibility</span>
  <span class="text-sm text-muted-foreground">The <code>allow-discrete</code> on <code>content-visibility</code> prevents the content from disappearing before the close animation finishes.</span>
</div>

<div class="sep" />

<H2>Programmatic exit animations</H2>

<p class="text-sm text-muted-foreground mb-5">Most components handle exit purely through CSS transitions. For cases where you need to run logic <em>after</em> the exit animation completes (like removing a toast element from the DOM), the Web Animations API provides a promise-based approach.</p>

<CodeWindow lang="javascript" title="web animations API - await exit before removal" mb="1.5rem" code={"// Animate out (Web Animations API), then remove through df$\nel.animate(\n  [\n    { opacity: 1, translate: '0 0' },\n    { opacity: 0, translate: '0 0.5rem' },\n  ],\n  { duration: 200, easing: 'ease', fill: 'forwards' },\n).finished.then(() => {\n  el.hidePopover();\n  df$(el).remove();\n});"} />

<p class="text-sm text-muted-foreground mb-5"><strong>Used by:</strong> Toast (dismiss animation before removing the element from the DOM)</p>

<div class="sep" />

<H2>Animation summary</H2>

<p class="text-sm text-muted-foreground mb-5">Which components use animations and what type of animation they use.</p>

<TableCard title="animation patterns by component" mb="2.5rem"><table class="table" data-variant="simple">
      <thead>
        <tr>
          <th class="whitespace-nowrap">Component</th>
          <th>Animation</th>
          <th>Technique</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>Popover</td>
          <td>Fade + slide up</td>
          <td><code>@starting-style</code> + <code>:popover-open</code></td>
        </tr>
        <tr>
          <td>Dropdown</td>
          <td>Fade + slide up</td>
          <td><code>@starting-style</code> + <code>:popover-open</code></td>
        </tr>
        <tr>
          <td>Tooltip</td>
          <td>Fade in</td>
          <td><code>@starting-style</code> + <code>:popover-open</code></td>
        </tr>
        <tr>
          <td>Dialog</td>
          <td>Fade + slide + scale</td>
          <td><code>@starting-style</code> + <code>dialog\[open\]</code> + <code>::backdrop</code></td>
        </tr>
        <tr>
          <td>Alert Dialog</td>
          <td>Fade + slide + scale</td>
          <td><code>@starting-style</code> + <code>dialog\[open\]</code> + <code>::backdrop</code></td>
        </tr>
        <tr>
          <td>Sheet</td>
          <td>Slide from edge</td>
          <td><code>@starting-style</code> + <code>dialog\[open\]</code> + <code>::backdrop</code></td>
        </tr>
        <tr>
          <td>Toast</td>
          <td>Slide in, slide out</td>
          <td><code>@starting-style</code> + Web Animations API for exit</td>
        </tr>
        <tr>
          <td>Accordion</td>
          <td>Height expand/collapse</td>
          <td><code>@starting-style</code> + <code>::details-content</code> + <code>block-size</code></td>
        </tr>
        <tr>
          <td>Collapsible</td>
          <td>Height expand/collapse</td>
          <td><code>@starting-style</code> + <code>::details-content</code> + <code>block-size</code></td>
        </tr>
        <tr>
          <td>Command</td>
          <td>Fade + scale up</td>
          <td><code>@starting-style</code> + <code>dialog\[open\]</code></td>
        </tr>
      </tbody>
    </table></TableCard>

<p class="text-sm text-muted-foreground mb-8"> All of these techniques are pure CSS (except toast exit). No animation libraries, no <code>requestAnimationFrame</code> hacks, no JS class toggling. See the <a href="native-web-apis.html" class="btn" data-variant="link">Native Web APIs</a> page for the broader set of platform APIs used across the system. </p>

</DocPage>
