Anchor positioning and popovers
How the popover attribute gives built-in open, close and light-dismiss, and how CSS anchor positioning tethers it to a trigger and flips it near an edge.
@property and typed custom propertiesStacking Contexts and z-index
Anchor positioning and popovers
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- The `popover` attribute gives an element show and hide behaviour with no JavaScript: a button with `popovertarget` toggles it, Escape closes it, and clicking outside dismisses it (light dismiss).
- A shown popover lives in the top layer, above everything else on the page, so it is not clipped by `overflow: hidden` ancestors and `z-index` does not matter.
- Anchor positioning tethers one element to another in CSS: `anchor-name` on the trigger, `position-anchor` on the thing to place, and `position-area` (or the `anchor()` function) to say which side.
- `position-try-fallbacks` tells the browser what to do when the first placement would run off screen, such as `flip-block` to open upward instead of downward.
- These features are recent. Use them as enhancement, with a layout that still makes sense, such as a visible disclosure, when they are missing.
Two problems that ate a lot of JavaScript
Every site has dropdowns, tooltips, date pickers and menus. Two things make them painful.
Showing and hiding well. Toggle a class, trap focus, close on Escape, close on outside click, make sure it is above everything, and make sure a parent with overflow: hidden does not cut it off. Each piece is small, and together they are a library’s worth of code.
Putting it next to the thing that opened it. The menu has to sit under its button, line up with an edge, and move up when there is no room below. That used to mean measuring rectangles in JavaScript and recalculating on scroll and resize.
The platform now has an answer to each: the popover attribute for the first, anchor positioning for the second. They work separately, and they are most useful together.
Popover: show, hide, dismiss
<button popovertarget="menu">Open menu</button>
<div id="menu" popover>
<a href="/profile">Profile</a>
<a href="/settings">Settings</a>
</div>
That is already a working toggle. Adding the popover attribute hides the element by default (the browser sets display: none until it opens). The button with popovertarget becomes its invoker. For the default popover="auto" kind:
- Escape closes it, and clicking outside it closes it (light dismiss).
- Opening another auto popover closes the first, unless the second is nested within it.
- Focus returns to the invoker when it closes.
popover="manual" removes the light dismiss, for things like toasts. In JavaScript, el.showPopover(), hidePopover() and togglePopover() do the same work. You style the open state with :popover-open and the backdrop with ::backdrop.
Popover shows content in the top layer, the browser-managed layer above all normal content. That is why a popover is not cut off by an ancestor’s overflow: hidden and does not need a z-index. A stacking context cannot trap it either. A modal <dialog> uses the same layer. A popover is non-modal (the page behind stays usable) while dialog.showModal() blocks it.
Popover is in Chrome 114, Safari 17 and Firefox 125 or later per MDN’s compatibility data.
Anchor positioning: tether in CSS
#trigger {
anchor-name: --trigger;
}
#menu {
position-anchor: --trigger;
position-area: block-end span-inline-end;
margin: 0;
inset: auto;
position-try-fallbacks: flip-block;
}
anchor-namegives an element a dashed name (like a custom property name) so others can refer to it.position-anchoron the positioned element says which anchor to use.position-areadivides the area around the anchor into a 3 by 3 grid and picks cells.block-endis the cell below (in a horizontal language) centred on the anchor, andblock-end span-inline-endis below, extending from the anchor’s start edge toward the end. Physical keywords likebottom,top left,centeralso work.- The positioned element must be
position: absoluteorfixed. A popover already isfixed, but its default UA styles centre it withinset: 0; margin: auto, so resettingmarginandinsetas above lets the area take over.
I checked this in Chrome 157: a 60 by 20 px box with position-area: block-end came out directly under an 80 px wide button with its centre aligned to the button’s centre.
For finer control there is the anchor() function in inset properties (top: anchor(bottom)) and anchor-size() for matching widths (min-width: anchor-size(width)). position-area covers most layouts with less to write.
When it does not fit: position-try-fallbacks
A tooltip below a button near the bottom of the window is clipped. Fallbacks let CSS handle it:
#menu { position-try-fallbacks: flip-block, flip-inline; }
The browser tries the main placement first. If the element would overflow its containing block, it tries each fallback in order. flip-block mirrors vertically (below becomes above), flip-inline mirrors horizontally, and @position-try --my-fallback { ... } defines a custom one. I tested a menu anchored near the bottom of a window in Chrome and it moved above its button with flip-block.
Putting them together
The two features do not depend on each other. Anchor positioning works on any positioned element, such as a tooltip that shows on hover through :has() or a custom attribute, and popover works with ordinary positioning. Together they replace the classic trigger, panel and JavaScript positioning library trio with HTML and CSS.
Transitions work too. Popovers are display: none while closed, so entering and leaving animations need @starting-style and allow-discrete, as covered in transitions.
Support
According to MDN’s compatibility data (version 8.1.10, checked today), anchor-name and position-area are in Chrome 125 and 129, Safari 26, and Firefox 147. These are newer than popover, and the early Chrome versions shipped with different initial values and the old inset-area name, so a lot of older tutorials show syntax that has since changed. Check the date of any example you copy. If a browser lacks support, a visible fallback needs to exist: the popover can open in its default centered position, or the content can sit inline. The supports lesson covers how to structure that fallback.
Accessibility
Popover gives you invoker semantics and focus return, but the content is still your responsibility. Use real <button> elements for triggers and give controls accessible names. Menus with arrow-key navigation need more than popover. A tooltip that appears only on hover will not help keyboard or touch users, so show it on focus too. For anything modal, use <dialog>.
What the video covers
Kevin Powell’s “A couple of great anchor positioning use cases” (January 2026, about 20 minutes) has chapters: Introduction, Tethering to another element (0:45), anchor-scope (6:45), and Tethering to more than one element (10:24). It assumes you know the basic anchor-name and position-anchor pair. It does not teach popovers, so the popover half of this lesson comes from the documentation. The CSS Podcast episode 96 from the Chrome team (Una Kravets and Bramus) gives a broader overview of the API, including tooltips, popovers and menus, and Kevin’s popover animation video is also in further viewing. I could not read transcripts for any of these.
Where this fits
Anchor positioning is the newest layout tool in this roadmap and builds on positioning and stacking contexts. The last lesson in this group, supports and progressive enhancement, is about using features like this one responsibly.
Check yourself
What do you get from `<button popovertarget="menu">` and `<div id="menu" popover>` before writing any CSS or JS?
Clicking the button toggles the div. The shown element is rendered in the top layer, pressing Escape or clicking outside closes it (for the default `popover="auto"` kind), and focus returns to the button when it closes. `popover="manual"` turns off the light dismiss.
Why isn't a popover clipped by a parent with `overflow: hidden`, and why can you stop worrying about `z-index`?
While open it is placed in the top layer, a special layer the browser paints above all normal content, outside the ancestors' clipping and stacking contexts. Several open popovers stack in the order they were opened.
What three pieces do you need to place a tooltip below its button with anchor positioning?
`anchor-name: --tip` on the button, `position-anchor: --tip` on the tooltip (which also needs `position: fixed` or `absolute`; popovers are already fixed), and `position-area: block-end` to put it on the block-end side. In older syntax you would use `top: anchor(bottom)` and `left: anchor(left)` instead.
A dropdown near the bottom of the window gets cut off. How does anchor positioning handle that without JavaScript?
Add `position-try-fallbacks: flip-block`. If the first placement would overflow the containing block, the browser tries the flipped position, above the anchor instead of below, and uses it if that fits. You can list several fallbacks, or define your own with `@position-try`.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.