Sub topic 10
How anchored elements follow scrolling
This chapter explains what happens to an anchored element when the page, or a scrolling box that contains the anchor, scrolls. You will see how the browser keeps the element attached without any JavaScript. You will also see where that tracking leaves visible problems, and how position-visibility fixes them.
It assumes you can already declare an anchor with anchor-name, link an element with position-anchor, and place it with anchor() or position-area. It also assumes you know what a containing block is and what makes an element a scroll container (overflow: auto, scroll, or hidden).
The problem scrolling creates
Before anchor positioning, a tooltip or menu had to be repositioned by script. A scroll listener read getBoundingClientRect() on the trigger and rewrote top and left on the popup. This code ran on every scroll event, often lagged a frame behind the scroll, and was easy to get wrong with nested scrollers.
With anchor positioning, you describe the relationship once in CSS. The browser keeps the relationship true as scroll offsets change. You write no listener and no measurement code.
The default behavior: scroll compensation
The anchor and the anchored element can sit in different scroll containers. Consider these two cases.
- Same scroller. The anchored element's containing block is inside the same scroll container as the anchor. Both move together when the box scrolls, as any two descendants would. Nothing special happens.
- Different scrollers. The anchor is inside a scrollable box, but the anchored element's containing block is outside it, such as the page or a popover in the top layer. Scrolling the box moves the anchor but would not normally move the element.
In the second case, the browser applies anchor scroll compensation. It shifts the anchored element by the scroll offset of the scroll containers that lie between the anchor and the element's containing block. The element appears to stay glued to the anchor while the anchor moves.
Three details matter in practice.
- The shift is applied after layout, as a visual offset. Scrolling does not trigger a new layout pass for each pixel, and the browser can often do it off the main thread.
- The compensation only follows scroll offsets. It does not track other things that change the anchor's size or position, such as a font loading. Those cause a normal re-layout, which recomputes the placement.
- It works for
position: absoluteandposition: fixed. It also works for popovers in the top layer, whose containing block is the viewport.
A runnable example
The list below scrolls. One row is the anchor. The note is a sibling of the list, so its containing block is the page, not the list.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Anchor follows scroll</title>
<style>
.list {
height: 200px;
width: 260px;
overflow-y: auto;
border: 1px solid #888;
margin: 60px;
}
.row {
height: 40px;
padding: 0 12px;
line-height: 40px;
}
.row.selected {
anchor-name: --selected;
background: #fe9;
}
.note {
position: absolute;
position-anchor: --selected;
position-area: right;
margin-left: 8px;
padding: 4px 8px;
background: #222;
color: #fff;
}
</style>
</head>
<body>
<div class="list">
<div class="row">Row 1</div>
<div class="row">Row 2</div>
<div class="row">Row 3</div>
<div class="row">Row 4</div>
<div class="row">Row 5</div>
<div class="row">Row 6</div>
<div class="row">Row 7</div>
<div class="row selected">Row 8 (selected)</div>
<div class="row">Row 9</div>
<div class="row">Row 10</div>
<div class="row">Row 11</div>
<div class="row">Row 12</div>
</div>
<div class="note">Selected</div>
</body>
</html>Open the file in a browser that supports anchor positioning and scroll the list. The note moves up and down with Row 8. Scroll far enough and Row 8 disappears under the top edge of the list. With the default settings, the note disappears too. The next section explains why.
Hiding the element: position-visibility
Scroll compensation creates a visible problem. The anchored element is not a descendant of the scroller, so the scroller's overflow does not clip it. If the anchor scrolls out of view, a naive implementation would leave the note floating over empty space or over other content.
The position-visibility property controls this. It sets whether the browser hides the anchored element based on the state of the anchor or of the element's own placement.
| Value | The element is hidden when |
|---|---|
| `always` | Never. The element is shown regardless of the anchor. |
| `anchors-visible` | The anchor is completely clipped, for example scrolled out of view inside an intervening scroll container, or otherwise hidden. |
| `no-overflow` | The element would overflow its containing block, even after all fallbacks have been tried. |
The specification names anchors-visible as the initial value, so the example above hides the note when Row 8 is scrolled out of the list. Browsers have shipped this feature at different times, so test the behavior in each browser you target. To remove the safety net, or to choose a different one, set the property yourself.
/* Keep a persistent label even when the anchor is clipped */
.note {
position-visibility: always;
}
/* Hide the element instead of letting it overflow the page */
.menu {
position-visibility: no-overflow;
}The two non-default values answer different questions.
anchors-visibleasks whether the thing you are pointing at can still be seen. It suits tooltips, popovers, and highlights, which are meaningless once their target is gone.no-overflowasks whether the element still fits. It suits menus that must remain fully readable. Use it together with fallbacks, because it only hides the element after the browser has run out of placements to try.
The hiding behaves like visibility: hidden. The element is still laid out and still takes part in the cascade. It is only not painted and not interactive. It does not become display: none, so you cannot use this property to remove it from the accessibility tree reliably. Treat it as a visual feature.
Fallbacks and scrolling
position-try-fallbacks chooses among placements when the preferred one does not fit. Scrolling adds a question. If the user scrolls and the anchor moves toward an edge of the viewport, does the browser switch to a different fallback?
Browser behavior here is less settled than the basic compensation. In general, the browser picks a fallback when it lays out the element, and the pick can change when scrolling alters the space around the anchor. Do not write CSS that depends on one exact switching moment. Instead:
- Make the primary placement and each fallback correct on their own, so any one of them looks acceptable.
- Add
position-visibilityas a last resort, so the element hides instead of looking broken. - Test by scrolling slowly in both directions near the viewport edges, because a flip that occurs only in one direction is a common bug.
Common errors
- The element does not move when the inner box scrolls. Check that the element is not a child of the scroller with a different containing block, and that
position-anchoror an explicitanchor()name matchesanchor-name. A mismatched name leaves the element unanchored, so nothing tracks anything. - The element sticks around after the anchor leaves. Something set
position-visibility: always, or the browser does not yet implement the property. Set it explicitly and check support. - A clipped tooltip. If the tooltip is a child of the scroller, the scroller's
overflowclips it, as with any child. Move the tooltip outside the scroller, or put it in the top layer with the Popover API, if you want it to escape. - Jumps at the edge of the viewport. A fallback switched while the user scrolled. Review each fallback and consider
position-visibility: no-overflow.
Choosing a setup
| Situation | Suggested approach |
|---|---|
| Tooltip on an item inside a scrolling list | Place the tooltip outside the list and keep the default `anchors-visible`. |
| Label that must remain on screen | Set `position-visibility: always` and make sure the placement still makes sense. |
| Dropdown near the bottom of the page | Use `position-try-fallbacks` with `flip-block`, then `position-visibility: no-overflow`. |
| Tooltip inside the scroller | Let it scroll with the content. No compensation is involved, and the scroller clips it. |
Exercise
In the runnable example, the note is a sibling of the list rather than a child of it. What changes if you move the note inside the list, as the last child of .list, and why?
Show answer
The note's containing block would now be inside the scroll container, or the scroller itself if it is positioned. It would scroll with the list content naturally, so no scroll compensation is needed. The scroller's overflow would also clip it, so it would no longer escape the list's edges.
Exercise
You build a menu anchored to a button. When the user scrolls so that the menu would extend past the bottom of the viewport, and no fallback placement fits either, you want the menu to disappear rather than be cut off. Which position-visibility value do you use, and why not anchors-visible?
Show answer
Use no-overflow. It hides the element when the element would overflow its containing block even after all fallbacks have been tried. anchors-visible only looks at whether the anchor is visible, so the menu would still show, cut off, as long as the button is on screen.