Menus, dropdowns, toasts and tooltips look simple, but building them well is fiddly. You need the content to sit above everything else, close when the user clicks outside or presses Escape, return focus sensibly, and not fight with z-index or overflow: hidden on some ancestor. Most teams either pull in a library or write a few hundred lines of JavaScript to cover it.

The Popover API moves that behaviour into HTML. Add a popover attribute to an element and point a button at it with popovertarget, and the browser handles showing it, layering it on top of the page, closing it on Escape or an outside click, and wiring up the basic accessibility. You can build a working menu with no JavaScript at all.

This article is for developers who build interactive UI and want to replace hand-rolled overlay code with the platform. We cover the three popover types, the JavaScript API, styling and animation, and three real components: a menu, a toast and a tooltip.

Your first popover

#

_10
<button popovertarget="account-menu">Account</button>
_10
_10
<div id="account-menu" popover>
_10
<a href="/profile">Profile</a>
_10
<a href="/settings">Settings</a>
_10
<a href="/logout">Sign out</a>
_10
</div>

That is all it takes. Clicking the button toggles the menu. While it is open:

  • It renders in the top layer, above all other content, regardless of z-index or any ancestor's overflow.
  • Pressing Escape closes it.
  • Clicking or tapping outside it closes it. This is called light dismiss.
  • Opening another popover of the same type closes this one.
  • The button gets the right accessibility state, so screen readers know whether it is expanded.

Elements with the popover attribute are hidden by default, with display: none, until they are shown.

The three popover types

#

The value of the popover attribute sets how the popover behaves.

popover="auto"

This is the default, used when you write popover with no value. Auto popovers light dismiss, close on Escape, and close any other auto popover that is open, unless the new one is nested inside it. That nesting rule is what makes sub-menus work: a popover opened from inside another popover keeps its parent open.

Use auto for menus, dropdowns, pickers and anything where only one thing should be open at a time.

popover="manual"

Manual popovers do not light dismiss and do not close other popovers. They only close when you tell them to, either with a button or with JavaScript. Several can be open at once.

Use manual for toasts, persistent panels and anything that should stay put while the user works elsewhere.

popover="hint"

Hint popovers are meant for tooltips and other transient hints. They light dismiss like auto popovers, but opening a hint does not close any auto popovers that are already open. That means you can show a tooltip over an item inside an open menu without the menu vanishing. Opening a hint does close other hints.

Support for hint is more limited than the rest of the API; see the browser support section below.

Controlling popovers with buttons

#

The popovertarget attribute goes on a button (or an input of type button) and takes the ID of the popover. By default it toggles. Add popovertargetaction to choose a specific action:


_10
<button popovertarget="help" popovertargetaction="show">Help</button>
_10
_10
<div id="help" popover="manual">
_10
<p>Here is some help text.</p>
_10
<button popovertarget="help" popovertargetaction="hide">Close</button>
_10
</div>

The three actions are toggle (the default), show and hide. A close button inside a manual popover is the most common use for hide.

The JavaScript API

#

You do not need JavaScript for the basics, but there is a small API when you want more control.


_10
const panel = document.getElementById('help');
_10
_10
panel.showPopover();
_10
panel.hidePopover();
_10
panel.togglePopover();
_10
_10
// Check whether it is open
_10
panel.matches(':popover-open');

Calling showPopover() on a popover that is already open throws an error, as does calling hidePopover() on one that is closed. Check with :popover-open first, or use togglePopover(), which accepts an optional boolean to force a state and returns whether the popover is now open.

Two events fire around every change:


_10
panel.addEventListener('beforetoggle', (event) => {
_10
// event.oldState and event.newState are 'open' or 'closed'
_10
if (event.newState === 'open') {
_10
loadPanelContent();
_10
}
_10
});
_10
_10
panel.addEventListener('toggle', (event) => {
_10
console.log(`Popover is now ${event.newState}`);
_10
});

beforetoggle fires before the change and, when opening, can be cancelled with event.preventDefault(). toggle fires after the change. These are handy for lazy loading content or sending analytics.

Styling popovers

#

The browser gives popovers some default styles: fixed positioning, centred in the viewport with margin: auto, a border and some padding. You will usually want to replace those.


_19
[popover] {
_19
margin: 0;
_19
padding: 0.5rem;
_19
border: 1px solid var(--border);
_19
border-radius: 0.5rem;
_19
background: var(--surface);
_19
box-shadow: 0 8px 24px rgb(0 0 0 / 0.15);
_19
}
_19
_19
/* Styles that only apply while it is open */
_19
[popover]:popover-open {
_19
display: grid;
_19
gap: 0.25rem;
_19
}
_19
_19
/* The area behind an open popover */
_19
[popover]::backdrop {
_19
background: rgb(0 0 0 / 0.1);
_19
}

The ::backdrop pseudo-element sits behind the popover in the top layer. It is transparent by default, which is usually right for menus.

Positioning next to the button

By default a popover is centred on the screen, which is fine for a toast or a panel but wrong for a menu. CSS anchor positioning solves this by tying the popover to its invoking button. When a popover is opened with popovertarget, the button becomes its implicit anchor, so you can position it without naming anything:


_10
#account-menu {
_10
position-area: bottom span-right;
_10
margin-block-start: 4px;
_10
position-try-fallbacks: flip-block;
_10
}

Anchor positioning is a topic of its own, and we have a separate article on it. If you need to support browsers without it, position the popover with a few lines of JavaScript in the toggle event, using getBoundingClientRect() on the button.

Animating open and close

Popovers switch between display: none and a visible display value, which used to make transitions impossible. Two newer CSS features fix that: @starting-style, which sets where the entry transition starts from, and transition-behavior: allow-discrete, which lets display take part in a transition.


_21
[popover] {
_21
opacity: 0;
_21
translate: 0 -0.5rem;
_21
transition:
_21
opacity 150ms ease,
_21
translate 150ms ease,
_21
display 150ms allow-discrete,
_21
overlay 150ms allow-discrete;
_21
}
_21
_21
[popover]:popover-open {
_21
opacity: 1;
_21
translate: 0 0;
_21
}
_21
_21
@starting-style {
_21
[popover]:popover-open {
_21
opacity: 0;
_21
translate: 0 -0.5rem;
_21
}
_21
}

The overlay transition keeps the popover in the top layer until the exit animation finishes. Browsers that do not support overlay ignore that line, and the exit animation may be cut short, which is an acceptable fallback.

Real example: a dropdown menu

#

_10
<nav>
_10
<button popovertarget="products-menu">Products</button>
_10
<ul id="products-menu" popover class="menu">
_10
<li><a href="/analytics">Analytics</a></li>
_10
<li><a href="/billing">Billing</a></li>
_10
<li><a href="/support">Support</a></li>
_10
</ul>
_10
</nav>


_17
.menu {
_17
list-style: none;
_17
margin: 0;
_17
padding: 0.25rem;
_17
position-area: bottom span-right;
_17
}
_17
_17
.menu a {
_17
display: block;
_17
padding: 0.5rem 0.75rem;
_17
border-radius: 0.25rem;
_17
}
_17
_17
.menu a:hover,
_17
.menu a:focus-visible {
_17
background: var(--hover);
_17
}

This gives you a menu that opens and closes on click, closes on Escape and on outside clicks, and closes when another auto popover opens. When a popover closes via Escape or its button, focus returns to the button.

What it does not give you is arrow-key navigation between items. For a simple list of links, Tab works fine. If you are building an application-style menu with role="menu", you need to add arrow-key handling yourself, because that role promises it.

Real example: toast notifications

#

Toasts should not disappear when the user clicks elsewhere, and several might stack up, so manual is the right type.


_10
<div id="toast-region" role="status" aria-live="polite"></div>


_16
function showToast(message, { duration = 4000 } = {}) {
_16
const toast = document.createElement('div');
_16
toast.popover = 'manual';
_16
toast.className = 'toast';
_16
toast.textContent = message;
_16
_16
document.getElementById('toast-region').append(toast);
_16
toast.showPopover();
_16
_16
setTimeout(() => {
_16
toast.hidePopover();
_16
toast.addEventListener('transitionend', () => toast.remove(), { once: true });
_16
}, duration);
_16
}
_16
_16
showToast('Settings saved');


_10
.toast {
_10
inset: auto 1rem 1rem auto;
_10
margin: 0;
_10
padding: 0.75rem 1rem;
_10
border: none;
_10
border-radius: 0.5rem;
_10
background: var(--toast-bg);
_10
color: var(--toast-text);
_10
}

The inset value moves the toast from the centre to the bottom-right corner. The container with role="status" exists in the page before any toast is added, which is important: screen readers only announce changes to live regions they already know about.

If toasts can stack, you will need to offset each one, for example by setting a custom property for its index and using it in the inset value. If your toast is not removed by the time the exit transition would end (for instance, when transitions are turned off), transitionend will not fire, so add a fallback timeout in production code.

Real example: a tooltip with hint

#

A tooltip should appear on hover and on keyboard focus, and should not close any menu that happens to be open.


_10
<button id="save-btn" aria-describedby="save-tip">Save</button>
_10
<div id="save-tip" popover="hint" class="tooltip">Saves a draft. Shortcut: Ctrl+S</div>


_14
const trigger = document.getElementById('save-btn');
_14
const tip = document.getElementById('save-tip');
_14
_14
const show = () => {
_14
if (!tip.matches(':popover-open')) tip.showPopover();
_14
};
_14
const hide = () => {
_14
if (tip.matches(':popover-open')) tip.hidePopover();
_14
};
_14
_14
trigger.addEventListener('mouseenter', show);
_14
trigger.addEventListener('focus', show);
_14
trigger.addEventListener('mouseleave', hide);
_14
trigger.addEventListener('blur', hide);

The aria-describedby link means screen readers read the tooltip text as a description of the button, whether or not the tooltip is visible. Keep tooltips to short supporting text; anything the user must read or interact with belongs in a proper popover or dialog.

In browsers without hint support, the attribute value is treated as invalid and falls back to manual, so the tooltip still shows and hides with the code above. The difference is only how it interacts with other popovers.

Popover or dialog?

#

The dialog element and the Popover API overlap, and it helps to be clear on the difference.

  • A modal dialog, opened with showModal(), makes the rest of the page inert. Users cannot click or tab to anything behind it. Use it for confirmations, forms and anything the user must deal with before carrying on.
  • A popover is non-modal. The rest of the page stays interactive and focus is not trapped. Use it for menus, toasts, tooltips and pickers.

You can put the popover attribute on a dialog element if you want light dismiss on a non-modal dialog, but for a true modal, use showModal().

Browser support

#

The core Popover API, with auto and manual popovers, popovertarget, the JavaScript methods and the toggle events, is supported in all major browsers. It became Baseline in 2024, with Chrome and Edge 114, Safari 17 and Firefox 125.

The pieces around it vary:

  • popover="hint" shipped in Chrome and Edge 133 and in Firefox 149. Safari does not support it at the time of writing, and treats it as manual.
  • @starting-style and transition-behavior are supported in all current major browsers. The overlay property is Chromium only, so exit animations may be cut short elsewhere.
  • Anchor positioning, for placing a popover next to its button, is newer still; check current support before relying on it.

You can feature detect the API itself with HTMLElement.prototype.hasOwnProperty('popover').

When not to use it

#
  • When the user must respond before carrying on. Use a modal dialog with showModal(). Popovers do not make the rest of the page inert.
  • For content that should always be visible. Popovers are for transient UI. A sidebar or panel that stays open is just a layout element.
  • When you need full menu keyboard behaviour without writing it. The API handles open, close and focus return, not arrow-key navigation or type-ahead. A well-tested component library may still save you time for complex menus.
  • For important information in a tooltip. Touch screens have no hover, so tooltip text is easy to miss. Put essential details in the page.

Wrapping up

#
  • Add popover to an element and popovertarget to a button to get top-layer rendering, light dismiss, Escape to close and basic accessibility with no JavaScript.
  • Use auto for menus and pickers, manual for toasts and persistent panels, and hint for tooltips where it is supported.
  • showPopover(), hidePopover(), togglePopover() and the beforetoggle and toggle events cover the cases where you need script.
  • Animate with @starting-style and transition-behavior: allow-discrete, and position next to the button with anchor positioning.
  • Popovers are non-modal; reach for dialog with showModal() when the rest of the page should be blocked.

Further reading: MDN: Popover API

file under:
HTML

Was this article helpful?

About the author

Danny Engineering

A software engineer with a strong belief in human-centric design and driven by a deep empathy for users. Combining the latest technology with human values to build a better, more connected world.