CSS Popover API: Native Tooltips and Dropdowns in WordPress Themes

The Popover API: menus, tooltips and panels without JavaScript toggles

Every theme has elements that open on click and close on Escape or a click outside: a user menu, a mini cart, a language switcher, a help tooltip. Each needs the same JavaScript: toggle a class, set aria-expanded, listen for Escape and outside clicks, manage z-index. The popover attribute gives all of that to plain HTML: the browser opens and closes the element, puts it above everything else and handles light dismiss.

Problem: A theme’s header has three dropdowns with three slightly different scripts. One does not close on Escape, one is hidden behind a sticky block with a higher z-index, and screen readers do not announce whether a menu is open.

Solution: Mark each panel with popover and an ID, give the button popovertarget with that ID, style the open state with :popover-open, and use the toggle event only where script still has work to do (loading the mini cart content).

A user menu without script

<button type="button" popovertarget="ha-account-menu" class="ha-account-button">
    <?php esc_html_e( 'Account', 'textdomain' ); ?>
</button>

<nav id="ha-account-menu" popover class="ha-popover">
    <ul>
        <li><a href="<?php echo esc_url( wc_get_account_endpoint_url( 'orders' ) ); ?>"><?php esc_html_e( 'Orders', 'textdomain' ); ?></a></li>
        <li><a href="<?php echo esc_url( wp_logout_url( home_url( '/' ) ) ); ?>"><?php esc_html_e( 'Log out', 'textdomain' ); ?></a></li>
    </ul>
</nav>

What the browser does for popover (the value auto):

  • The button toggles the panel; Escape and a click outside close it (“light dismiss”).
  • Opening one auto popover closes other open auto popovers that are not its ancestors.
  • The open panel is rendered in the top layer, above all other content regardless of z-index and overflow: hidden on parents.
  • The button gets the right accessibility relation: screen readers announce it as expanded or collapsed.

Styling

.ha-popover {
    margin: 0;
    padding: .75rem;
    border: 1px solid #dcdcde;
    border-radius: .5rem;
    background: var(--wp--preset--color--base, #fff);
    box-shadow: 0 8px 24px rgb(0 0 0 / .12);
    inset: auto;                 /* reset the default centering */
    top: 4rem;
    right: 1rem;
}

.ha-popover::backdrop {
    background: transparent;
}

.ha-account-button:has(+ .ha-popover:popover-open) {
    background: var(--wp--preset--color--contrast, #1e1e1e);
    color: #fff;
}

Popovers are centered in the viewport by default (the user agent sets inset: 0 and margin: auto). For a panel next to its button, either set a fixed position as above, or use CSS anchor positioning where it is supported: CSS anchor positioning. Entry and exit animations work with @starting-style: entry animations.

manual popovers and script hooks

<div id="ha-cart-panel" popover="manual" class="ha-popover">…</div>
<button type="button" popovertarget="ha-cart-panel" popovertargetaction="toggle">Cart</button>
const panel = document.getElementById( 'ha-cart-panel' );
panel.addEventListener( 'toggle', ( event ) => {
    if ( event.newState === 'open' ) {
        haLoadMiniCart( panel ); // fetch the content only when needed
    }
} );
// from code: panel.showPopover(), panel.hidePopover(), panel.togglePopover()

popover="manual" has no light dismiss and does not close other popovers; it is for panels that must stay open, like a cart after adding a product. The toggle event fires after the state changed, beforetoggle before it.

What popover is not

A popover is non-modal: the rest of the page stays usable. For a dialog that blocks the page until answered (a confirmation, a login form), use <dialog> with showModal(), which also traps the focus. Navigation submenus that open on hover are not a fit either, because popovers open on activation.

NOTE: The Popover API works in all current browsers since 2024 (Chrome 114, Safari 17, Firefox 125). Older browsers do not know the attribute: the panel is shown as a normal part of the page and the button does nothing. For a theme that must still support them, hide the panels with [popover]:not(:popover-open) only inside @supports selector(:popover-open), or keep a tiny script that toggles a class when HTMLElement.prototype.togglePopover is missing.