Sub topic 10
How anchored elements follow scrolling
This chapter assumes you know position: absolute and fixed, containing blocks, and the basics of anchor-name, position-anchor, and position-area. It explains what happens when the page or a scroll container moves after you have anchored an element.
The working model
Before anchor positioning, a tooltip placed with JavaScript had to be recalculated on every scroll event. With anchor positioning the browser does this work itself.
The browser lays out the anchored element against the anchor's position. When a scroll container between the anchor and the anchored element's containing block scrolls, the browser shifts the anchored element by the same amount. The shift happens at paint time. It does not trigger a new layout, so no scroll listeners are needed and the element does not lag behind the anchor.
A tooltip outside the scroller
The anchor sits inside a scrolling box. The tooltip sits outside it, so the scroll container does not clip it.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Anchor and scroll</title>
<style>
.scroller {
height: 200px;
width: 300px;
overflow: auto;
border: 1px solid #888;
}
.spacer {
height: 300px;
}
.trigger {
anchor-name: --tip;
}
.tooltip {
position: fixed;
position-anchor: --tip;
position-area: top;
margin-bottom: 8px;
position-visibility: anchors-visible;
}
</style>
</head>
<body>
<div class="scroller">
<div class="spacer"></div>
<button class="trigger">Scroll me</button>
<div class="spacer"></div>
</div>
<div class="tooltip">Attached</div>
</body>
</html>Scroll the box and the tooltip moves with the button. The .tooltip element is a sibling of the scroller, not a child. It is positioned against the viewport, and the browser adds the scroller's offset to its position.
Leaving the visible area
The tooltip is not a descendant of the scroller, so the scroller's overflow does not clip it. When the button scrolls out of the box, the tooltip would still float over the page next to the box's edge.
position-visibility: anchors-visible handles this. It hides the anchored element while its anchor is clipped out of view by a scroll container. Other values, such as no-overflow, are covered in the chapter on hiding anchored elements.
Common setups and their results
| Setup | Result |
|---|---|
| Anchored element outside the scroller, `position: fixed` or `absolute` | Follows the anchor and is not clipped |
| Anchored element inside the same scroller, `position: absolute` | Scrolls naturally with the content and is clipped by the scroller |
| Anchored element in a different scroller than the anchor | Follows the anchor's scrolling, but the element's own scroller clips it |
| Anchor with `display: none` | No anchor box exists, so the element is not anchored |
Common errors
- The tooltip is cut off at the scroller's edge. The tooltip is inside an element with
overflow: autoorhidden. Move it out of the scroller, or use the top layer with a popover. - The tooltip jumps instead of following. Check that you are not recalculating
topandleftin JavaScript. Those values override the anchor-based values and cause conflicts. - The tooltip hangs in space after the anchor scrolls away. Add
position-visibility: anchors-visible. - Two scrollers confuse the result. Only scroll containers between the anchor and the anchored element's containing block are compensated. If you cannot predict the result, simplify the nesting.
Exercise
A tooltip is a child of a div with overflow: auto, and its anchor is a button in that same div. The tooltip is position: absolute. Why does the tooltip scroll with the button, and why is it clipped when it sits near the top edge of the div?
Show answer
Both elements are inside the same scroll container, so they scroll together as ordinary content, with no special compensation needed. Because the tooltip is inside the container, overflow: auto clips anything that extends beyond its box. To avoid clipping, move the tooltip outside the scroller (for example as a sibling with position: fixed) or make it a popover in the top layer.