---
title: "The Popover API: menus, toasts and tooltips without a library"
description: "Use the HTML popover attribute and popovertarget to build menus, toasts and tooltips with light dismiss, top-layer rendering and keyboard support built in."
url: https://www.danny.engineering/article/the-popover-api-menus-toasts-and-tooltips-without-a-library
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:23:00.000Z
tags: ["HTML"]
---

# The Popover API: menus, toasts and tooltips without a library

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


```html
<button popovertarget="account-menu">Account</button>

<div id="account-menu" popover>
  <a href="/profile">Profile</a>
  <a href="/settings">Settings</a>
  <a href="/logout">Sign out</a>
</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:


```html
<button popovertarget="help" popovertargetaction="show">Help</button>

<div id="help" popover="manual">
  <p>Here is some help text.</p>
  <button popovertarget="help" popovertargetaction="hide">Close</button>
</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.


```javascript
const panel = document.getElementById('help');

panel.showPopover();
panel.hidePopover();
panel.togglePopover();

// Check whether it is open
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:


```javascript
panel.addEventListener('beforetoggle', (event) => {
  // event.oldState and event.newState are 'open' or 'closed'
  if (event.newState === 'open') {
    loadPanelContent();
  }
});

panel.addEventListener('toggle', (event) => {
  console.log(`Popover is now ${event.newState}`);
});
```


`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.


```css
[popover] {
  margin: 0;
  padding: 0.5rem;
  border: 1px solid var(--border);
  border-radius: 0.5rem;
  background: var(--surface);
  box-shadow: 0 8px 24px rgb(0 0 0 / 0.15);
}

/* Styles that only apply while it is open */
[popover]:popover-open {
  display: grid;
  gap: 0.25rem;
}

/* The area behind an open popover */
[popover]::backdrop {
  background: rgb(0 0 0 / 0.1);
}
```


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:


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


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.


```css
[popover] {
  opacity: 0;
  translate: 0 -0.5rem;
  transition:
    opacity 150ms ease,
    translate 150ms ease,
    display 150ms allow-discrete,
    overlay 150ms allow-discrete;
}

[popover]:popover-open {
  opacity: 1;
  translate: 0 0;
}

@starting-style {
  [popover]:popover-open {
    opacity: 0;
    translate: 0 -0.5rem;
  }
}
```


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


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


```css
.menu {
  list-style: none;
  margin: 0;
  padding: 0.25rem;
  position-area: bottom span-right;
}

.menu a {
  display: block;
  padding: 0.5rem 0.75rem;
  border-radius: 0.25rem;
}

.menu a:hover,
.menu a:focus-visible {
  background: var(--hover);
}
```


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.


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


```javascript
function showToast(message, { duration = 4000 } = {}) {
  const toast = document.createElement('div');
  toast.popover = 'manual';
  toast.className = 'toast';
  toast.textContent = message;

  document.getElementById('toast-region').append(toast);
  toast.showPopover();

  setTimeout(() => {
    toast.hidePopover();
    toast.addEventListener('transitionend', () => toast.remove(), { once: true });
  }, duration);
}

showToast('Settings saved');
```


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


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.


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


```javascript
const trigger = document.getElementById('save-btn');
const tip = document.getElementById('save-tip');

const show = () => {
  if (!tip.matches(':popover-open')) tip.showPopover();
};
const hide = () => {
  if (tip.matches(':popover-open')) tip.hidePopover();
};

trigger.addEventListener('mouseenter', show);
trigger.addEventListener('focus', show);
trigger.addEventListener('mouseleave', hide);
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](https://developer.mozilla.org/en-US/docs/Web/API/Popover_API)
