The Popover API is a modern web API that allows developers to create lightweight popups such as menus, tooltips, help panels, and dropdowns using native browser features without external libraries.
π What is the Popover API?
The Popover API is built around the HTML popover attribute and JavaScript methods like showPopover(),hidePopover(), and togglePopover().
π‘ Why Use the Popover API?
- π Create dropdown menus
- π‘ Display tooltips and help panels
- βοΈ Build settings or action menus
- π Show contextual information
- π Replace many custom popup implementations
π Creating a Popover
Add the popover attribute to an HTML element.
Basic Popover
<div
id="info"
popover>
Hello from Popover!
</div>π Selecting the Popover
Get Element
const popover =
document.getElementById("info");π Showing the Popover
Show Popover
popover.showPopover();β Hiding the Popover
Hide Popover
popover.hidePopover();π Toggling the Popover
Toggle Popover
popover.togglePopover();π Popover Methods
| Method | Description |
|---|---|
| showPopover() | Displays the popover. |
| hidePopover() | Hides the popover. |
| togglePopover() | Toggles the popover's visibility. |
π’ Popover Events
| Event | Description |
|---|---|
| beforetoggle | Fires before the popover opens or closes. |
| toggle | Fires after the popover's open state changes. |
π― Listening for Events
Toggle Event
popover.addEventListener(
"toggle",
() => {
console.log(
"Popover state changed"
);
}
);π Complete Example
HTML
<button id="btn">
Show Info
</button>
<div
id="popover"
popover>
Welcome to the Popover API!
</div>JavaScript
const popover =
document.getElementById("popover");
document
.getElementById("btn")
.onclick = () => {
popover.togglePopover();
};β‘ Automatic Popover Control
You can connect a button to a popover directly in HTML usingpopovertarget, without writing JavaScript.
Automatic Toggle
<button
popovertarget="menu">
Open Menu
</button>
<div
id="menu"
popover>
Menu Content
</div>π¨ Styling a Popover
CSS
[popover] {
padding: 20px;
border: none;
border-radius: 8px;
background: white;
box-shadow:
0 5px 20px rgba(0,0,0,.3);
}
[popover]::backdrop {
background:
rgba(0,0,0,.2);
}π Popover States
| State | Description |
|---|---|
| Hidden | Popover is not visible. |
| Visible | Popover is displayed. |
π§ͺ Checking Visibility
Use the :popover-open CSS pseudo-class to style an open popover.
Open Popover
:popover-open {
animation:
fadeIn .2s ease;
}π Popover API vs Dialog API
| Feature | Popover API | Dialog API |
|---|---|---|
| Purpose | Lightweight popups, menus, tooltips | Modal and non-modal dialogs |
| Blocks Page Interaction | β Usually No | β With showModal() |
| Built-In Toggle Support | β Yes | β No |
| Best For | Dropdowns and contextual UI | Forms, confirmations, alerts |
β οΈ Limitations
- π The Popover API is supported in modern browsers but may not be available in older versions.
- π¨ Advanced positioning and animations often require additional CSS or JavaScript.
- π± Test behavior across browsers, especially on mobile devices.
Note
β Best Practices
- π Use popovers for menus, tooltips, and contextual information.
- π¨ Style open popovers with the :popover-open pseudo-class.
- β‘ Prefer popovertarget for simple interactions to reduce JavaScript.
- β¨οΈ Ensure keyboard accessibility for interactive popover content.
- π± Test the user experience on different screen sizes and browsers.
π― Summary
The Popover API provides a native, lightweight solution for creating popups, dropdowns, menus, and contextual panels. With methods likeshowPopover(), hidePopover(), andtogglePopover(), plus declarative HTML support through thepopover and popovertarget attributes, it simplifies the development of modern interactive interfaces.