Overview
Top-layer entry and exit transitions are about lifecycle, not decoration. A native <dialog> or popover can appear above every normal stacking context, but closing it changes discrete state: display and overlay do not interpolate like opacity. This pattern lets the browser keep the surface in the top layer long enough for the visual exit to finish, while the native primitive still owns focus, backdrop, and dismissal behavior.
When to use it
Use it for modal dialogs, popovers, command palettes, contextual menus, and teaching demos where the surface needs a real open and close motion but should not be rebuilt as a custom z-index stack. Skip it when the panel is purely decorative or when the browser support target cannot tolerate a snapped fallback. A clean snap is better than a fake JavaScript modal that loses native close and focus rules.
How it works
The open state transitions normal animatable properties such as opacity and transform. @starting-style supplies the first open frame so the browser has a value to animate from when the element first enters the top layer. On close, transition-behavior: allow-discrete plus display and overlay in the transition list tells the browser to delay those discrete flips until the visual exit has played. The backdrop follows the same timing so the page does not brighten before the surface finishes leaving.
Production gotchas
Browser support is the first production decision. If @starting-style, transition-behavior, or overlay retention is missing, the UI should still open and close natively; only the polish snaps. Keep that fallback behind @supports instead of writing a second JavaScript animation state. Also avoid animating layout around the surface: compare demos need fixed-height wrappers, and production dialogs should not push page content while they leave.
Accessibility
The native element owns interaction. Open dialogs with showModal(), close them with close() or method="dialog", and use popover APIs for non-modal surfaces. Store the trigger when you need explicit focus return, but do not trap focus in a fake closing shell after the native primitive has closed. Under prefers-reduced-motion: reduce, remove travel and fades while preserving the same close button, Escape behavior, backdrop state, and focus path.
References
Implementation depth
This pattern is about top-layer lifecycle, not modal layout. @starting-style gives dialog and popover a first open frame, while display and overlay allow-discrete keep the surface rendered until the exit transition completes.
Keep the native primitive in charge. JavaScript should call showModal, close, showPopover, or hidePopover; CSS should own the entry, exit, backdrop, reduced-motion fallback, and the no-layout-shift compare behavior.
The fallback is semantic continuity, not motion parity. If a browser cannot retain display or overlay through the transition, the surface may snap, but the modal or popover must still close through the native API and return focus through the same ownership path.