@starting-style, allow-discrete, and Display Transitions
For years, animating an element out of the DOM required JavaScript. CSS transitions only run when a property changes from one value to another, and display: none is a discrete switch: the element vanishes in a single frame with no in-between state for the browser to animate. The workaround was always the same: listen for transitionend, then apply display: none after the opacity transition finished. Two event listeners and a timing dependency just to fade something out.
Two modern CSS features eliminate that pattern entirely. @starting-style defines the property values an element transitions from when it first appears, enabling pure CSS entry animations. transition-behavior: allow-discrete tells the browser to hold a discrete property like display at its current value until the transition completes, enabling pure CSS exit animations. Together they form a complete enter and exit system with no JavaScript required.
Continuous vs. Discrete Properties
CSS properties fall into two categories for transition purposes. Continuous properties like opacity, transform, and color have interpolatable values: the browser can calculate meaningful states between 0 and 1, or between red and blue. Discrete properties like display, visibility, and content-visibility have no in-between states. display: none and display: block are two distinct modes with nothing in between.
When you transition a continuous property, the browser smoothly interpolates. When you transition a discrete property without allow-discrete, the browser snaps at the very start of the transition: display: none applies immediately on exit, cutting off any other transitions that were supposed to play alongside it.
@starting-style: Entry Animations
When an element is added to the DOM or becomes visible for the first time, CSS transitions do not fire because there is no previous value to transition from. The element simply renders at its current styles. @starting-style solves this by letting you define a set of property values that only apply on that first render, giving the transition a "from" state to work with.
.toast {
opacity: 1;
transform: translateY(0);
transition: opacity 300ms ease-out, transform 300ms ease-out;
}
@starting-style {
.toast {
opacity: 0;
transform: translateY(12px);
}
}
When .toast is inserted into the DOM, the browser sees the starting values from @starting-style and the current values from the rule above, and transitions between them. On every subsequent render, @starting-style is ignored.
You can also nest @starting-style inside the element's own rule using CSS nesting, which keeps the entry state visually close to the final state it transitions to:
.toast {
opacity: 1;
transform: translateY(0);
transition: opacity 300ms ease-out, transform 300ms ease-out;
@starting-style {
opacity: 0;
transform: translateY(12px);
}
}
Both approaches produce a fade-in. The key advantage of @starting-style is that it produces an interruptible transition: if the element is removed from the DOM before the entry animation finishes, the exit transition picks up from wherever the animation was. A @keyframes animation does not interrupt this cleanly. For UI that can be dismissed quickly, the transition-based approach feels more responsive.
transition-behavior: allow-discrete: Exit Animations
Without allow-discrete, adding display: none to an element's hidden state instantly removes it from the layout, cutting off any transition that was supposed to play first. transition-behavior: allow-discrete instructs the browser to hold the discrete property at its current value for the full duration of the transition before applying the new value.
You set it in the transition shorthand by appending allow-discrete as the behavior for the discrete property's transition:
.toast {
opacity: 1;
transform: translateY(0);
display: block;
transition:
opacity 300ms ease-out,
transform 300ms ease-out,
display 300ms allow-discrete;
}
.toast.is-hidden {
opacity: 0;
transform: translateY(-12px);
display: none;
}
When .is-hidden is added, the browser transitions opacity and transform over 300ms while holding display at block. Only after 300ms does display: none apply, so the exit animation is fully visible before the element disappears.
The Full Enter and Exit Pattern
Combining both features gives you a complete CSS-only enter and exit system. The state timeline for each direction works like this:
-
Entry: Element becomes visible → browser reads
@starting-stylevalues as the from-state → transitions to the element's normal styles. -
Exit: Hidden class or attribute applied →
opacityandtransformtransition to the hidden values →allow-discreteholdsdisplay: noneuntil the transition ends → element removed from layout.
.notification {
opacity: 1;
transform: translateY(0) scale(1);
display: flex;
transition:
opacity 250ms ease-out,
transform 250ms ease-out,
display 250ms allow-discrete;
}
.notification[hidden] {
opacity: 0;
transform: translateY(8px) scale(0.97);
display: none !important;
}
@starting-style {
.notification:not([hidden]) {
display: flex;
opacity: 0;
transform: translateY(8px) scale(0.97);
}
}
Using the HTML hidden attribute as the toggle is clean because the browser already sets display: none for it. The !important is needed to overcome the attribute's low specificity when the element has a custom display value set in CSS. Place @starting-style on the visible state (:not([hidden]) here) and include display: flex inside it so the browser has a renderable from-state when the element reappears.
The overlay Property and the Top Layer
Popovers and dialogs render in the browser's top layer, which sits above all other content. When a popover closes, the browser wants to remove it from the top layer immediately. Without overlay in the transition list, the element drops out of the top layer before the exit animation finishes, making the animation invisible or clipped.
Adding overlay with allow-discrete defers the top-layer removal until after the transition completes, just like it defers display: none:
[popover] {
opacity: 1;
transform: scale(1);
transition:
opacity 300ms ease,
transform 300ms ease,
display 300ms allow-discrete,
overlay 300ms allow-discrete;
@starting-style {
opacity: 0;
transform: scale(0.95);
}
}
[popover]:not(:popover-open) {
opacity: 0;
transform: scale(0.95);
}
You only need overlay in the transition list for elements that participate in the top layer: popovers, dialogs, and fullscreen elements. For regular in-flow UI like toasts, notification banners, or dropdown menus, display alone is sufficient.
Seeing It in Action
The demo below shows a notification that enters and exits with CSS transitions. Click Hide notification for the exit animation, then Show notification to see the enter animation run in reverse. Toggle Without the pattern to remove @starting-style and allow-discrete so both directions snap instantly.
Reduced Motion
Apply the standard prefers-reduced-motion guard by removing transitions inside a reduce block. The element still shows and hides correctly because display: none applies as normal without the transition. The allow-discrete behavior is tied to the transition duration, so when transition: none is set, the discrete property switches instantly as it always did.
@media (prefers-reduced-motion: reduce) {
.notification {
transition: none;
}
}
Build Your Demo
You have two options for where to build your portfolio demo:
-
Combine: Extend your motion prove assignment at
unit-6/motion/index.html. Add@starting-styleentry animations to the existing cards or buttons, and add a dismissible notification or toast element that uses the full enter and exit pattern. -
Standalone: Create a new directory at
unit-6/display-transitions/withindex.htmlanddisplay-transitions.css. Build a page with at least one element that demonstrates the complete enter and exit system.
Whichever path you choose, your demo must include:
-
At least one element with a
@starting-styleentry animation that is clearly visible when the element appears. -
At least one element with an exit animation using
transition-behavior: allow-discreteondisplay, so the exit transition plays fully before the element is removed from the layout. -
A
prefers-reduced-motionguard that removes transitions for users who prefer reduced motion, while ensuring elements still show and hide correctly.
If you went the standalone route, add a link to your demo on your portfolio homepage and in your README.md.
Teaching Video Checklist
When you record your teaching video, make sure your teammates come away understanding these concepts:
-
Why CSS transitions could not animate entry or exit for
display-toggled elements before these features, and what specifically each one solves:@starting-stylefor entries,allow-discretefor exits. - The state timeline for both directions: what property values are active at each phase of the enter animation and each phase of the exit animation.
-
Why
allow-discretetransitions are interruptible and why that matters for UI that can be dismissed before the animation completes. -
When
overlayneeds to be added to the transition list and why, covering the top-layer use case for popovers and dialogs. -
Your portfolio demo: show the enter and exit animations, then disable
@starting-styleandallow-discretetemporarily in DevTools so teammates see the before state clearly.
Start with the problem: show the old JavaScript workaround in a code snippet, then show how many lines of CSS replace it. The contrast makes the value of these features immediately clear.
Resources
- MDN: @starting-style — At-rule reference with entry animation examples and nesting syntax
-
MDN: transition-behavior — Property reference for
allow-discretewith discrete property list -
MDN: overlay — The top-layer property and why it needs
allow-discretefor popover and dialog transitions - Can I Use: @starting-style — Current browser support breakdown