Dive into the fascinating world of CSS anchor positioning! This tutorial explores this innovative CSS feature, using Juan Diego Rodriguez's comprehensive "Anchor Positioning Guide" (available on CSS-Tricks) as our reference.
This is an exciting development. Many will recall the impact of CSS-Tricks' "Flexbox Layout Guide" and "Grid Layout Guide"— invaluable resources I still use regularly! I often juggle multiple tabs to ensure correct syntax in my CodePen experiments.
Since Juan's guide, I've been experimenting with CSS anchor positioning, and I'm eager to share my findings, learn more, experiment further, and, of course, build things!
Introducing CSS Anchor Positioning
Anchor positioning allows us to connect—or "anchor"—one element to one or more others. Crucially, it defines how a "target" element (the element attached to an anchor) is positioned relative to the anchor, including fallback positioning via the @position-try
at-rule.
Simply put, anchor positioning significantly enhances position: absolute;
, helping absolutely-positioned elements behave predictably. We'll explore this in detail.
Currently a W3C draft spec, anchor positioning is relatively new. It's marked "limited availability" in Baseline, meaning it's primarily supported by Chromium-based browsers (version 125 ). However, Oddbird provides a helpful polyfill for broader browser compatibility.
Desktop Browser Support
Mobile/Tablet Browser Support
Oddbird creates polyfills for many new CSS features. Support their work on GitHub or Open Collective!
Tab Atkins-Bittner, a contributor to the W3C spec, presented anchor positioning at CSS Day 2024 (video available on YouTube). Juan demonstrated its use with view-driven animations for a floating notes effect on CSS-Tricks. Kevin Powell showcased "CSS Popover Anchor Positioning" in a recent video, and Thomas Park created Anchoreum (a Flexbox Froggy-style game) to teach anchor positioning.
The Assignment
Now that we understand CSS anchor positioning, let's explore its functionality. Tethering elements offers immense potential, solving many issues previously tackled with complex absolute positioning and z-index
adjustments.
Let's examine the basic syntax. We need two elements: an anchor-positioned element and its tethered target.
<div> Anchor </div> <div> Target </div>
We designate an anchor-positioned element using anchor-name
, a unique name (prefixed with double dashes, like CSS custom properties).
https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15banchor { anchor-name: --anchor; }
The target element requires position: absolute;
and position-anchor
(matching the anchor's anchor-name
).
https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15banchor { anchor-name: --anchor; } https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15btarget { position: absolute; position-anchor: --anchor; }
These elements are now linked. position-area
creates an invisible 3x3 grid over the anchor element, allowing precise target placement.
https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15btarget { position: absolute; position-anchor: --anchor; position-area: top center; }
The target is now anchored to the top-center of the anchor element!
Anchoring Pseudo-elements
Pseudo-elements can be anchored like regular elements:
https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15banchor { anchor-name: --anchor; &::before { content: "Target"; position: absolute; position-anchor: --anchor; left: anchor(center); bottom: anchor(center); } }
This is useful for adding visual enhancements or indicators.
Moving Anchors
Anchors can be repositioned using anchor()
functions instead of position-area
.
https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15btarget { position: absolute; position-anchor: --anchor-one; top: anchor(bottom); left: anchor(left); }
anchor()
functions use the anchor element's computed values. Here, the target's top
matches the anchor's bottom
. Hovering changes position-anchor
and smooth transitions are possible.
Advanced Experiments
The Chrome team released new pseudo-selectors for <details></details>
and <summary></summary>
elements (:details-content
). These can also be anchored. This is experimental but demonstrates the potential.
Practical Applications
Let's explore practical uses of CSS anchor positioning (currently Chrome-only).
Tooltips
Tooltips are a natural fit. Hovering over an icon displays a nearby label. Zell Liew's article on tooltip best practices provides semantic guidance.
<button id="inbox-tool" class="tool" aria-labelledby="inbox-label"> <svg></svg> </button> <div id="inbox-label" role="tooltip">Inbox</div>
The tooltip is a sibling of the anchor element (aria-labelledby
links them). CSS handles positioning and visibility.
https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15binbox-tool { anchor-name: --inbox-tool; } https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15binbox-label { position: absolute; position-anchor: --inbox-tool; position-area: end center; visibility: hidden; } .tool:hover + [role="tooltip"], .tool:focus-visible + [role="tooltip"] { visibility: visible; }
A working CSS-anchored tooltip! Consider toggletips for touch devices.
Floating Disclosures
Disclosures (like
) benefit from anchor positioning, particularly for floating elements. The Popover API provides a non-modal, top-layer solution with features like light dismissals. Zell Liew offers further information on popovers, dialogs, and modality.
A dropdown menu example:
<div> Anchor </div> <div> Target </div>
CSS handles anchoring and fallback positioning:
https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15banchor { anchor-name: --anchor; }
Shopping Cart Component
Combining previous techniques, we can create a shopping cart component:
https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15banchor { anchor-name: --anchor; } https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15btarget { position: absolute; position-anchor: --anchor; }
CSS anchors the tooltip, shopping cart dialog, and badge:
https://www.php.cn/link/93ac0c50dd620dc7b88e5fe05c70e15btarget { position: absolute; position-anchor: --anchor; position-area: top center; }
Three elements anchored to a single anchor!
Combining Techniques
This section showcases the combined use of tooltips, disclosures, and badges, demonstrating the power of anchor positioning.
Final Project: Onboarding Tool
As a final project, I built a CSS anchor-positioned onboarding tool (CodePen available). This avoids the complexities of my previous JavaScript-based attempt ("HandHoldJS").
A custom element <hand-hold></hand-hold>
uses data-tour-stop
attributes to define tour steps. JavaScript dynamically updates anchor positions, and a View Transition ensures smooth transitions. This project is experimental and not production-ready due to limited browser support.
Conclusion
CSS anchor positioning has the potential to revolutionize CSS development, similar to flexbox and grid. Its applications are vast, and I'm excited to see future innovations from the community.
The above is the detailed content of One of Those 'Onboarding' UIs, With Anchor Positioning. For more information, please follow other related articles on the PHP Chinese website!

I got this question the other day. My first thought is: weird question! Specificity is about selectors, and at-rules are not selectors, so... irrelevant?

Yes, you can, and it doesn't really matter in what order. A CSS preprocessor is not required. It works in regular CSS.

You should for sure be setting far-out cache headers on your assets like CSS and JavaScript (and images and fonts and whatever else). That tells the browser

Many developers write about how to maintain a CSS codebase, yet not a lot of them write about how they measure the quality of that codebase. Sure, we have

Have you ever had a form that needed to accept a short, arbitrary bit of text? Like a name or whatever. That's exactly what is for. There are lots of

I'm so excited to be heading to Zürich, Switzerland for Front Conference (Love that name and URL!). I've never been to Switzerland before, so I'm excited

One of my favorite developments in software development has been the advent of serverless. As a developer who has a tendency to get bogged down in the details

In this post, we’ll be using an ecommerce store demo I built and deployed to Netlify to show how we can make dynamic routes for incoming data. It’s a fairly


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Notepad++7.3.1
Easy-to-use and free code editor