FRENVersion française

Overlays & display

Popover

Popover

Demo

Loading demo…

A small panel that opens when you click a button and stays attached to it. It shows actions, a picker or a short form without blocking the page, and closes when you click outside.

Story

Did you know

For years, the number one cause of dropdown bugs was a parent with overflow: hidden or a z-index set too low. The Popover API's top layer fixes that at the browser level.

Timeline

  1. 2010sBootstrap and jQuery UI popularize JS dropdowns and popovers.
  2. 2015Popper.js (now Floating UI) standardizes positioning.
  3. 2024The Popover API becomes available in every browser.

Vocabulary

Names

FR
Popover, bulle contextuelle, menu déroulant, menu d'actions
EN
Popover, dropdown, flyout, popup menu, menu button

Common uses

  • "…" action menus on a row
  • Pickers (date, emoji, color)
  • Profile preview on hover
  • Small forms attached to a button

Often paired with

The components it usually sits with, by page type, in reading order.

Code

<button class="trigger" popovertarget="actions">Actions</button>

<div id="actions" popover>
  <button>Rename</button>
  <button>Duplicate</button>
  <button>Delete</button>
</div>

<style>
  .trigger { anchor-name: --actions; }
  #actions {
    position-anchor: --actions;
    inset: auto; margin: 4px 0 0;
    top: anchor(bottom); left: anchor(left);
    position-try-fallbacks: flip-block;
  }
</style>

<script>
  actions.addEventListener('click', e => {
    const item = e.target.closest('button')
    if (!item) return
    console.log(item.textContent)
    actions.hidePopover()
  })
</script>

Good to know

  • The popover attribute gives you the top layer (no more z-index wars or overflow: hidden parents), closing on outside click and Escape, with no JS. popovertarget only works on a <button> or an <input>.
  • By default the browser centers the popover on screen. Reset inset and margin before placing it with CSS Anchor Positioning. Otherwise, Floating UI is still the go-to library.
  • position-try-fallbacks: flip-block flips it above the button when there is no room below. In a list, give each row its own anchor-name.
  • popover="auto" closes other open popovers, popover="manual" leaves everything to you. A popover doesn't block: if people must answer before going on, you need a modal.

Accessibility

Must have

  • The trigger is a real <button> with a clear name. A lone "…" icon needs an aria-label.
  • With popovertarget, the browser exposes the button's open state (implicit aria-expanded). In a home-made version, set aria-expanded and aria-controls yourself.
  • With the keyboard, Tab moves from the button into the popover content. Escape closes it and returns focus to the button.

Often forgotten

  • role="menu" commits you to arrow keys, Home, End and role="menuitem" (the APG Menu Button pattern). Without that keyboard support, plain buttons with no role are better.
  • A popover that only opens on hover is out of reach for touch and keyboard users.

Nice to have

  • Add aria-haspopup="menu" to the button when it is a real menu.
  • Animate the opening with @starting-style and transition-behavior: allow-discrete, while respecting prefers-reduced-motion.

Who uses it