Popover API
The Popover API provides developers with a standard, consistent, flexible mechanism for displaying popover content on top of other page content. Popover content can be controlled either using HTML attributes, or via JavaScript.
Concepts and usage
A very common pattern on the web is to show content over the top of other content, drawing the user's attention to specific important information or actions that need to be taken. This content can take several different names — overlays, popups, popovers, dialogs, etc. We will refer to them as popovers through the documentation. Generally speaking, these can be:
- modal, meaning that while a popover is being shown, the rest of the page is rendered non-interactive until the popover is actioned in some way (for example an important choice is made).
- non-modal, meaning that the rest of the page can be interacted with while the popover is being shown.
Popovers created using the Popover API are always non-modal. If you want to create a modal popover, a <dialog> element is the right way to go. There is significant overlap between the two — you might for example want to create a popover that persists, but control it using HTML. You can turn a <dialog> element into a popover (<dialog popover> is perfectly valid) if you want to combine popover control with dialog semantics.
Typical use cases for the popover API include user-interactive elements like action menus, custom "toast" notifications, form element suggestions, content pickers, or teaching UI.
You can create popovers in multiple different ways:
-
Via a set of new HTML attributes. A simple popover with a toggle button can be created using the following code:
html<button popovertarget="mypopover">Toggle the popover</button> <div id="mypopover" popover>Popover content</div> -
Via a JavaScript API. For example,
HTMLElement.togglePopover()can be used to toggle a popover between shown and hidden.
There are also new events to react to a popover being toggled, and CSS features to aid in styling popovers. See Using the popover API for a detailed guide to using this API.
A related feature — interest invokers — can be used to show popovers on hover/focus, without requiring JavaScript. Check out Using interest invokers to learn more.
HTML attributes
interestforExperimental-
Defines an HTML
<a>,<button>, or<area>element, or an SVG<a>element, as an interest invoker. Takes as its value theidof a target element that will be affected in some way (normally shown or hidden) when interest is shown or lost on the invoker element. popover-
A global attribute that turns an element into a popover element; takes a popover state (
"auto","hint", or"manual") as its value. popovertarget-
Turns a
<button>or<input>element into a popover control button; takes the ID of the popover element to control as its value. popovertargetaction-
Specifies the action to be performed (
"hide","show", or"toggle") on the popover element being controlled by a control<button>or<input>.
CSS features
::backdrop-
The
::backdroppseudo-element is a full-screen element placed directly behind popover elements, allowing effects to be added to the page content behind the popover(s) if desired (for example blurring it out). interest-delay,interest-delay-start, andinterest-delay-endExperimental-
The
interest-delayshorthand property and its relatedinterest-delay-startandinterest-delay-endlonghands can be set to add a delay between the user showing/losing interest and the interest change being acted on by the browser. :interest-sourceand:interest-target-
Can be used to apply styles to the interest invoker and its associated target element, respectively, only when interest is being shown.
:popover-open-
The
:popover-openpseudo-class matches a popover element only when it is in the showing state — it can be used to style popover elements when they are showing.
Interfaces
InterestEventExperimental-
The event object for the
interestandloseinterestevents. This includes asourceproperty that contains a reference to the associated interest invoker element. ToggleEvent-
Represents an event that fires when a popover element is toggled between being shown and hidden. It is the event object for the
beforetoggleandtoggleevents, which fire on popovers when their state changes.
Extensions to other interfaces
>Instance properties
interestForElementExperimental-
Returns a reference to the element being targeted by an interest invoker. This will be the element whose
idis referenced in the equivalent HTML or SVG interest invoker element'sinterestforattribute. Available on theHTMLButtonElement,HTMLAnchorElement,HTMLAreaElement, andSVGAElementinterfaces. HTMLElement.popover-
Gets and sets an element's popover state via JavaScript (
"auto","hint", or"manual"), and can be used for feature detection. Reflects the value of thepopoverglobal HTML attribute. -
Gets and sets the popover element being controlled by the control button. The JavaScript equivalent of the
popovertargetHTML attribute. -
Gets and sets the action to be performed (
"hide","show", or"toggle") on the popover element being controlled by the control button. Reflects the value of thepopovertargetactionHTML attribute.
Instance methods
HTMLElement.hidePopover()-
Hides a popover element by removing it from the top layer and styling it with
display: none. HTMLElement.showPopover()-
Shows a popover element by adding it to the top layer.
HTMLElement.togglePopover()-
Toggles a popover element between the showing and hidden states.
Events
beforetoggleevent-
Fired just before a popover element's state changes between showing and hidden, or vice versa. Can be used to prevent a popover from opening, or to update other elements that need to be triggered by popover state.
toggleevent-
Fired just after a popover element's state changes between showing and hidden, or vice versa.
interestExperimental-
Fired on an interest invoker's target element when interest is shown, allowing code to be run in response.
loseinterestExperimental-
Fired on an interest invoker's target element when interest is lost, allowing code to be run in response.
Examples
See our Popover API examples landing page to access multiple popover examples.
Specifications
| Specification |
|---|
| HTML> # dom-popover> |
| HTML> # event-beforetoggle> |
| HTML> # event-toggle> |
Browser compatibility
>api.HTMLElement.popover
api.HTMLElement.beforetoggle_event.popover_elements
api.HTMLElement.toggle_event.popover_elements
See also
popoverHTML global attributepopovertargetHTML attributepopovertargetactionHTML attribute::backdropCSS pseudo-element:popover-openCSS pseudo-class