Skip to main content

CSS Animations & Transitions

Entry & Exit Transitions

Animate elements as they enter, leave, or move through the top layer with starting styles and discrete property transitions. It covers first-render transitions, discrete properties, popovers, dialogs, top-layer removal, and reduced-motion handling.

A normal CSS transition cannot animate something that has just appeared, because there is no "before" to animate from. @starting-style gives the browser that missing before, so new elements and popups can fade or slide in with pure CSS.

Exiting is the other half: display: none normally switches off instantly, so a fade-out gets cut off. A newer setting lets display wait until the fade has finished. This builds on CSS Transitions, and for animating whole page changes see View Transitions.

Fade in when it appears

Example

css

.notice {
  opacity: 1;
  transform: translateY(0);
  transition: opacity 200ms, transform 200ms;
}

@starting-style {
  .notice {
    opacity: 0;
    transform: translateY(0.75rem);
  }
}

The notice's normal state is fully visible. @starting-style says what it looks like at the very first moment, transparent and slightly lower, so the browser animates from there to the normal state. Put @starting-style after the normal rule, because both have the same specificity and the later one wins.

Fade out before it disappears

Example

css

[popover] {
  opacity: 0;
  transform: scale(0.96);
  transition:
    opacity 180ms,
    transform 180ms,
    display 180ms allow-discrete,
    overlay 180ms allow-discrete;
}

[popover]:popover-open {
  opacity: 1;
  transform: scale(1);
}

@starting-style {
  [popover]:popover-open {
    opacity: 0;
    transform: scale(0.96);
  }
}

allow-discrete lets display and overlay take part in the transition. Instead of switching off at the start, they switch at the end, after the opacity and scale have finished. Together with @starting-style for the opening, the popover now fades and scales both in and out.

Entry Transition Reference

SyntaxPurposeExample
@starting-styleDefines the first rendered style.@starting-style { .item { opacity: 0; } }
transition-behaviorAllows discrete properties to participate.transition-behavior: allow-discrete
:popover-openSelects an open popover.[popover]:popover-open
overlayDefers removal from the top layer.transition: overlay 180ms allow-discrete

Complete Example

Complete runnable example

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Entry & Exit Transitions example</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 2rem;
      font-family: system-ui, sans-serif;
      line-height: 1.5;
    }
    main { max-width: 70rem; margin-inline: auto; }
    article, section, aside, form, .card, .panel {
      padding: 1rem;
      border: 1px solid #cbd5e1;
      border-radius: 0.6rem;
    }

.notice {
  opacity: 1;
  transform: translateY(0);
  transition: opacity 200ms, transform 200ms;
}

@starting-style {
  .notice {
    opacity: 0;
    transform: translateY(0.75rem);
  }
}

[popover] {
  opacity: 0;
  transform: scale(0.96);
  transition:
    opacity 180ms,
    transform 180ms,
    display 180ms allow-discrete,
    overlay 180ms allow-discrete;
}

[popover]:popover-open {
  opacity: 1;
  transform: scale(1);
}

@starting-style {
  [popover]:popover-open {
    opacity: 0;
    transform: scale(0.96);
  }
}
  </style>
</head>
<body>
  <main><button popovertarget="transition-demo">Open notice</button><div id="transition-demo" popover class="notice">This popover uses entry and exit transitions.</div></main>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
@starting-style11711712917.5
transition-behavior11711712917.4

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.

Good to know

  • In browsers without support, elements simply appear and disappear. Nothing is lost.
  • Popovers and dialogs must still work with the keyboard and move focus correctly; the animation is only decoration.
  • Keep entry animations short and turn them off for people who prefer reduced motion.

Entry & Exit Transitions exercises

Discrete properties too

Let the panel transition every property over 0.3s, also properties like display, with transition-behavior allow-discrete.

The HTML your code works on
<style>
  .panel { background: teal; color: white; padding: 12px; }
</style>
<div class="panel">Menu</div>

Result

Check your answer to see it here.