Anchor Positioning & color-mix()

Tether a tooltip to a moving element entirely in CSS. No JavaScript positioning libraries required.

CSS5 min readConcept 39 of 43

Pinning elements together

For years, building a robust tooltip or dropdown menu meant using JavaScript. If the user scrolled or the window resized, JS had to constantly recalculate coordinates so the tooltip didn't detach from its target.

CSS Anchor Positioning solves this natively. You define an 'anchor' element, and tell a positioned element (like a tooltip) to tether itself to that anchor.

As the anchor moves, the browser automatically updates the tooltip's position in real-time, matching the performance of native scrolling.

Replacing complex JavaScript

Libraries like Popper.js and Floating UI were created specifically to solve this problem. While excellent, they add JavaScript payload and computational overhead.

Native anchor positioning removes that payload. It also guarantees perfect synchronization: because the browser handles the layout natively, the tooltip never 'lags' behind the anchor during fast scrolling.

Additionally, the new color-mix() function allows you to dynamically blend colors in CSS (e.g., creating a 10% translucent version of your primary theme color), further reducing reliance on CSS preprocessors or JS.

The anchor-name and anchor() function

First, declare the anchor by giving it a custom dashed name: .button { anchor-name: --my-anchor; }.

Second, tether the absolute element to it: .tooltip { position: absolute; position-anchor: --my-anchor; }.

Finally, use the anchor() function to set coordinates. .tooltip { top: anchor(bottom); justify-self: anchor-center; } places the tooltip directly below the center of the button.

Try it

Drag the anchor element around the boundary box. Watch the CSS-powered tooltip perfectly track its target, and observe the dynamic color mixing in action.

Anchor Positioning & Color Mix

Tooltip Placement

// 1. Declare the anchor
.anchor-button {
anchor-name: --target;
}
// 2. Tether the tooltip & mix colors
.tooltip {
position: absolute;
position-anchor: --target;
/* Pin to top edge */
bottom: anchor(top);
justify-self: anchor-center;
/* Dynamic tinted background */
background: color-mix(
in srgb,
var(--theme) 15%,
white
);
}
Anchor
Pure CSS Tooltip
Drag the anchor box

Check yourself

Pick an answer to lock it in, then read why. Getting one wrong is part of how it sticks.

  1. 1Which property transforms a standard element into an anchor that other elements can tether to?
  2. 2How does native anchor positioning handle a tooltip colliding with the edge of the screen?
  3. 3What is the primary benefit of the CSS `color-mix()` function?

Remember this

  • Anchor Positioning tethers a positioned element to an anchor element without JavaScript.
  • Define the anchor with anchor-name, and attach the tooltip with position-anchor.
  • Use the anchor() function (e.g., top: anchor(bottom)) to map edges together.
  • Use color-mix() to dynamically generate color variants directly in native CSS.

Done with this concept?

Mark it complete to track your progress. No login needed.