<button class="pc-pop-btn" type="button" popovertarget="pc-share">Share</button>
<div class="pc-pop" id="pc-share" popover>
<a href="#">Copy link</a>
<a href="#">Send by email</a>
<a href="#">Post to Messenger</a>
</div>About this menu
A share menu that opens, closes and dismisses on outside click or Esc with the native popover attribute, anchored under its button, with no JavaScript.
How it works
The button points at the menu with the popovertarget attribute, and the menu has the popover attribute, so the browser handles open, close, outside-click dismissal and the Esc key. The anchor-name and position-anchor properties pin the menu under the button, and @starting-style lets it fade and slide in.
Ways to customise it
- Change the menu items and links in the HTML.
- Change margin: 8px 0 0 to move the menu closer or further away.
- Change the 0.2s transition for a faster or slower reveal.
Good to know
The popover attribute is supported in current browsers, and the code includes a fallback for browsers without anchor positioning, which place the menu in the top layer instead. Popovers take focus handling from the browser, which is better than a custom script.
Open an example, copy the HTML and the CSS, and paste them into your page. There is nothing to install and no JavaScript to load.