webdevpuneet.com

CSS Anchor-Positioned Tooltip Menu — Free Native Anchor Positioning Demo

CSS Anchor-Positioned Tooltip Menu · Navigation · Plain HTML, CSS & JS · Live preview

CategoryNavigation

Copy & paste

Source Code

<div class="demo-wrap">
  <p class="demo-note">The menu below is positioned relative to its trigger button using only the native CSS Anchor Positioning API (<code>anchor-name</code>, <code>position-anchor</code>, <code>anchor()</code>) &mdash; no JavaScript computes its coordinates in a supporting browser. In an unsupporting browser, a small JS fallback measures the button and positions the menu the old-fashioned way.</p>

  <div class="trigger-row">
    <button class="anchor-btn" id="anchorBtn" popovertarget="anchorMenu" aria-haspopup="menu">
      Account <span aria-hidden="true">&#9662;</span>
    </button>
  </div>

  <ul class="anchor-menu" id="anchorMenu" popover role="menu">
    <li role="none"><button role="menuitem" class="menu-item">Profile</button></li>
    <li role="none"><button role="menuitem" class="menu-item">Billing</button></li>
    <li role="none"><button role="menuitem" class="menu-item">Team settings</button></li>
    <li role="none" class="menu-divider"></li>
    <li role="none"><button role="menuitem" class="menu-item danger">Sign out</button></li>
  </ul>

  <div class="fallback-badge" id="fallbackBadge" hidden>JS fallback positioning active (anchor-name unsupported here)</div>
</div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month, 7 days free), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Pure CSS positioning
anchor-name + anchor() place the menu with zero JS in supporting browsers.
Native Popover API
Show/hide and light-dismiss handled by the browser, not custom listeners.
Feature-detected fallback
CSS.supports("anchor-name: --a") gates a JS positioning routine.
Fallback tracks resize/scroll
Manual positioning recalculates exactly when the anchor could move.
Visible fallback indicator
A badge confirms which code path is active for easy testing.
Accessible menu semantics
role="menu"/"menuitem" and aria-haspopup on the trigger.
No positioning library
Replaces the core job of a JS popper/floating-UI style dependency.
Honest 2026 support notes
Chromium-only today; Firefox and Safari not yet shipped.

About this UI Snippet

CSS Anchor-Positioned Tooltip Menu — Positioning a Popover With Pure CSS

Screenshot of the CSS Anchor-Positioned Tooltip Menu snippet rendered live

Every dropdown, tooltip, and popover library on the web has historically needed JavaScript to compute where the floating element should sit relative to its trigger — measuring the trigger's getBoundingClientRect(), accounting for scroll and viewport edges, and repositioning on every resize. The CSS Anchor Positioning API changes that for browsers that support it: an element can be tied to another element's edges directly in CSS, with the browser recalculating position on scroll, resize, and layout changes automatically.

The three pieces: anchor-name, position-anchor, anchor()

The trigger button declares anchor-name: --account-anchor, a custom-ident that names it as an anchor other elements can reference. The menu then sets position-anchor: --account-anchor to link itself to that specific anchor, and uses the anchor() function inside its top/left values — top: anchor(--account-anchor bottom) reads the anchor's bottom edge directly, and left: anchor(--account-anchor left) reads its left edge — so the menu's position is expressed declaratively against the button's real, live layout box, not a JS-computed snapshot.

Paired with the Popover API

This menu also uses the native popover attribute and popovertarget (see the native popover API demo for that piece in isolation) for the actual show/hide and light-dismiss behavior — clicking outside or pressing Escape closes it natively, with zero JS event listeners for that part. Anchor positioning and the Popover API are separate specs that happen to compose extremely well together: popover handles *whether* the menu is visible, anchor positioning handles *where* it sits.

Honest support status

Anchor Positioning is genuinely new and, as of 2026, Chromium-only: full support landed in Chrome and Edge 125 in 2024, but Firefox and Safari have not shipped it yet, both still tracking the specification. This is not a "safe everywhere with a vendor prefix" situation — it's an active, ongoing rollout. This snippet feature-detects with CSS.supports('anchor-name: --a') in JavaScript and, when unsupported, falls back to a small manual positioning routine using getBoundingClientRect(), re-run on open, resize, and scroll — the exact JS logic that anchor positioning exists to eventually make unnecessary.

Why bother with anchor positioning if you still need a JS fallback today

Even with a required fallback in 2026, the CSS-only path is worth adopting incrementally: browsers that support it get automatically correct positioning through scroll, resize, and dynamic content changes with no listeners at all, which is strictly less code running and fewer edge cases than a fully JS-driven solution — while the fallback guarantees nothing breaks elsewhere. As support broadens, the fallback branch simply stops running, and you can eventually delete it. Pair this with the native dialog showcase or the anchor positioning menu for more of this same emerging pattern.

Step by step

How to Use

  1. 1
    Click "Account"The menu opens via the native Popover API, positioned just below the button.
  2. 2
    Check for the fallback badgeIn a browser without Anchor Positioning support, a yellow badge confirms JS fallback positioning is active.
  3. 3
    Resize or scroll while it is openIn a supporting browser, CSS repositions it automatically; in the fallback, a resize/scroll listener recalculates it.
  4. 4
    Press Escape or click outsideThe popover light-dismisses natively, independent of the positioning method.
  5. 5
    Inspect anchor-name and anchor()See how top/left read the button's live edges directly in CSS.
  6. 6
    Swap the anchor edgeChange anchor(--account-anchor bottom) to top/right/left to reposition the menu.

Real-world uses

Common Use Cases

Account and settings dropdowns
Position a menu below its header button with anchor-name and anchor() in pure CSS, with no getBoundingClientRect calls.
Tooltips and popovers
Pair with the native popover API demo so show, hide and light-dismiss are handled by the browser.
Replacing a positioning library
Drop a floating-element dependency where CSS anchor positioning is supported, keeping a JavaScript fallback gated by CSS.supports.
Context and kebab menus
Anchor a right-click or overflow menu to its trigger, with the fallback recalculating whenever the anchor scrolls or the window resizes.
Progressive enhancement demos
Show a real fallback pattern, and compare with the anchor positioning menu for another take on the same API.
Related: Dot Pagination Carousel
See the Dot Pagination Carousel for a related navigation pattern worth pairing with this one.

Got questions?

Frequently Asked Questions

anchor-name assigns a custom-ident (a name you choose, prefixed with --) to an element, marking it as something other elements can position themselves relative to. It doesn't move or style the element itself — it only makes its layout box available as a named reference for the anchor() function elsewhere in the stylesheet.

The menu sets position-anchor: --account-anchor to link itself to that specific named anchor, then uses the anchor() function inside its top and left values, e.g. top: anchor(--account-anchor bottom), which reads the anchor element's real, live bottom edge directly in CSS. The browser keeps this position correct through scroll and layout changes without any JavaScript recalculating it.

As of 2026, it is Chromium-only: Chrome and Edge shipped full support starting from version 125 in 2024. Firefox and Safari have not shipped the Anchor Positioning API yet, both still tracking the specification, so any production use needs a genuine fallback rather than assuming universal support.

It checks CSS.supports('anchor-name: --a') once on load. If that returns false, it shows a visible fallback badge and switches to a getBoundingClientRect()-based positioning routine, re-run whenever the menu opens, the window resizes, or the page scrolls while the menu is open — the same recalculation triggers native anchor positioning would otherwise handle for free.

No — they are separate CSS/HTML specifications that happen to compose very well together. The Popover API (broadly supported in current browsers) handles whether an element is shown, hidden, and dismissed by clicking outside or pressing Escape; Anchor Positioning handles where a positioned element sits relative to another element. This snippet uses both, but you can use either independently.

Yes. While the CSS Anchor-Positioned Tooltip Menu snippet gives you one pattern to copy, the free Responsive Navbar Builder at webdevpuneet.com/navbar-builder/ designs a whole navigation bar: logo, links and dropdown menus, a mobile hamburger menu (dropdown panel, slide-in drawer or full screen) and a scroll effect (sticky, shrink, hide on scroll or transparent). It has six presets, a live preview that runs the real code, and exports HTML + CSS + vanilla JS, React, Tailwind, Vue 3 or Angular — or you can click Fork & Edit to keep editing it in My Code.