The Popover API

By

Updated

Tagged: Web Platform

Build native browser popovers with the Popover API: HTML attributes, auto, manual and hint modes, positioning next to the button, open and close animations, events, and common pitfalls.

The Popover API gives you native popovers right in the browser, so you don’t have to manage the top layer or light dismiss yourself.

Add the popover attribute to an element and it turns into a popover. From there, the browser handles layering, keyboard navigation order, the relationship with the invoker, and light dismiss for auto popovers.

A popover isn’t modal, though. It doesn’t make the rest of the page inert or trap focus the way <dialog>.showModal() does.

HTML only, no JavaScript

You can get a working popover with just HTML by pairing a button with popovertarget and an element with popover:

<button popovertarget="help-tip">Show help</button>

<div id="help-tip" popover>
  <p>Click save to store your changes.</p>
</div>

Click the button and the popover opens. Click anywhere outside it, or press Esc, and it closes. That behavior is called light dismiss.

You can point popovertarget at any element that has a popover attribute, without wiring up any event listeners.

The button side is more limited. Only a <button> or an <input type="button"> can have popovertarget. Put it on a link or a <div> and nothing happens.

When you open a popover this way, the browser also links the two elements for screen readers, and the popover comes right after the button in the Tab order. If you close the popover with the keyboard, focus goes back to the button.

Show, hide or toggle

By default the button toggles the popover. Use popovertargetaction to make a button that only shows it or only hides it. It accepts show, hide and toggle (the default).

This is handy for a close button inside the popover:

<button popovertarget="settings">Settings</button>

<div id="settings" popover>
  <p>Your settings go here.</p>
  <button popovertarget="settings" popovertargetaction="hide">
    Close
  </button>
</div>

Newer browsers also support the command and commandfor attributes. They do the same job, with a more general design that isn’t limited to popovers:

<button commandfor="settings" command="toggle-popover">Settings</button>

The values are show-popover, hide-popover and toggle-popover. They’re supported since Chrome 135, Firefox 144 and Safari 26.2. If you need older browsers, stick with popovertarget.

Auto, manual and hint popovers

An auto popover uses popover="auto", or just a bare popover attribute. Light dismiss works out of the box, and opening a new auto popover closes any other auto popover that’s currently open. Nested auto popovers are the one exception, so menus can open submenus without closing themselves.

A manual popover uses popover="manual". Light dismiss doesn’t apply here, so it stays open until you close it with another toggle or from JavaScript. Opening an auto popover doesn’t close it either. Here’s an example:

<button popovertarget="status-panel">Toggle status</button>
<div id="status-panel" popover="manual">
  <p>Deploy in progress...</p>
</div>

Use manual mode for panels that shouldn’t disappear when someone clicks elsewhere on the page, like a status widget or a persistent notification. You can have several manual popovers open at the same time.

A hint popover uses popover="hint". It’s meant for tooltips. It can be light dismissed, but opening it doesn’t close an auto popover that’s already open. So you can show a tooltip on a button inside an open menu without closing the menu.

hint is the newest value. The current version is in Chrome 151, Firefox 153 and Safari 27.3. Browsers that don’t support it treat it as manual, so make sure your tooltip still has a way to close.

Styling with ::backdrop and :popover-open

Popovers render in the top layer, above the rest of your page. You can style the open state with :popover-open:

[popover] {
  border: 1px solid #ccc;
  padding: 1rem;
  border-radius: 8px;
}

[popover]:popover-open {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

[popover]::backdrop {
  background: rgba(0, 0, 0, 0.4);
}

The ::backdrop pseudo-element covers the page behind the popover, so you can dim it, blur it, or leave it transparent.

By default the browser shows popovers in the middle of the viewport. Its built-in stylesheet gives them position: fixed, inset: 0 and margin: auto, plus a border and a little padding.

Don’t set display on [popover]

A hidden popover is hidden with display: none. If your CSS sets display on every popover, you override that, and the popover shows up on the page before anyone clicks:

/* Wrong: the popover is always visible */
[popover] {
  display: flex;
}

Set display only on the open state instead:

[popover]:popover-open {
  display: flex;
}

Position the popover next to its button

A centered popover is fine for a dialog-style panel. For a menu or a tooltip, you want it next to the button. CSS anchor positioning does that:

<button class="menu-button" popovertarget="menu">Menu</button>

<div id="menu" popover>
  <a href="/profile/">Profile</a>
  <a href="/logout/">Log out</a>
</div>
.menu-button {
  anchor-name: --menu-button;
}

#menu {
  position-anchor: --menu-button;
  position-area: bottom span-right;
  margin: 0;
}

position-area: bottom span-right places the popover below the button, starting at its left edge. Reset margin to 0, because the default margin: auto is what centers the popover in the first place.

The browser also creates an implicit anchor between a popover and the button that opened it, so in theory you can skip anchor-name. Browsers have changed how that implicit link works by default in recent versions, so I name the anchor explicitly. It’s one extra line and it behaves the same everywhere.

anchor-name and position-area work in Chrome 129, Firefox 147 and Safari 26 or later. In older browsers the popover stays centered, which is still usable.

Animate opening and closing

Popovers switch between display: none and visible, so a normal transition doesn’t run. You need three things:

[popover] {
  opacity: 0;
  transform: translateY(-8px);
  transition:
    opacity 0.2s,
    transform 0.2s,
    overlay 0.2s allow-discrete,
    display 0.2s allow-discrete;
}

[popover]:popover-open {
  opacity: 1;
  transform: translateY(0);
}

@starting-style {
  [popover]:popover-open {
    opacity: 0;
    transform: translateY(-8px);
  }
}

Support varies here. The overlay property only works in Chromium browsers, and Firefox doesn’t transition display with allow-discrete. In those browsers the popover still opens and closes, you just lose part of the animation.

JavaScript API

When you need to control a popover from JavaScript, you’ve got three methods to open and close it:

const tip = document.querySelector('#help-tip')

tip.showPopover()
tip.hidePopover()
tip.togglePopover()

togglePopover() returns true if the popover is now open and false if it’s now closed. You can also pass true or false to force a state, like classList.toggle():

tip.togglePopover(true) // always opens

These methods only work on elements that have a popover attribute. On any other element they throw a NotSupportedError.

To know if a popover is open, check the pseudo-class:

tip.matches(':popover-open')

If you open a popover from code instead of from a button, pass the element that triggered it as source. The browser then uses it for the Tab order and as the anchor:

tip.showPopover({ source: someElement })

The source option works in Chrome 137, Firefox 144 and Safari 26 or later.

The toggle and beforetoggle events

You can listen for state changes using events:

tip.addEventListener('toggle', (event) => {
  console.log(event.newState) // 'open' or 'closed'
})

toggle fires after the change. beforetoggle fires just before it, and you can cancel the opening with preventDefault():

tip.addEventListener('beforetoggle', (event) => {
  if (event.newState === 'open' && !userIsLoggedIn) {
    event.preventDefault()
  }
})

You can only cancel opening. Calling preventDefault() when the popover is closing does nothing.

Both events have oldState and newState, set to 'open' or 'closed'.

Use the JavaScript API when your trigger isn’t a button, or when you want to open a popover from your app logic.

When to use it

For tooltips, menus, and small contextual panels, the Popover API is much lighter than building a modal from scratch.

If you need a full-screen dialog with complex focus rules, <dialog> is still the better fit.

The basic Popover API works in Chrome 114, Firefox 125 and Safari 17 or later, so you don’t need a polyfill. The newer parts I covered above, like hint, command and anchor positioning, need more recent versions.

Want me to talk about your product? You can sponsor this site.

Related posts about platform: