← Blog8 min read#css anchor positioning#anchor-name#tooltip

CSS Anchor Positioning: Tooltips and Popovers That Follow Their Target

CSS Anchor Positioning lets tooltips and popovers track their trigger element natively - no JavaScript, no libraries, just two CSS properties and a popover attribute.

Developer writing CSS tooltip positioning code on dark monitor screen

What CSS Anchor Positioning Actually Is

For a long time, floating UI - tooltips, dropdown menus, popovers, context menus - was JavaScript territory. You'd pull in Popper.js or Floating UI, wire up a ResizeObserver, call computePosition() on every scroll event, and hope nothing jittered when the window resized. It worked, but it was a lot of machinery for what is fundamentally a layout problem.

CSS Anchor Positioning changes that. Shipping in Chrome 125 (May 2024) with Firefox and Safari support landing through 2025 and 2026, it gives you two CSS properties - anchor-name on the reference element and position-anchor on the floating element - and a new set of anchor() function values you can use inside top, left, right, and bottom. The browser handles the geometry. The tooltip follows the button. You write maybe 15 lines of CSS.

Honestly, the feature is more significant than it looks on the surface. It's not just about tooltips. It's the first time CSS has had a proper concept of inter-element positional relationships, and it opens the door for a whole category of UI patterns that previously demanded runtime JavaScript.

That said, you still need JavaScript for things like toggling visibility on click or keyboard focus. CSS Anchor Positioning is a layout primitive, not a complete tooltip system. But it handles the hardest part - computing where the floating element should actually render - without a single line of script.

The Core Syntax: anchor-name and position-anchor

The API has two sides. The anchor element - your button, icon, or whatever the tooltip attaches to - gets anchor-name: --my-anchor. That double-dash prefix is not optional; these are CSS custom identifiers and they follow the same naming convention as custom properties. Then your floating element declares position-anchor: --my-anchor and uses anchor() calls in its inset properties.

Here's a minimal tooltip wired up entirely in CSS: ``css /* The trigger */ .btn-trigger { anchor-name: --tip-anchor; } /* The floating tooltip */ .tooltip { position: absolute; position-anchor: --my-anchor; /* Place it above the anchor, centered */ bottom: anchor(top); left: anchor(center); translate: -50% -8px; /* Visibility toggling still needs JS or :has() */ opacity: 0; pointer-events: none; transition: opacity 0.15s; } .btn-trigger:hover + .tooltip { opacity: 1; pointer-events: auto; } ``

The anchor() function takes one argument: which edge of the anchor element to align to. Your options are top, right, bottom, left, center, start, and end. Logical values (start/end) respect writing mode, which matters more than most people expect when you're supporting RTL languages.

Worth noting: position: absolute is required on the floating element. That might feel odd since you're no longer in the same stacking context as the anchor, but absolute positioning is how the browser lets the element escape its containing block. Without it, position-anchor has nothing to anchor *to* in the rendering pipeline.

One more thing - anchor-name values are scoped to the document by default. That means if two components on the same page both define --my-anchor, the second one wins. Use specific, namespaced names like --user-card-tooltip or generate them dynamically if you're building reusable components.

Flipping with position-try-fallbacks

The killer feature isn't just placing a tooltip above a button. It's automatically flipping that tooltip below when there isn't enough space at the top of the viewport. Before CSS Anchor Positioning, you'd compute that in JavaScript on every resize and scroll. Now you declare fallback positions and the browser figures it out at paint time.

.tooltip {
  position: absolute;
  position-anchor: --tip-anchor;

  /* Primary: above the anchor */
  bottom: anchor(top);
  left: anchor(center);
  translate: -50% -8px;

  /* Fallback: below the anchor if viewport clips it */
  position-try-fallbacks: --flip-below;
}

@position-try --flip-below {
  bottom: unset;
  top: anchor(bottom);
  translate: -50% 8px;
}

The @position-try at-rule defines an alternative geometry that the browser tries when the primary position causes overflow. You can chain multiple fallbacks - above, below, left, right - and the browser picks the first one that fits. In practice, a two-fallback setup (above + below) covers 95% of real-world tooltip cases without any JavaScript involved.

Quick aside: position-try-fallbacks also accepts the keyword flip-block, flip-inline, and flip-start for common cases so you don't always have to write a full @position-try block. position-try-fallbacks: flip-block is equivalent to swapping top/bottom, which is exactly what most tooltip flip logic does.

Look, this is the part where most tutorials stop. But there's a subtlety: the browser evaluates fallbacks in declaration order, and it uses the *first* one that doesn't overflow the scrollport. If you put the most common position last, you'll see flicker in narrow viewports. Put your most likely position first - this is usually 'above' for page-middle elements.

Combining with the Popover API

CSS Anchor Positioning pairs naturally with the HTML Popover API - the popover attribute and popovertarget that ship in all major browsers as of 2024. Together they handle two of the three hardest parts of floating UI: the browser manages top-layer stacking (no more z-index wars) and position tracking, while you just need to style the thing.

<button id="help-btn"
        popovertarget="help-tip"
        style="anchor-name: --help-anchor">
  Help
</button>

<div id="help-tip"
     popover
     style="position-anchor: --help-anchor">
  This feature exports your data as CSV.
</div>
```

```css
#help-tip {
  /* Popover resets position, so re-establish it */
  position: absolute;
  margin: 0;

  /* Position relative to anchor */
  top: anchor(bottom);
  left: anchor(center);
  translate: -50% 8px;

  /* Styling */
  background: #1a1a2e;
  color: #e2e8f0;
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 6px;
  padding: 8px 12px;
  font-size: 13px;
  max-width: 240px;

  position-try-fallbacks: flip-block;
}

A few things to know here. The popover attribute causes the element to live in the top layer, which means it escapes overflow-clip from any ancestor - exactly what you want for tooltips inside overflow-hidden cards or sticky headers. It also gets an automatic light-dismiss: clicking outside closes it. You get all of that for free.

In practice, the main gotcha is that popovers get their margin reset to auto by the UA stylesheet, and some browsers also reset position. Always explicitly set position: absolute; margin: 0 on your popover or you'll spend 20 minutes wondering why anchor positioning isn't working.

If you're building a full design system and want ready-made tooltip and popover components without this plumbing work, Empire UI ships pre-built variants across all major visual styles - including glassmorphism components that look incredible as floating overlays. Pair them with anchor positioning and you're done in minutes.

Real-World Tooltip Component

Let's build something you'd actually ship. A reusable React tooltip component that uses CSS Anchor Positioning for placement and the Popover API for top-layer rendering and light-dismiss. No Floating UI, no Popper. Just HTML, CSS, and ~40 lines of React.

// Tooltip.tsx
import { useId, useRef } from 'react';
import './tooltip.css';

interface TooltipProps {
  label: string;
  children: React.ReactElement;
  side?: 'top' | 'bottom' | 'left' | 'right';
}

export function Tooltip({ label, children, side = 'top' }: TooltipProps) {
  const id = useId().replace(/:/g, '');
  const anchorName = `--tooltip-${id}`;

  return (
    <span className="tooltip-wrapper">
      {/* Anchor trigger */}
      <span
        style={{ anchorName } as React.CSSProperties}
        tabIndex={0}
        aria-describedby={id}
      >
        {children}
      </span>

      {/* Floating tooltip */}
      <span
        id={id}
        role="tooltip"
        className={`tooltip tooltip--${side}`}
        style={{ positionAnchor: anchorName } as React.CSSProperties}
      >
        {label}
      </span>
    </span>
  );
}
```

```css
/* tooltip.css */
.tooltip-wrapper {
  position: relative;
  display: inline-block;
}

.tooltip {
  position: absolute;
  margin: 0;
  width: max-content;
  max-width: 220px;
  background: #18181b;
  color: #fafafa;
  font-size: 12px;
  padding: 5px 10px;
  border-radius: 4px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.12s ease;
  z-index: 50;
}

.tooltip--top {
  bottom: anchor(top);
  left: anchor(center);
  translate: -50% -6px;
  position-try-fallbacks: flip-block;
}

.tooltip--bottom {
  top: anchor(bottom);
  left: anchor(center);
  translate: -50% 6px;
  position-try-fallbacks: flip-block;
}

.tooltip-wrapper:hover .tooltip,
.tooltip-wrapper:focus-within .tooltip {
  opacity: 1;
}

The useId() hook generates a unique anchor name per instance - critical when you have multiple tooltips on the page. Without unique anchor names you'd get the CSS scoping collision mentioned earlier. This pattern scales to any number of tooltips without a single lookup table or ref-based position calculation.

Worth noting: TypeScript doesn't know about anchorName or positionAnchor as valid CSS properties yet (as of TypeScript 5.5). The as React.CSSProperties cast sidesteps the type error without disabling strictness globally. Once these properties land in the TypeScript DOM types, you can drop the cast.

For teams who want this styled and production-ready out of the box, the Empire UI component library already ships tooltip components across every visual theme. The box shadow generator is also handy here - tooltip drop shadows at around 0 4px 16px rgba(0,0,0,0.3) tend to look best without overwhelming the content.

Browser Support, Progressive Enhancement, and Fallbacks

Here's where you need to be pragmatic. As of September 2026, CSS Anchor Positioning has strong Chromium coverage (Chrome 125+, Edge 125+) and Safari support from Safari 18.2. Firefox shipped it in Firefox 131. That covers the vast majority of global browser usage - but you should still feature-detect and fall back gracefully.

/* Feature detection */
@supports (anchor-name: --test) {
  .tooltip {
    position: absolute;
    position-anchor: --trigger-anchor;
    bottom: anchor(top);
    left: anchor(center);
    translate: -50% -8px;
    position-try-fallbacks: flip-block;
  }
}

/* Fallback: absolute position relative to wrapper */
@supports not (anchor-name: --test) {
  .tooltip-wrapper {
    position: relative;
  }
  .tooltip {
    position: absolute;
    bottom: calc(100% + 8px);
    left: 50%;
    translate: -50% 0;
  }
}

The fallback is just old-school relative/absolute positioning inside the wrapper. It doesn't auto-flip, but it works everywhere. Most tooltips don't desperately need flip behavior - it's a nice-to-have, not a functional requirement. Ship the enhanced version with anchor positioning and let the fallback handle the edge cases.

In practice, @supports (anchor-name: --test) is the right gate. Avoid checking for position-try-fallbacks specifically because it's a newer addition and you'd end up missing some browsers that support the core anchor positioning features just fine without flip support.

One more thing - if you're using a CSS-in-JS library or Tailwind, arbitrary values let you drop these in without ejecting to a separate stylesheet. In Tailwind 4 you can write [anchor-name:--my-tip] in your className. It's not pretty, but it works for one-off cases until official utilities land.

Context Menus, Submenus, and Beyond Tooltips

Tooltips are the obvious demo case, but CSS Anchor Positioning really shines for context menus and nested dropdowns - patterns where JavaScript position computation gets especially messy. A right-click context menu that opens relative to a table row. A submenu that appears to the right of its parent item, flipping left when it'd overflow the viewport. All of that is now declarable in CSS.

/* Context menu positioned next to the row that was right-clicked */
.context-menu {
  position: absolute;
  position-anchor: --row-anchor;
  top: anchor(top);
  left: anchor(right);
  translate: 4px 0;
  position-try-fallbacks: --flip-left, --flip-below;
}

@position-try --flip-left {
  top: anchor(top);
  left: unset;
  right: anchor(left);
  translate: -4px 0;
}

@position-try --flip-below {
  top: anchor(bottom);
  left: anchor(left);
  translate: 0 4px;
}

For dynamic context menus where the anchor changes (right-clicking different rows), you update anchor-name on the row via JavaScript and the browser recomputes the position automatically. No computePosition() call on every click. The amount of code you delete when migrating a context menu from Floating UI to native anchor positioning is genuinely satisfying.

If you're building visual interfaces that need consistent, design-system-level tooltip and popover styles, it's worth checking what Empire UI already ships. The glassmorphism generator can even help you prototype the visual treatment for your floating surfaces - that frosted glass look on a tooltip or context menu is having a serious moment in 2026 product design.

The bigger picture: CSS Anchor Positioning is part of a broader trend of the browser getting smarter about layout problems that used to require JavaScript. CSS Scroll-Driven Animations, View Transitions, the Popover API - they're all moving logic out of userland and into the rendering engine, where it belongs. Worth keeping an eye on what comes next.

FAQ

Is CSS Anchor Positioning ready for production in 2026?

Yes, for most projects. Chrome 125+, Edge 125+, Firefox 131+, and Safari 18.2+ all support it. Add an @supports fallback for older browsers and you're covered - the fallback is just regular relative/absolute positioning.

Do I still need Floating UI or Popper.js with CSS Anchor Positioning?

For basic tooltips and single-level popovers, no. For complex cases like virtual lists, iframe-crossing overlays, or extensive accessibility management, Floating UI still adds value on top of native positioning.

Why does my tooltip position break when the element is inside an overflow: hidden container?

Use the HTML popover attribute on your tooltip element - it moves the element into the top layer, which escapes overflow clipping from any ancestor. Then combine it with CSS Anchor Positioning for placement.

Can multiple tooltips on the same page have different anchor-name values?

They must have different values or the last-declared one wins. Use JavaScript's useId() in React or a similar uniqueness mechanism to generate scoped anchor names per component instance.

Free components in 41 styles
React & Tailwind, copy-paste ready.
Browse β†’

Read next

CSS Animations & Motion Design: The Complete 2026 Playbook β†’CSS Anchor Positioning: Tooltips and Popovers Without JS Libraries β†’Intersection Observer in React: Scroll-Triggered Animations the Right Way β†’Glassmorphism Tooltip: Frosted-Glass Floating Labels in React β†’