CSS Anchor Positioning example

CSS Anchor Positioning example. Name an element with anchor-name, then position another against it. Tooltips and menus without JS coordinates.

The problem

Often, positioning an element relative to another is tricky. The anchor positioning API allows you to name an element and then position another element relative to that name. This is especially useful for tooltips, popovers, and other UI elements that need to be positioned dynamically.

The method

The anchor-name property allows you to assign a name to an element. The position-anchor property allows you to position another element relative to that named anchor. The position-area property specifies the preferred area around the anchor, and position-try-fallbacks allows you to specify fallback areas if the preferred area is not available.

The stage

Drag to a wall and the fallback takes the other side.

bottom

The example

The name is the join. The area is the seat. Fallbacks are the other seats.

Right
.pin {
  anchor-name: --pin;
}

.tip {
  position-anchor: --pin;
  position-area: right;
  position-try-fallbacks: flip-block, flip-inline;

  position: absolute;
  left: anchor(right);
  margin-left: 10px;
  top: anchor(top);
}

All Web API labs

  • CSS overflow carousel example
  • Customizable select example
  • CSS Custom Highlight API example