Centring a floating element relative to an arbitrary trigger element historically required cumbersome coordinate arithmetic—combining left: anchor(center) with transform: translateX(-50%) or complex margin offsets. Within Google Chrome, the modern CSS Anchor Positioning specification introduces justify-self: anchor-center and align-self: anchor-center. These alignment keywords enable developers to align anchored floating elements to the visual midpoint of an anchor cleanly and declaratively without CSS transforms.
The Evolution from Transforms to Anchor-Center
In legacy CSS layouts, centering an absolutely positioned box over an anchor element typically relied on CSS transforms:
/* Legacy transform centering approach */
.legacy-anchored-popover {
position: fixed;
position-anchor: --target-button;
left: anchor(center);
transform: translateX(-50%);
}
While functional, introducing CSS transforms establishes a new stacking and containing context, potentially disrupting nested fixed descendants, sub-pixel text rendering, and animation transitions. The new anchor-center alignment keyword integrates directly into the CSS Box Alignment model, centring the element along the designated inline or block axis without side effects.
Configuring Strict Anchor-Center Stylesheets
To align a floating popover or dropdown menu to the exact horizontal centre of an action button:
/* Define the reference button anchor */
.primary-action-trigger {
anchor-name: --action-trigger-anchor;
}
/* Centred floating contextual popover */
.centered-floating-popover {
position: fixed;
position-anchor: --action-trigger-anchor;
top: anchor(bottom);
justify-self: anchor-center;
margin-top: 8px;
background-color: #0f172a;
color: #f8fafc;
padding: 12px 20px;
border-radius: 8px;
box-shadow: 0 10px 25px -5px rgba(0, 0, 0, 0.2);
}
Because justify-self: anchor-center calculates alignment before layout boundaries are resolved, the anchored box centres precisely around the reference anchor midpoint, respecting margins and avoiding overflow.
Bidirectional Centering for Modal Overlays
For callouts that need to center both horizontally and vertically over a target focal point (such as an interactive map pin or guided tour hotspot), combine both axes:
.hotspot-radar-badge {
position: fixed;
position-anchor: --map-hotspot-anchor;
justify-self: anchor-center;
align-self: anchor-center;
pointer-events: none;
}
Auditing and Debugging in Chrome DevTools
Google Chrome provides specialized DevTools tooling to audit anchor-center alignment:
- Open DevTools (
F12) and select the Elements panel. - Locate the anchored element in the DOM tree.
- In the Styles panel, examine
justify-self: anchor-center. DevTools displays the alignment badge. - Hover over the anchor property to view visual alignment vectors showing the calculated center axis connecting the anchor to the floating box.
- Resize the viewport or toggle device emulation mode to verify that the element tracks the anchor centre dynamically without jitter.