Skip to content

[css-transitions-2] Exit animations (@exit-style) #9500

Open

Description

@jessicajaniuk

Problem statement

Elements are often removed from the DOM, either via JS (element.remove() etc), or by applying display: none to them. Some common examples: deleting items from lists, toasts, dismissing dialogs and popovers, etc.

Current solutions:

  • Listening to the animationend or transitionend event, filtering by the right transition/animation, and then removing the element
  • Setting a timeout, and manually matching the animation or transition duration and then removing the element after that timeout fires
  • Two CSS animations, one for the exit style, and one for applying display: none (using a transition for the latter wouldn't work)

All of these solutions are too heavyweight, and most require additional JS and possibly the allocation of an event listener which is a perf overhead.

Proposed solution

In #8174 the group added @starting-style, to solve the reverse of this problem and make it easier to specify entrance transitions. Something analogous to that, such as @exit-style (name TBB) would complement it nicely.

.posts {
  > li {
    transition: .4s opacity;

    @exit-style {
      opacity: 0;
    }
  }
}

Other potential names: @ending-style, @remove-style, @exiting-style, @removing-style

If renaming @starting-style would not cause compat issues (it just shipped in Blink and it has not shipped anywhere else), we could name them as a pair:

  • @start-style / @end-style
  • @entrance-style / @exit-style

Additionally, if there is a way to specify both entrance and exit styles, this simplifies a lot of other interactions that are technically neither, e.g. moving elements around.

Other solutions

Activity

  1. LeaVerou commented on Oct 19, 2023

    @LeaVerou
    Member

    I helped @jessicajaniuk flesh out and write this proposal, so obviously strong +1 for this!
    It’s very low hanging fruit, that addresses a very common author pain point.

    For context, Jessica works on the Angular team at Google, and thus us intimately familiar with how much of a common pain point this is.

    If renaming @starting-style would not cause compat issues (it just shipped in Blink and it has not shipped anywhere else), we could name them as a pair:

    I would be very surprised if it's too late to rename @starting-style given its current level of browser support.
    I think naming them together as a pair would lead to a more learnable, consistent design.
    If we're going to rename it we need to do it very soon though, so I’m going to Agenda+ this.

  2. Loirooriol commented on Oct 19, 2023

    @Loirooriol
    Contributor

    I don't see how this would work. The thing about @starting-style is that when an element becomes rendered, it gets the styles from @starting-style, and then transitions towards the normal styles.

    But with this, once the element stops being rendered, it immediately disappears, so no transition is possible. We would either need to delay the actual removal until the transition finishes, which seems to open a can of worms, or somehow detect that the element is going to be removed before it happens and start the transition then.

  3. chriscoyier commented on Oct 20, 2023

    @chriscoyier

    This would be excellent. The ability to animate an element as it ultimately leaves the DOM has been janky forever.

    I could be mistaken, but I think you can do this already with View Transitions, something to the tune of:

    Element.style.viewTransitionName = "unique-outgoing-ident";
    Element.remove();
    

    Then in CSS if you want control...

    ::view-transition-old(unique-outgoing-ident) {
      animation: outgoing 1s;
    }
    

    This example does that for list items "on the way out" of the DOM.

    We would either need to delay the actual removal until the transition finishes, which seems to open a can of worms

    I think the View Transitions thing avoids the "can of worms" because it still immediately removes the element from the DOM, and the animation takes place on a rasterized visual version of the element, right?

    Still, I think @exit-style is a more elegant API than View Transitions, because:

    1. The pairing with @starting-style is sensible.
    2. You need JS to make View Transitions work because the "outgoing" elements need entirely unique idents otherwise the transitions don't work at all. Plus, if the elements have "incoming" view transitions, you need to swap out the ident to a different one so it can have a different animation, which seems awkward. Seems like a concern best left to CSS alone.
    3. It could automatically trigger on situations other than leaving the DOM, as Jessica notes, like when an element becomes display: none;
  4. andruud commented on Oct 25, 2023

    @andruud
    Member

    I don't see how this would work.

    Easy, we add the vitality: auto | none property, make it transitionable, make it prefer the auto side during the transition, and spec in HTML that any element with no vitality gets removed at a certain point in the event loop.

    (worm-wriggling intensifies)

  5. LeaVerou commented on Oct 26, 2023

    @LeaVerou
    Member

    @Loirooriol

    I don't see how this would work.

    Yes, it's more challenging than @starting-style but it's such a common problem that I think it's worth spending some cycles on. Some ideas below.

    The thing about @starting-style is that when an element becomes rendered, it gets the styles from @starting-style, and then transitions towards the normal styles.

    But with this, once the element stops being rendered, it immediately disappears, so no transition is possible. We would either need to delay the actual removal until the transition finishes, which seems to open a can of worms, or somehow detect that the element is going to be removed before it happens and start the transition then.

    We cannot make removal async for a number of reasons. Beyond the obvious, one nonobvious reason is also that the exit style and the removal code may be written by different entities, and it's important for this to work well. E.g. I'm using a library which removes elements with no animation, so I write some styles to animate them.

    We could however delay it (sync) for the instant it would take to apply exit styles. Then, the transition effect would be entirely visual and the element would be taken out of the DOM immediately. Probably with some safeguard against infinite animations that could keep its ghost around forever. An easy such safeguard could be to simply not support animations, or cut them off when all transitions finish regardless of where they are in their playback.

    It could even be defined in terms of view transitions under the hood, just that the UA would generate it for you.

  6. Loirooriol commented on Oct 26, 2023

    @Loirooriol
    Contributor

    I haven't really been following view transitions, but yeah, using them under the hood sounds more reasonable I guess.

  7. dbaron commented on Oct 27, 2023

    @dbaron
    Member

    transition-behavior: allow-discrete was recently added (see #4441 and #8857 and Chrome blog post) to address what I think are the same set of use cases. [Edit: except for the DOM removal one.]

    @starting-style is special because transitions operate on the style before and after a style change, and browsers don't keep styles up-to-date for elements that are display:none -- so there's no record of what the style "before the change" was and it's not necessarily possible to reconstruct it after the change has happened. When something is changing to display: none that problem doesn't exist and we don't need the special construct; we can use existing transition mechanisms. What @starting-style does is allow transitions to operate on elements that are changing from none to other values of display; we don't need a special mechanism to allow transitions in the other direction, and they should just work.

  8. dbaron commented on Oct 27, 2023

    @dbaron
    Member

    But yes, actually removing an element from the DOM is different. It feels like a pretty significant layering violation to delay DOM operations from CSS. That said, maybe we shouldn't worry too much about that, but I'm inclined to think that if we want to improve the ergonomics of doing a transition that ends with the element being removed from the dom, we might want to think about an API on the DOM side to make that more ergonomic.

  9. dbaron commented on Oct 27, 2023

    @dbaron
    Member

    I should probably stop replying late at night, but now that I've reread #9500 (comment) more carefully I see Lea's point about immediate DOM removal being important -- and given that I agree that something built on top of view transitions is probably best. But I could imagine building it on top of view transitions in interesting ways.

    (Sorry for the multiple comments.)

  10. chrishtr commented on Oct 27, 2023

    @chrishtr
    Contributor

    I should probably stop replying late at night, but now that I've reread #9500 (comment) more carefully I see Lea's point about immediate DOM removal being important -- and given that I agree that something built on top of view transitions is probably best. But I could imagine building it on top of view transitions in interesting ways.

    I can also see the developer difficulty, but keeping track of a now-gone DOM element and where it used to be until the end of the animation is very difficult/maybe impossible. Even keeping it until the next render and a View Transition can start is hard and in general almost as hard. The alternative of stalling the rendering pipeline to get a screenshot of the pixels for a View Transition is possible, but that comes at a very high cost of forced layout, stalled pipeline, and memory impact.

    OTOH the developer code to do it async would be something like:

    startViewTranstion(() => { removeNode(); });
    

    (setting it to display:none is already doable with allow-discrete has the same effect visually but of course not in terms of DOM state)

    Which is certainly not as easy for the developer to deal with, but also seems feasible.

  11. dbaron commented on Oct 28, 2023

    @dbaron
    Member

    One question in this context: how useful to developers is it if the DOM removal is async, but still at the start time of the animation (rather than the end).

  12. LeaVerou commented on Oct 28, 2023

    @LeaVerou
    Member

    @chrishtr

    I can also see the developer difficulty, but keeping track of a now-gone DOM element and where it used to be until the end of the animation is very difficult/maybe impossible. Even keeping it until the next render and a View Transition can start is hard and in general almost as hard. The alternative of stalling the rendering pipeline to get a screenshot of the pixels for a View Transition is possible, but that comes at a very high cost of forced layout, stalled pipeline, and memory impact.

    OTOH the developer code to do it async would be something like:

    startViewTranstion(() => { removeNode(); });
    

    First, it's not quite a one liner, it's more like:

    if (document.startViewTransition) {
    	await document.startViewTransition(() => { el.remove() }).finished;
    }
    else {
    	el.classList.add("removing");
    	await new Promise(r => el.addEventListener("transitionend", r, {once: true}));
    	el.remove();
    }

    But that could be abstracted away. More importantly it's about coupling.
    Yes, if you are the same entity writing the element removal code it's trivial to replace it with a view transition. But that's not usually the case. Authors these days use long pipelines of tooling to do these things, they rarely write DOM element removal code themselves. E.g. I would have no idea where to even start to use a view transition to animate the removal of an element that’s removed via Vue’s v-if or a React conditional.

    @dbaron

    One question in this context: how useful to developers is it if the DOM removal is async, but still at the start time of the animation (rather than the end).

    I think my comment above may reply to this too? If not, could you expand on what you mean a bit more, ideally with a code example?

  13. argyleink commented on Oct 30, 2023

    @argyleink

    I agree a sugar sweet way to specify an animation to play for DOM mutations would be useful. I've been thoroughly enjoying view transitions for this, and am very familiar with the naming dances that need to happen for it all to work. I'm going to try and share where my mind is dwelling on when comparing exit-style and view transitions, and where each can shine.

    For dom mutations and providing visual feedback to users, here's some of the transitions that are common:

    1. transition out / exit stage: remove()
    2. transition in / enter stage: append()
    3. transition to a new location / move on the stage: combo of remove and append
    4. transition to a new visual style: .style = '…' or classList.toggle()
    5. reduced motion

    View Transitions has ways to handle all these scenarios, and i think with exit-style i'm most curious about configuration for when it shouldn't run. Like, if an element is moving position in a list, due to sort/filter/removal/etc. Would exit-style be a bit too blunt of an instrument for this case, as the elements would animate out (because they were removed) and then animate in again? If so, would I need to add a class to a "moving" element and ensure in my CSS that exit-style isn't applying to those? Now JS is articulating edge case handling, which is something we're trying to avoid here?

    To me, seems like exit-style is sugar for "certain" node removal scenarios and not all of them. And to prevent it from running I'd need to intervene with JS classes to inform selectors? It's almost like I'd like a way to tell exit-style which DOM mutations to observe and intercept for the exit animation.

    Meanwhile, view transitions have this logic already figured out. Authors can use CSS, no JS required beyond the call .startViewTransition(), to distinguish and customize exit stage, enter stage, and move within stage. The tradeoff is naming, but afaik, angular and nearly every front-end framework now has unique ID tracking of loop rendered lists, which can make naming trivial.

    maybe there's a handoff point between exit-style and view transitions? exit-style for when you know the elements are only going to be removed and you want to animate it; view transitions when they might morph or change position in the dom?

    there's also allow-discrete which has some super powers in that it can delay the transition of elements in overlay or not, in addition to delaying display:none until just the right moment. lots of moving parts here!

    tldr;
    my biggest question is how do i tell exit-style when not to run? i can foresee moments where it'd run at times i'd like it not to, like sorting a list.

  14. 13 remaining items

  15. LeaVerou commented on Jun 27, 2025

    @LeaVerou
    Member

    So we could potentially define a new syntax for an "exit animation" that is syntactic sugar for the above. A browser implementation might be quite tricky or expensive though, since gathering a screenshot on every update to child is expensive, and a synchronous delay to gather a screenshot before removing the child might stall the UI.

    I wonder if there could be a declarative opt-in for this, akin to will-change.

  16. thePunderWoman commented on Jun 30, 2025

    @thePunderWoman

    I'm very curious about what you had in mind there. I would also be interested in understanding how view transitions solve this same problem. I imagine the transition wrapper code changes something about how removal happens - and that allows for more style behavior before removal?

    An exit transition is about the same as a view transition like this:

    document.startViewTransition(() => parent.removeChild(child))
    

    Where the style sheet is configured for child to have a view-transition-name and also specify the desired fade animation for child.

    So we could potentially define a new syntax for an "exit animation" that is syntactic sugar for the above. A browser implementation might be quite tricky or expensive though, since gathering a screenshot on every update to child is expensive, and a synchronous delay to gather a screenshot before removing the child might stall the UI.

    This is essentially what I assumed this implementation would be. Once scoped view transitions land, people are going to do this pattern anyway, and having this syntactic sugar would be nice. I definitely can see the challenges with the screenshot performance, though. Either way, though, this problem exists, either with the syntactic sugar or not.

  17. bramus commented on Jun 30, 2025

    @bramus
    Contributor

    About the View Transitions route for this: the tricky part here is that you would not only need to define the exit-styles but also need to:

    • Define on which part of the tree you want to run the View Transition: the direct parent or some other ancestor?
    • Determine which elements get a view-transition-name as part of that mutation.
    • Need to figure out whether the removed element also gets a (special) view-transition-name or not, and what to do with the exit-styles in case of conflict.

    Also not covered are batched updates – such as multiple elements getting removed at once or elements getting removed in once place and added in a new place (for which one should use moveBefore).

    Perhaps some automatically generated types on the VT object could aid with some of that, but I haven’t explored that.

    Side Note: In https://codepen.io/bramus/pen/zxOOzYX I have an exploration that automatically wraps a removal of a node in a View Transition. It’s using a MutationObserver to undo and then redo the mutation. See https://brm.us/auto-transitions for a full write-up.

  18. benedikt commented on Sep 10, 2025

    @benedikt

    Also not covered are batched updates – such as multiple elements getting removed at once

    I'd like to emphasize this. Now that there can only be one active view transition at a time the view transitions approach is even less feasible. It's not uncommon for multiple elements to be removed in quick succession and skipping all transitions only for the last one is not great. Waiting for the previous one to finish might for some use cases, but not for all.

  19. bramus commented on Sep 10, 2025

    @bramus
    Contributor

    I'd like to emphasize this. Now that there can only be one active view transition at a time the view transitions approach is even less feasible.

    #11292 was concerned with how to batch updateCallbacks. Even before that issue was discussed, you’d get only one active view transition. The new one would skip the old one.

    Note that the upcoming Scoped View Transitions – now available for testing in Chrome – will allow multiple view transitions to run at the same time, as long as they have different scopes and don’t want to snapshot the same elements.

  20. ShacharHarshuv commented on Jan 8, 2026

    @ShacharHarshuv

    Strong +1. This is a major missing primitive in CSS today.

    There is currently no declarative way to say: “when this element is about to be removed or hidden, transition to this state and wait for completion.” As a result, every framework re-implements the same lifecycle orchestration in JavaScript (Angular :leave / animate.exit, React + Framer Motion presence APIs, Vue <transition> wrappers, etc.).

    These solutions:

    • are complex and framework-specific
    • negatively impact DX
    • duplicate logic across ecosystems
    • prevent a shared, native mental model

    A symmetric counterpart to @starting-style (e.g. @exit-style / @ending-style) would complete the transition model and allow pure-CSS exit animations for common cases like DOM removal, display: none, popover/dialog closing, and conditional rendering.

    View Transitions are powerful, but they solve a different problem (state snapshots and page-level transitions). For component-level exit animations, a lifecycle-aware CSS primitive would be significantly simpler and more ergonomic.

    This feels like the missing half of @starting-style.

  21. oy3o commented on Feb 3, 2026

    @oy3o

    I find it hard to understand why a basic view-related requirement would need to be implemented using JavaScript. I believe this is the responsibility of CSS itself. For example, in the implementation of a collapsible drawer menu, we always want the menu to appear smoothly from the side. We could naturally use transition to control this process. However, if we don't set display: none, we will still retain this element in the document flow, and even if the element is not in the view, then Tab can still jump to the menu. This is why @starting-style exists, and its counterpart is @ending-style.

    Another practical example is when editing tags. We want each tag to shrink and disappear naturally when deleted. Without startViewTransition, we need to add removing to classList for each element and setTimout. Ignoring the code's aesthetics, this can also lead to bugs because element removal is delayed. Even with startViewTransition, we still have problems. Suppose we delete multiple tags simultaneously; we know they will all apply the same animation, but now we have to specify different view-transition-names. I believe @ending-style can solve these problems.

  22. bramus commented on Feb 18, 2026

    @bramus
    Contributor

    (#) I believe @ending-style can solve these problems.

    One thing it doesn’t cover is what needs to happen after the element got removed from the DOM. Say you have a list of 4 cards and you remove the third one. Once/While the third one is/gets removed, the other items should – most likely – move towards each other, as seen in https://view-transitions.chrome.dev/cards/spa-auto/

    Image

    With View Transitions you can orchestrate all this. With a simple @ending-style on the removed element you can’t.

    (Also see this comment of mine that details this)

  23. Psychpsyo commented on Mar 14, 2026

    @Psychpsyo
    Contributor

    I find it hard to understand why a basic view-related requirement would need to be implemented using JavaScript. I believe this is the responsibility of CSS itself. For example, in the implementation of a collapsible drawer menu, we always want the menu to appear smoothly from the side. We could naturally use transition to control this process. However, if we don't set display: none, we will still retain this element in the document flow, and even if the element is not in the view, then Tab can still jump to the menu. This is why @starting-style exists, and its counterpart is @ending-style.

    It's been said in this thread before, but for cases where you just want to set display: none instead of removing the node entirely, this is already solved by transition-behavior: allow-discrete, which lets you transition to and from none. (The element stays in the non-none state for the whole transition instead of flipping at the midpoint.)

  24. tabatkins commented on Mar 14, 2026

    @tabatkins
    Member

    Correct. 'transition-behavior' probably didn't exist at the time this issue was posted (thus the demo in the OP showing you can't just transition it), but it exists and work now (try it in the demo - add transition-behavior: allow-discrete; to the third element's styles).

    The reason for @starting-style to exist is because display:none stomps over most styles and prevents them from having any meaningful value to animate from. But that's not the case for transitioning away, so there shouldn't be any need for an @ending-style.

  25. bramus commented on Mar 16, 2026

    @bramus
    Contributor

    I don’t think transition-behavior: allow-discrete on display fixes anything here. It doesn’t change anything to the current workarounds, expect for the timing now being controlled by the transition-duration set on display.

    See https://codepen.io/bramus/pen/azmJgLQ/d0aa5bbb528fa6c52e19a2234abc8075 for a starter demo, with these notes:

    • When removing an element using the bin icons, the removal is immediate.
    • If you swap out the e.target.parentElement.remove(); by a e.target.parentElement.style.display = 'none'; then, yes, you would get a delayed visual removal thanks to the transition-behavior: allow-discrete but:
      • This is not what you intended: you want the element to be removed from the DOM, not just hidden.
      • There still is no way to add a slide-out transition of some sorts before the element gets removed.
      • You’d still need to listen for a transitionend to do the actual removal, which was one of the points of friction listed in the OP

    So I think we’re back to square one here.


    What transition-behavior: allow-discrete on display does allow you to do though, is to have this be more backed by CSS-y bits. In https://codepen.io/bramus/pen/zxKZVjM/8ff25483b96e69e1d5d8f8aacc63243c I have a demo that uses this, but as you can see it still requires the current JavaScript-approaches as listed in the OP:

    • CSS:

      .element {
        /* Exaggerated timings */
        transition: opacity 1s ease, translate 1s ease, display 1s ease allow-discrete;
      
        &.exit-style {
          opacity: 0;
          translate: 0 -200px;   
        }
      }
    • JS:

      document.querySelector('.cards').addEventListener('click', e => {
        if (e.target.classList.contains('delete-btn')) {
          const $parent = e.target.parentElement;
          removeWithExitTransition($parent);
        }
      });
      
      const removeWithExitTransition = ($element) => {
        $element.classList.add('exit-style');
        $element.style.display = 'none';
        $element.addEventListener('transitionend', (e) => {
          if (e.propertyName === 'display') {
            $element.remove();
          }
        });
      }

    What @exit-style is asking about, is to give us a CSS way to do this in just CSS, so that when calling Element.remove() (or setting the element to display: none), it would do those transitions first.

    • CSS:

      .element {
        /* Exaggerated timings */
        transition: opacity 1s ease, translate 1s ease, display 1s ease allow-discrete;
      
        @exit-style {
          opacity: 0;
          translate: 0 -200px;   
        }
      }
    • JS:

      document.querySelector('.cards').addEventListener('click', e => {
        if (e.target.classList.contains('delete-btn')) {
          const $parent = e.target.parentElement;
          $parent.remove();
      });

    So, in summary, I think the ask is two-fold here:

    • Introduce @exit-style to allow authors to put those styles in that rule
    • When a Element gets removed or hidden, respect those @exit-styles

    The transition-behavior: allow-discrete on display is a requirement here to make this work.

    Hereby reopening for further discussion.

  26. reopened this on Mar 16, 2026
  27. jpzwarte commented on Mar 16, 2026

    @jpzwarte

    @bramus This probably partially overlaps with the overlay discussion at #13200 ? As in: wait with removing an element from the top layer until @exit-style has finished?

  28. HGCollier commented on Aug 27, 2026

    @HGCollier

    Having @ending-style or @exit-style, etc. would make accessible native components (dialog) more attractive than their custom counterparts. Big +1 on this draft.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions