## The Problem with JavaScript Positioning For years, positioning tooltips, popovers, and dropdown menus required JavaScript libraries like Popper.js or Floating UI. You'd calculate positions, handle viewport collisions, manage scroll events, and fight z-index wars. CSS Anchor Positioning eliminates all of that. You declare which element to anchor to, and where to place the positioned element - that's it. ## The Core Concept Anchor Positioning works in two steps: 1. **Name an anchor** - Give any element an `anchor-name` 2. **Position relative to it** - Use `position-anchor` and `position-area` on the positioned element ```css /* Step 1: Name the anchor */ .trigger-button { anchor-name: --my-button; } /* Step 2: Position relative to it */ .tooltip { position: fixed; position-anchor: --my-button; position-area: top center; } ``` That's the entire concept. No JavaScript coordinates, no resize observers, no manual calculations. ## Real Example: Floating Summary Modal Here's how I position the AI summarizer modal on this site: ```css .summary-bubble { position: fixed; bottom: 2rem; right: 2rem; width: 64px; height: 64px; anchor-name: --summary-bot; } .summary-modal { position: fixed; position-anchor: --summary-bot; position-area: top left; max-width: 400px; width: 90vw; } ``` The modal automatically positions itself above and to the left of the button. When the button moves (different screen sizes, scroll position), the modal follows. ## Understanding `position-area` The `position-area` property uses a 9-region grid model: ``` ┌─────────┬────────┬──────────┐ │ top │ top │ top │ │ left │ center │ right │ ├─────────┼────────┼──────────┤ │ center │ center │ center │ │ left │ │ right │ ├─────────┼────────┼──────────┤ │ bottom │ bottom │ bottom │ │ left │ center │ right │ └─────────┴────────┴──────────┘ ``` Common values: - `top center` - Above the anchor, centered - `bottom left` - Below the anchor, aligned left - `span-top span-left` - Span across top-left quadrant ## Real Example: Share Button Notification Here's a notification positioned relative to a share button: ```css #share { anchor-name: --share-button; } #notification { position: fixed; position-anchor: --share-button; bottom: calc(anchor(top) + 10px); /* Position 10px above the anchor's top edge */ } ``` The `anchor()` function gives you precise control - reference any edge of the anchor and add offsets. ## Real Example: Carousel Navigation For scroll marker navigation in carousels: ```css ul.carousel { anchor-name: --badges-carousel; /* ... scroll snap styles */ } ul.carousel::scroll-marker-group { position: fixed; position-anchor: --badges-carousel; position-area: block-end; /* Positioned below the carousel */ } ``` ## Dynamic Anchor Names For lists where each item needs its own anchor, use CSS custom properties: ```astro --- const items = ['Item 1', 'Item 2', 'Item 3'] --- {items.map((item, i) => (