On this page9 sections
Lecture 047: Styling Popover & Dialog
| Category | 7: Advanced Styling |
| Lecture | 047 of 057 |
| Estimated video length | 10 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
HTML's
<dialog>andpopover(HTML Lectures 038 and 045) appear in the browser's top layer, above everything else. CSS styles the box, the dimmed::backdropbehind 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-styleandtransition-behavior: allow-discrete
1. Styling the Box and the Backdrop
Example
<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
| Selector | Matches |
|---|---|
dialog[open] | A dialog that's open |
:modal | A dialog opened with showModal() |
[popover]:popover-open | An 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-stylesets where the opening animation starts from.transition-behavior: allow-discreteletsdisplayandoverlaywait for the closing animation.
Example
<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
<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
- Which pseudo-element styles the dimmed area behind a modal dialog?
- Which selector matches an open popover?
- What does
@starting-styledo? - Why add
allow-discreteto thedisplaytransition? - How do you move a popover to the bottom-right corner?
Practice Answers
::backdrop:popover-open(e.g.[popover]:popover-open).- It sets the starting values for the entry animation when the element first appears.
- So the element stays visible until the closing animation finishes.
- Set
inset: auto 16px 16px auto;andmargin: 0;
Key Takeaways
- Dialogs and popovers sit in the top layer; style the box and its
::backdrop. dialog[open],:modaland:popover-opentarget open ones.@starting-style+allow-discreteanimate both opening and closing.