webdevpuneet.com

Animated Dialog and Popover with @starting-style CSS

Animated Dialog and Popover with @starting-style CSS · Modals · Plain HTML, CSS & JS · Live preview

CategoryModals

About this UI Snippet

Animating display:none Without a Library: @starting-style in Practice

Screenshot of the Animated Dialog and Popover with @starting-style CSS snippet rendered live

Native <dialog> and popover elements are removed from rendering when closed, so for years a CSS transition could not play when they appeared or disappeared. Two newer features fix that. @starting-style defines how an element looks on its first frame, and transition-behavior: allow-discrete lets display and overlay take part in a transition. This snippet uses both on a confirm dialog and a share menu.

The enter animation

The resting style of .ds-dialog is transparent and slightly lowered and shrunk. .ds-dialog[open] is fully visible. Inside @starting-style the open state repeats the transparent values, so the browser starts from there when the element first renders and interpolates to the visible state.

The exit animation

Closing works because display and overlay are in the transition list with allow-discrete. The browser keeps the element rendered, and in the top layer, until the opacity and transform transitions finish, then flips it to display: none. No animationend listeners and no timeouts.

The backdrop

::backdrop is transitioned the same way: background colour and backdrop-filter blur go from nothing to a dim, blurred page, with their own starting-style rule. Clicking the backdrop is handled by comparing event.target to the dialog.

Native behaviour kept

showModal() gives focus trapping, Escape to close and an inert page. The popover gets light-dismiss and Escape from the popover attribute, and popovertarget wires the button with no script. Where anchor-name is supported the menu is placed under its button with anchor positioning.

Where it fits

Use it for confirmations, share menus, filters and anything that should feel smooth without adding a package. For a script-driven version see the modal and the plain popover snippets.

Accessibility and motion

The dialog is labelled with aria-labelledby, focus returns to the button that opened it, and the result line is an aria-live region so the outcome is announced. Under prefers-reduced-motion the durations drop to almost nothing, so the dialog still works but no longer moves.

What's included

Features

Enter animation
@starting-style fades and scales the dialog in from its first frame.
Exit animation
allow-discrete keeps the dialog rendered until the transition ends.
Blurred backdrop
::backdrop fades from clear to dimmed and blurred.
Native focus handling
showModal traps focus, closes on Escape and makes the page inert.
Light-dismiss popover
Click away or press Escape closes the menu, no script needed.
Anchor-positioned menu
The popover sits under its trigger using anchor positioning.
Form-driven results
method="dialog" returns the clicked button's value to returnValue.
Graceful fallback
Browsers without the features still open and close it, just without motion.

Step by step

How to Use

  1. 1
    Write the closed look as the base styleGive .ds-dialog opacity: 0 and a small translate and scale. The [open] rule then sets opacity: 1 and transform: none.
  2. 2
    Add @starting-style for the first frameRepeat the closed values inside @starting-style { .ds-dialog[open] { ... } }. Without it the browser has nothing to animate from when the element first appears.
  3. 3
    List display and overlay in the transitionAdd overlay and display with allow-discrete to the transition list so the dialog stays on screen until the fade finishes on close.
  4. 4
    Fade the backdrop tooTransition ::backdrop background and backdrop-filter, and give it its own @starting-style rule.
  5. 5
    Open with showModal(), wire the popover with an attributeThe dialog needs one call; the share menu needs only popovertarget="dsMenu" on the button and popover on the div.
  6. 6
    Place the menu under its buttonInside @supports (anchor-name: --a) set anchor-name on the button and top: calc(anchor(bottom) + 8px) on the popover.

Real-world uses

Common Use Cases

Delete and discard confirmations
Replace window.confirm with a real dialog that fades in and out, keeps focus inside and returns which button was pressed.
Share and action menus
Open a small menu from a button, positioned under it, that closes when the visitor clicks elsewhere or presses Escape.
Dropping animation libraries
Remove a Framer Motion or GSAP dependency that existed only to animate overlays, and keep the same polish in the CSS file.
Account and checkout prompts
Show a "leave without saving?" or "remove this item from your basket?" prompt that eases in over a blurred page and hands focus back to the button that opened it.

Got questions?

Frequently Asked Questions

@starting-style supplies the first-frame values so the enter transition has a starting point. allow-discrete on display and overlay keeps the element in the top layer while the exit transition runs. One handles entry, the other handles exit.

The dialog and popover still open and close correctly because they are native elements. The only difference is that they appear and disappear instantly instead of animating, so there is nothing to polyfill.

You can animate entry with keyframes, but exit is harder because display none removes the element immediately. Transitions with allow-discrete handle both directions with the same declarations, which is why this pattern replaces most keyframe workarounds.

Yes, mostly through the platform. showModal traps focus and restores it on close, Escape works, and the dialog is labelled with aria-labelledby. Keep the transitions short and respect prefers-reduced-motion, which this snippet does.

Copy & paste

Source Code

<main class="ds-page">
  <h1>Animate the top layer with CSS only</h1>
  <p class="ds-sub">A native dialog and a native popover, both fading and scaling in and out. No animation library, no class toggling.</p>
  <div class="ds-row">
    <button class="ds-btn" type="button" id="dsOpen">Delete project&hellip;</button>
    <button class="ds-btn ds-ghost" type="button" popovertarget="dsMenu">Share &#9662;</button>
  </div>

  <dialog class="ds-dialog" id="dsDialog" aria-labelledby="dsTitle">
    <h2 id="dsTitle">Delete &ldquo;Autumn campaign&rdquo;?</h2>
    <p>This removes 14 files and cannot be undone. Anyone with the link will lose access.</p>
    <form method="dialog" class="ds-actions">
      <button class="ds-btn ds-ghost" value="cancel">Cancel</button>
      <button class="ds-btn ds-danger" value="delete">Delete</button>
    </form>
  </dialog>

  <div class="ds-menu" id="dsMenu" popover>
    <button type="button">Copy link</button>
    <button type="button">Invite by email</button>
    <button type="button">Export as PDF</button>
  </div>
  <p class="ds-result" id="dsResult" aria-live="polite"></p>
</main>

Extend with AI

Take This Snippet Further With AI

Paste this snippet into an AI coding assistant and ask it to explain how @starting-style and transition-behavior allow-discrete make the open and close animations work. Useful follow-ups: add a slide-from-right drawer variant, animate a toast popover stack, or add a spring easing with linear(). Ask it to keep reduced-motion support and the native focus behaviour.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build an animated native dialog and popover using only CSS transitions, in plain HTML, CSS and a few lines of JavaScript.

Requirements:
- A button that calls dialog.showModal() on a <dialog> containing a heading (aria-labelledby), a sentence, and a form method="dialog" with Cancel and Delete buttons that return their value. Show the returned value in an aria-live line.
- The dialog's base style is opacity 0, translateY(18px) and scale(.96); the [open] style is opacity 1 with no transform. Repeat the closed values inside @starting-style for the [open] rule. Transition opacity, transform, overlay and display with transition-behavior allow-discrete (list display and overlay explicitly).
- Animate ::backdrop the same way, from transparent to a dark tint with backdrop-filter blur, including a @starting-style rule.
- A second button with popovertarget opens a div[popover] menu with three actions. Animate it with :popover-open plus @starting-style, anchor it under the trigger using anchor-name and position-anchor inside @supports, and call hidePopover after choosing an item.
- Clicking the dialog backdrop closes it. Respect prefers-reduced-motion and keep visible focus styles. Work at phone width.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.