On this page9 sections
  1. Learning Objectives
  2. 1. Styling the Box and the Backdrop
  3. 2. Selecting Open Ones
  4. 3. Animating Open and Close
  5. 4. A Toast Notification
  6. Common Beginner Mistakes
  7. Practice Questions
  8. Practice Answers
  9. Key Takeaways

Lecture 047: Styling Popover & Dialog

Category7: Advanced Styling
Lecture047 of 057
Estimated video length10 min
TargetCSS3 · any modern browser
LevelIntermediate

HTML's <dialog> and popover (HTML Lectures 038 and 045) appear in the browser's top layer, above everything else. CSS styles the box, the dimmed ::backdrop behind it, and with @starting-style, smooth open and close animations.


Learning Objectives

  • Style dialogs and popovers, and their ::backdrop
  • Target open ones with [open] and :popover-open
  • Animate opening and closing with @starting-style and transition-behavior: allow-discrete

1. Styling the Box and the Backdrop

Example

html
<style>
  dialog {
    border: none;
    border-radius: 14px;
    padding: 22px 24px;
    width: min(90vw, 340px);
    box-shadow: 0 20px 50px rgb(0 0 0 / 0.25);
    font-family: system-ui;
  }
  dialog::backdrop { background: rgb(15 23 42 / 0.55); backdrop-filter: blur(3px); }
  dialog h3 { margin-top: 0; }
  .actions { display: flex; justify-content: flex-end; gap: 8px; }
  .actions button { padding: 8px 14px; border-radius: 8px; border: 1px solid #ced4da; background: white; }
  .actions .primary { background: #1971c2; color: white; border-color: #1971c2; }
</style>

<button onclick="document.getElementById('d').showModal()">Open dialog</button>

<dialog id="d">
  <h3>Save changes?</h3>
  <p>Your edits will be lost if you leave now.</p>
  <form method="dialog" class="actions">
    <button>Cancel</button>
    <button class="primary">Save</button>
  </form>
</dialog>

::backdrop only appears for modal dialogs (showModal()) and popovers.


2. Selecting Open Ones

SelectorMatches
dialog[open]A dialog that's open
:modalA dialog opened with showModal()
[popover]:popover-openAn open popover

3. Animating Open and Close

Dialogs and popovers switch between display: none and display: block, which normally can't animate. Two new tools fix this:

  • @starting-style sets where the opening animation starts from.
  • transition-behavior: allow-discrete lets display and overlay wait for the closing animation.

Example

html
<style>
  [popover] {
    border: none; border-radius: 12px; padding: 16px 20px; box-shadow: 0 12px 30px rgb(0 0 0 / 0.2);
    opacity: 0;
    transform: translateY(12px) scale(0.96);
    transition: opacity 0.25s, transform 0.25s, display 0.25s allow-discrete, overlay 0.25s allow-discrete;
  }
  [popover]:popover-open { opacity: 1; transform: none; }
  @starting-style {
    [popover]:popover-open { opacity: 0; transform: translateY(12px) scale(0.96); }
  }
</style>

<button popovertarget="note">Show note</button>
<div id="note" popover>I fade and slide in, and out again.</div>

The same pattern works for dialog[open].


4. A Toast Notification

Example

html
<style>
  #toast { inset: auto 16px 16px auto; margin: 0; border: 0; border-radius: 10px; padding: 12px 16px;
           background: #2b8a3e; color: white; font: 14px system-ui; }
</style>

<button onclick="const t = document.getElementById('toast'); t.showPopover(); setTimeout(() => t.hidePopover(), 2000)">Save</button>
<div id="toast" popover="manual">✔ Saved successfully</div>

Popovers are centred by default; inset and margin: 0 move this one to the bottom-right corner.


Common Beginner Mistakes

1. Expecting ::backdrop on show(). Only modal dialogs (showModal()) and popovers get one.

2. Animating with only transition. The close animation is skipped unless display uses allow-discrete.

3. Forgetting the default centring. Dialogs and popovers come with margin: auto and inset: 0; reset them to position elsewhere.


Practice Questions

  1. Which pseudo-element styles the dimmed area behind a modal dialog?
  2. Which selector matches an open popover?
  3. What does @starting-style do?
  4. Why add allow-discrete to the display transition?
  5. How do you move a popover to the bottom-right corner?

Practice Answers

  1. ::backdrop
  2. :popover-open (e.g. [popover]:popover-open).
  3. It sets the starting values for the entry animation when the element first appears.
  4. So the element stays visible until the closing animation finishes.
  5. Set inset: auto 16px 16px auto; and margin: 0;

Key Takeaways

  • Dialogs and popovers sit in the top layer; style the box and its ::backdrop.
  • dialog[open], :modal and :popover-open target open ones.
  • @starting-style + allow-discrete animate both opening and closing.