On this page11 sections
  1. Learning Objectives
  2. 1. A First Popover
  3. 2. Show, Hide or Toggle
  4. 3. auto vs manual
  5. 4. From JavaScript
  6. 5. Styling
  7. Popover or Dialog?
  8. Common Beginner Mistakes
  9. Practice Questions
  10. Practice Answers
  11. Key Takeaways

Lecture 045: Popover API

Category8: Web APIs
Lecture045 of 048
Estimated video length8 min
TargetHTML5 · any modern browser
LevelBeginner

Menus, tips and small pop-up panels used to need JavaScript to open, close, and close again when you click elsewhere. With the Popover API, two attributes do it all, with no JavaScript.


Learning Objectives

  • Make an element a popover with the popover attribute
  • Open and close it with popovertarget and popovertargetaction
  • Choose between auto and manual popovers
  • Control popovers from JavaScript

1. A First Popover

Example

html
<button type="button" popovertarget="tip">What's this?</button>

<div id="tip" popover>
  A popover sits on top of everything. Click outside it or press Esc to close it.
</div>
  • popover on the panel hides it until it's opened.
  • popovertarget on a button points at the panel's id.
  • Popovers appear above the rest of the page, so z-index doesn't matter.

2. Show, Hide or Toggle

Example

html
<button type="button" popovertarget="menu" popovertargetaction="show">Open menu</button>
<button type="button" popovertarget="menu" popovertargetaction="hide">Close menu</button>

<div id="menu" popover>
  <p><a href="#">Profile</a></p>
  <p><a href="#">Settings</a></p>
  <p><a href="#">Log out</a></p>
</div>

popovertargetaction can be toggle (the default), show or hide.


3. auto vs manual

popover / popover="auto"popover="manual"
Click outside closes itYesNo
Esc closes itYesNo
Opening one closes other auto popoversYesNo

Example: a notice that stays until dismissed:

html
<button type="button" popovertarget="notice">Show notice</button>

<div id="notice" popover="manual" style="padding: 14px;">
  <p>Your changes were saved.</p>
  <button type="button" popovertarget="notice" popovertargetaction="hide">Dismiss</button>
</div>

4. From JavaScript

html
<div id="hint" popover>Saved!</div>
<button type="button" onclick="document.getElementById('hint').showPopover(); setTimeout(() => document.getElementById('hint').hidePopover(), 1500)">
  Save
</button>

Methods: showPopover(), hidePopover(), togglePopover().


5. Styling

Example

html
<style>
  [popover] { border: none; border-radius: 12px; padding: 16px 20px; box-shadow: 0 10px 30px rgb(0 0 0 / 0.2); }
  [popover]::backdrop { background: rgb(0 0 0 / 0.15); }
</style>

<button type="button" popovertarget="styled">Styled popover</button>
<div id="styled" popover>Rounded corners, a soft shadow and a dim backdrop.</div>

CSS Lecture 047 covers popover and dialog styling in more detail.


Popover or Dialog?

  • Popover: light, non-blocking UI such as menus, tips and toasts.
  • <dialog> with showModal() (Lecture 038): something the visitor must deal with before continuing.

Common Beginner Mistakes

1. Mismatched ids. popovertarget must equal the popover's id.

2. popovertarget on a <div>. It only works on <button> and <input type="button">.

3. Manual popovers with no close button. Visitors get stuck with them.


Practice Questions

  1. Which attribute turns an element into a popover?
  2. How does a button open a popover?
  3. What does popovertargetaction="hide" do?
  4. What's the difference between auto and manual popovers?
  5. Which JavaScript method opens a popover?
  6. When should you use a dialog instead of a popover?

Practice Answers

  1. popover
  2. With popovertarget="the-popover-id".
  3. It makes the button only close the popover.
  4. Auto popovers close when you click outside or press Esc; manual ones only close when told to.
  5. showPopover()
  6. When the visitor must respond before continuing, such as confirming a deletion.

Key Takeaways

  • popover + a button with popovertarget = an open/close panel with no JavaScript.
  • auto popovers light-dismiss; manual ones stay until closed.
  • showPopover(), hidePopover() and togglePopover() work from JavaScript.
  • Style with [popover] and ::backdrop.