On this page11 sections
Lecture 045: Popover API
| Category | 8: Web APIs |
| Lecture | 045 of 048 |
| Estimated video length | 8 min |
| Target | HTML5 · any modern browser |
| Level | Beginner |
Menus, tips and small pop-up panels used to need JavaScript to open, close, and close again when you click elsewhere. With the Popover API, two attributes do it all, with no JavaScript.
Learning Objectives
- Make an element a popover with the
popoverattribute - Open and close it with
popovertargetandpopovertargetaction - Choose between
autoandmanualpopovers - Control popovers from JavaScript
1. A First Popover
Example
<button type="button" popovertarget="tip">What's this?</button>
<div id="tip" popover>
A popover sits on top of everything. Click outside it or press Esc to close it.
</div>popoveron the panel hides it until it's opened.popovertargeton a button points at the panel'sid.- Popovers appear above the rest of the page, so
z-indexdoesn't matter.
2. Show, Hide or Toggle
Example
<button type="button" popovertarget="menu" popovertargetaction="show">Open menu</button>
<button type="button" popovertarget="menu" popovertargetaction="hide">Close menu</button>
<div id="menu" popover>
<p><a href="#">Profile</a></p>
<p><a href="#">Settings</a></p>
<p><a href="#">Log out</a></p>
</div>popovertargetaction can be toggle (the default), show or hide.
3. auto vs manual
popover / popover="auto" | popover="manual" | |
|---|---|---|
| Click outside closes it | Yes | No |
| Esc closes it | Yes | No |
| Opening one closes other auto popovers | Yes | No |
Example: a notice that stays until dismissed:
<button type="button" popovertarget="notice">Show notice</button>
<div id="notice" popover="manual" style="padding: 14px;">
<p>Your changes were saved.</p>
<button type="button" popovertarget="notice" popovertargetaction="hide">Dismiss</button>
</div>4. From JavaScript
<div id="hint" popover>Saved!</div>
<button type="button" onclick="document.getElementById('hint').showPopover(); setTimeout(() => document.getElementById('hint').hidePopover(), 1500)">
Save
</button>Methods: showPopover(), hidePopover(), togglePopover().
5. Styling
Example
<style>
[popover] { border: none; border-radius: 12px; padding: 16px 20px; box-shadow: 0 10px 30px rgb(0 0 0 / 0.2); }
[popover]::backdrop { background: rgb(0 0 0 / 0.15); }
</style>
<button type="button" popovertarget="styled">Styled popover</button>
<div id="styled" popover>Rounded corners, a soft shadow and a dim backdrop.</div>CSS Lecture 047 covers popover and dialog styling in more detail.
Popover or Dialog?
- Popover: light, non-blocking UI such as menus, tips and toasts.
<dialog>withshowModal()(Lecture 038): something the visitor must deal with before continuing.
Common Beginner Mistakes
1. Mismatched ids. popovertarget must equal the popover's id.
2. popovertarget on a <div>. It only works on <button> and <input type="button">.
3. Manual popovers with no close button. Visitors get stuck with them.
Practice Questions
- Which attribute turns an element into a popover?
- How does a button open a popover?
- What does
popovertargetaction="hide"do? - What's the difference between
autoandmanualpopovers? - Which JavaScript method opens a popover?
- When should you use a dialog instead of a popover?
Practice Answers
popover- With
popovertarget="the-popover-id". - It makes the button only close the popover.
- Auto popovers close when you click outside or press Esc; manual ones only close when told to.
showPopover()- When the visitor must respond before continuing, such as confirming a deletion.
Key Takeaways
popover+ a button withpopovertarget= an open/close panel with no JavaScript.autopopovers light-dismiss;manualones stay until closed.showPopover(),hidePopover()andtogglePopover()work from JavaScript.- Style with
[popover]and::backdrop.