# Anchor Positioning & color-mix()

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

Subject: CSS | Lesson 40 of 44 | 5 min read
Interactive lesson: https://codewithanimation.in/css/anchor-positioning
Previous: [Cascade Layers (@layer)](https://codewithanimation.in/css/cascade-layers.md) | Next: [Logical Properties & i18n](https://codewithanimation.in/css/logical-properties-and-i18n.md)

## 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.

## Common mistake: Screen edges and flipping

A common pain point with tooltips is when they hit the edge of the viewport. If a tooltip is pinned to the bottom of a button, but the user scrolls the button to the bottom of the screen, the tooltip gets cut off.

Anchor Positioning includes a native collision engine. Using `position-try-options: flip-block;`, you can instruct the browser to automatically flip the tooltip to the *top* of the button if the bottom lacks space.

## Tip: Mixing colors on the fly

You can use `color-mix()` to tint your tooltips to match the anchor's context. For example, `background: color-mix(in oklch, var(--theme) 20%, white);` mixes your theme color with white to create a soft, dynamic background.

## Try it: 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.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/anchor-positioning

## Check yourself

### Question 1

Which property transforms a standard element into an anchor that other elements can tether to?

- A. anchor-name
- B. position-anchor
- C. display: anchor
- D. tether-to

Correct answer: A

You use `anchor-name: --custom-name;` to register an element as an anchor in the browser's layout engine.

- Why not B: `position-anchor` is applied to the tooltip to tell it *which* anchor to attach to.
- Why not C: Anchor positioning works alongside standard display properties; it is not a layout mode itself.
- Why not D: This property does not exist in CSS.

### Question 2

How does native anchor positioning handle a tooltip colliding with the edge of the screen?

- A. It uses position-try-options to automatically flip or shift the tooltip.
- B. It requires a JavaScript IntersectionObserver to detect collisions.
- C. It simply clips the tooltip off-screen.
- D. It forcibly scrolls the window to keep the tooltip visible.

Correct answer: A

Properties like `position-try-options: flip-block` allow the browser's layout engine to test alternative positions and flip the tooltip automatically if it runs out of space.

- Why not B: The entire point of the API is to eliminate the need for JS collision detection.
- Why not C: By default it might, but the spec provides built-in fallback mechanisms.
- Why not D: The browser does not hijack scroll; it repositions the element.

### Question 3

What is the primary benefit of the CSS `color-mix()` function?

- A. It blends two colors directly in the browser, enabling dynamic theme variants.
- B. It converts RGB to Hex format.
- C. It allows you to declare CSS variables.
- D. It prevents contrasting colors from clashing.

Correct answer: A

`color-mix()` allows you to take an existing CSS variable (like a primary brand color) and generate shades, tints, or translucent variants entirely in CSS, without needing Sass.

- Why not B: It can output different formats, but its primary job is mixing.
- Why not C: CSS variables (`--var`) are declared independently.
- Why not D: It mathematically mixes colors; it doesn't automatically guarantee accessibility.

## 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.
