Repository navigation
Description
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
animationendortransitionendevent, 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
- View transitions could solve this problem (just like entrance animations), but this offers a higher level, portable primitive that can be used directly from CSS and is not subject to cross-origin restrictions.
- A pseudo-class (per the initial proposal in [selectors-4] Add syntax to establish before-change style for css-transitions on new elements. #8174), though this would be inconsistent with
@starting-styleand would likely suffer from the same issues that led to it becoming an @-rule
Activity
LeaVerou commentedon Oct 19, 2023
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.
I would be very surprised if it's too late to rename
@starting-stylegiven 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.
Loirooriol commentedon Oct 19, 2023
I don't see how this would work. The thing about
@starting-styleis 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.
chriscoyier commentedon Oct 20, 2023
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:
Then in CSS if you want control...
This example does that for list items "on the way out" of the DOM.
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-styleis a more elegant API than View Transitions, because:@starting-styleis sensible.display: none;andruud commentedon Oct 25, 2023
Easy, we add the
vitality: auto | noneproperty, make it transitionable, make it prefer theautoside 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)
LeaVerou commentedon Oct 26, 2023
@Loirooriol
Yes, it's more challenging than
@starting-stylebut it's such a common problem that I think it's worth spending some cycles on. Some ideas below.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.
Loirooriol commentedon Oct 26, 2023
I haven't really been following view transitions, but yeah, using them under the hood sounds more reasonable I guess.
dbaron commentedon Oct 27, 2023
transition-behavior: allow-discretewas 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-styleis 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 aredisplay: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 todisplay: nonethat problem doesn't exist and we don't need the special construct; we can use existing transition mechanisms. What@starting-styledoes is allow transitions to operate on elements that are changing fromnoneto other values ofdisplay; we don't need a special mechanism to allow transitions in the other direction, and they should just work.dbaron commentedon Oct 27, 2023
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.
dbaron commentedon Oct 27, 2023
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.)
chrishtr commentedon Oct 27, 2023
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:
(setting it to
display:noneis already doable withallow-discretehas 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.
dbaron commentedon Oct 28, 2023
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).
LeaVerou commentedon Oct 28, 2023
@chrishtr
First, it's not quite a one liner, it's more like:
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-ifor a React conditional.@dbaron
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?
argyleink commentedon Oct 30, 2023
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:
remove()append()removeandappend.style = '…'orclassList.toggle()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-stylebe 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-discretewhich has some super powers in that it can delay the transition of elements inoverlayor not, in addition to delayingdisplay:noneuntil 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.
13 remaining items
LeaVerou commentedon Jun 27, 2025
I wonder if there could be a declarative opt-in for this, akin to
will-change.thePunderWoman commentedon Jun 30, 2025
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.
bramus commentedon Jun 30, 2025
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:
view-transition-nameas part of that mutation.view-transition-nameor 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
MutationObserverto undo and then redo the mutation. See https://brm.us/auto-transitions for a full write-up.benedikt commentedon Sep 10, 2025
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.
bramus commentedon Sep 10, 2025
#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.
ShacharHarshuv commentedon Jan 8, 2026
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:
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/dialogclosing, 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.oy3o commentedon Feb 3, 2026
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
transitionto control this process. However, if we don't setdisplay: none, we will still retain this element in the document flow, and even if the element is not in the view, thenTabcan still jump to the menu. This is why@starting-styleexists, 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 addremovingto classList for each element andsetTimout. Ignoring the code's aesthetics, this can also lead to bugs because element removal is delayed. Even withstartViewTransition, 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 differentview-transition-names. I believe@ending-stylecan solve these problems.bramus commentedon Feb 18, 2026
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/
With View Transitions you can orchestrate all this. With a simple
@ending-styleon the removed element you can’t.(Also see this comment of mine that details this)
Psychpsyo commentedon Mar 14, 2026
It's been said in this thread before, but for cases where you just want to set
display: noneinstead of removing the node entirely, this is already solved bytransition-behavior: allow-discrete, which lets you transition to and fromnone. (The element stays in the non-nonestate for the whole transition instead of flipping at the midpoint.)tabatkins commentedon Mar 14, 2026
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-styleto exist is becausedisplay:nonestomps 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.bramus commentedon Mar 16, 2026
I don’t think
transition-behavior: allow-discreteondisplayfixes anything here. It doesn’t change anything to the current workarounds, expect for the timing now being controlled by thetransition-durationset ondisplay.See https://codepen.io/bramus/pen/azmJgLQ/d0aa5bbb528fa6c52e19a2234abc8075 for a starter demo, with these notes:
e.target.parentElement.remove();by ae.target.parentElement.style.display = 'none';then, yes, you would get a delayed visual removal thanks to thetransition-behavior: allow-discretebut:transitionendto do the actual removal, which was one of the points of friction listed in the OPSo I think we’re back to square one here.
What
transition-behavior: allow-discreteondisplaydoes 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:
JS:
What
@exit-styleis asking about, is to give us a CSS way to do this in just CSS, so that when callingElement.remove()(or setting the element todisplay: none), it would do those transitions first.CSS:
JS:
So, in summary, I think the ask is two-fold here:
@exit-styleto allow authors to put those styles in that rule@exit-stylesThe
transition-behavior: allow-discreteondisplayis a requirement here to make this work.Hereby reopening for further discussion.
jpzwarte commentedon Mar 16, 2026
@bramus This probably partially overlaps with the
overlaydiscussion at #13200 ? As in: wait with removing an element from the top layer until@exit-stylehas finished?HGCollier commentedon Aug 27, 2026
Having
@ending-styleor@exit-style, etc. would make accessible native components (dialog) more attractive than their custom counterparts. Big +1 on this draft.