CSS Anchor Positioning: Build Viewport-Aware Overlays Without JavaScript
Attach menus and tooltips to their triggers, then let CSS choose a fallback when space runs out.
CSS anchor positioning connects a positioned element to an anchor elsewhere in the document. It can place a menu beside its trigger, match its width, and try alternate positions when the preferred placement overflows.
THE PRODUCTION PROBLEM STATEMENT
Overlay libraries often measure elements, watch scrolling, and recalculate coordinates in JavaScript. The browser already has the layout information; anchor positioning lets it keep related UI aligned as the page moves and resizes.
Connect an anchor and a positioned element
Give the anchor a dashed custom identifier with anchor-name, then refer to it with position-anchor. Use logical position-area values so placement follows writing direction instead of assuming left-to-right text.
For a popover opened with popovertarget, browsers can establish an implicit relationship between the trigger and popover. That avoids generated anchor names for many common menus and tooltips.
.help-popover {
margin: 0;
position: fixed;
position-area: block-end;
position-try-fallbacks: flip-block;
}Use fallback positions instead of collision math
A preferred position can be followed by fallback tactics such as flip-block or flip-inline. The browser evaluates those choices against available space and performs the layout as the anchor or viewport changes.
Use @supports to provide a conventional placement for older engines. Test both sides of the viewport and nested scrolling containers: fallback behavior should keep the panel usable, and the panel should remain associated with the intended control.
Keep positioning separate from interaction and semantics
Anchor positioning solves geometry. It does not create keyboard behavior, accessible names, dismissal rules, or modal focus management. Use native popovers for transient overlays and dialog for modal workflows, then add the appropriate labeling and keyboard behavior.
Feature support changes over time. Treat this as progressive enhancement, check the browser support required by your audience, and keep a usable fallback for critical controls.
Frequently Asked Questions & Technical Inquiries
Direct, practical answers to common architectural and debugging questions.
No. It controls where a positioned element appears. Use the Popover API or application state to control whether the element is open.
Faisal Askari
Verified AuthorSoftware Engineer & Full-Stack Developer
Faisal Askari is a software engineer with deep expertise in web systems architecture, the JavaScript runtime ecosystem, and client-side performance engineering. Over years of hands-on production engineering, he has designed and shipped enterprise dashboards, high-converting digital interfaces, and specialized full-stack applications.
Mastering Core Web Vitals: Engineering for Sub-100ms INP and Optimal LCP
A technical blueprint for diagnosing Interaction to Next Paint bottlenecks, accelerating Largest Contentful Paint, and maintaining zero Cumulative Layout Shift in production.
Mastering CSS Subgrid: Perfect Card Alignment and Fluid Multi-Column Form Layouts
How subgrid solves the historical card height mismatch, flushes CTA buttons, and creates synchronized multi-column forms without JavaScript.