Sub topic 1
What the `popover` Attribute Does
Prerequisites
You should know basic HTML elements and attributes, CSS selectors, and JavaScript element selection. You should also understand how buttons receive keyboard focus.
The HTML popover attribute turns an ordinary element into a non-modal overlay. A non-modal overlay appears above the page while still allowing the rest of the page to remain available. The browser manages opening, closing, and much of the keyboard behavior.
Add popover to the element that contains the temporary content. Give it an id, then connect a button with popovertarget.
<button popovertarget="help">Show help</button>
<div id="help" popover>
<p>Press Escape or click outside this box to close it.</p>
</div>The button is an invoker. Its popovertarget value must match the popover's id. The popover is closed initially. Clicking the button opens it.
Opening and Closing with `popovertarget` and `popovertargetaction
popovertargetaction controls what an invoker does:
toggleopens a closed popover and closes an open one.showopens it.hidecloses it.
If you omit the attribute, the button normally toggles the target.
<button popovertarget="menu" popovertargetaction="toggle">Menu</button>
<div id="menu" popover>
<a href="/profile">Profile</a>
<button popovertarget="menu" popovertargetaction="hide">
Close
</button>
</div>Why Popovers Appear Above Everything Else: The Top Layer
An open popover is placed in the browser's top layer. This is a special rendering layer above ordinary positioned elements and their stacking contexts. As a result, a popover generally appears above headers, cards, and elements with high z-index values.
Choosing Between `auto` and `manual` Popovers
An unvalued popover attribute means popover="auto". An auto popover supports light dismiss: clicking outside it or pressing Escape closes it. Opening another auto popover can also close the current one.
Use popover="manual" when the content must remain open until code or an explicit control closes it. Manual popovers do not close through outside clicks or Escape.
<div id="notice" popover="manual">
<p>Your changes were saved.</p>
<button popovertarget="notice" popovertargetaction="hide">
Dismiss
</button>
</div>Exercise
Which value should you choose when a popover must close only through an explicit button?
Show answer
Use popover="manual".