Positioning UI the Native Way with Popovers and Anchors
Build tooltips, menus, and overlays without JavaScript gymnastics
Building things like tooltips, dropdowns, and menus has always felt a little… overengineered.
You show something on click or hover, then spend the rest of your time figuring out where it should go, how it should stay aligned, and what happens when the layout shifts.
That usually means JavaScript. Measurements. Repositioning logic. Edge-case fixes.
Native popovers and CSS anchor positioning change that equation. They let the browser handle the relationship between trigger and UI—so you can focus on what it should do, not how to keep it in place.
Why this has always been harder than it looks
On the surface, these UI patterns seem simple. A button opens a menu. A link shows a tooltip. A control reveals some extra options.
The complexity shows up the moment positioning enters the picture.
The UI element needs to stay aligned to its trigger, avoid overflowing the viewport, respond to resizing, and behave consistently across different layouts. And because CSS historically did not understand that relationship, JavaScript had to fill the gap.
Over time, that turns into a surprising amount of logic for something that should feel straightforward.
What native popovers give you
The popover attribute brings a built-in way to show and hide UI elements without wiring up custom JavaScript behavior.
You define an element as a popover, then trigger it from a button or control. The browser handles the toggling, focus behavior, and basic interaction patterns.
<button popovertarget="menu">Open menu</button>
<div id="menu" popover>
Menu content
</div>That alone simplifies things—but the real value shows up when you start thinking about positioning.
Showing something is easy. Keeping it aligned is the hard part.
What CSS anchor positioning adds
CSS anchor positioning lets you tie one element’s position to another element—directly in CSS.
Instead of calculating offsets in JavaScript, you define a relationship: this element should sit relative to that one.
.trigger {
anchor-name: --menu-anchor;
}
.menu {
position-anchor: --menu-anchor;
top: anchor(bottom);
left: anchor(left);
}That reads almost like plain language: place the menu at the bottom-left of the trigger.
The browser now understands that connection and keeps it intact as the layout changes.
The mental model (how these work together)
These two features solve different parts of the same problem.
Popovers handle behavior—when something appears, how it opens and closes, how it interacts with focus.
Anchor positioning handles layout—where that thing should go, and how it stays aligned.
When you combine them, you get something that used to require a fair amount of custom logic:
a UI element that appears when needed and stays exactly where it belongs
The key shift is that the browser now understands the relationship between elements. You are not simulating it anymore.
Where this becomes immediately useful
Dropdown menus that stay attached
A classic dropdown is a perfect example. It should open from a trigger and stay aligned, even if the layout changes.
<button class="trigger" popovertarget="menu">Menu</button>
<div id="menu" class="menu" popover>
...
</div>.trigger {
anchor-name: --menu;
}
.menu {
position-anchor: --menu;
top: anchor(bottom);
left: anchor(left);
}No measurements. No recalculations. Just a defined relationship.
Tooltips that follow their target
Tooltips are another place where positioning logic tends to grow quickly.
With anchors, the tooltip can stay tied to its element without manual adjustments.
.tooltip {
position-anchor: --target;
bottom: anchor(top);
left: anchor(center);
}The layout stays stable even as content or viewport size changes.
Contextual UI that adapts naturally
More complex interfaces—like inline editors or contextual action menus—benefit even more from this model.
Instead of constantly recalculating positions, you define intent once and let the browser maintain it.
Less JavaScript, more intent
This is not about removing JavaScript entirely. It is about removing the parts that exist purely to work around missing layout capabilities.
Popovers replace a lot of show/hide wiring. Anchors replace a lot of positioning math.
What remains is logic that actually belongs in JavaScript—state, data, and interaction—not layout bookkeeping.
You are shifting from “calculate and correct” to “declare and let it work.”
What to watch for
These features are still relatively new, which means support and behavior can vary depending on your target environment.
It is also important to remember that anchor positioning defines relationships, not constraints. You may still need to think about how elements behave near edges or in tight spaces.
And while native popovers handle a lot for you, more complex interaction patterns may still require additional logic.
Use popovers for behavior, anchors for positioning, and let the browser handle the connection between them.
Can you use this today?
Support is improving quickly, but this is still an area where you should be intentional about progressive enhancement.
A common approach is to build a functional baseline using traditional positioning, then enhance it with popovers and anchors where supported.
That way, the experience improves in modern browsers without breaking in older ones.
Quick answers
Do popovers replace modals?
Not exactly. They overlap in some cases, but popovers are more about lightweight, contextual UI.
Is anchor positioning only for popovers?
No. It can be used anywhere you want one element to follow another.
What is the biggest benefit?
You stop writing code to keep things aligned—and start describing how they should relate.
Want to go deeper?
These features are part of a broader shift toward more capable, declarative UI in the platform. As they mature, patterns that once required custom solutions start to feel native.
Tooltips, menus, and overlays used to be “special cases.” Now they are starting to feel like first-class citizens.