Sub topic 9
Hiding anchored elements with position-visibility
This chapter assumes you know how position: absolute and inset properties work, and that you have declared an anchor with anchor-name and linked an element to it with position-anchor. It also assumes you have seen position-try-fallbacks, which moves an element when it would overflow.
The problem: anchored elements that outlive their anchor's visibility
An anchored element follows its anchor as the page scrolls. That is usually what you want. But the anchor can scroll out of view inside a scroll container while the anchored element stays visible. The element then floats over unrelated content, detached from the thing it describes.
Fallbacks do not solve this. They change the placement, but the element is still rendered. You need a way to say "if this element has lost its anchor or has nowhere to go, do not show it."
The position-visibility property
position-visibility controls whether the browser renders an anchored element, based on how it is positioned. It does not change layout. The element still takes part in layout as before. When it is hidden, it behaves as if visibility: hidden applied, so it is not painted and cannot be clicked.
| Value | Effect |
|---|---|
| `always` | Always visible. This is the default. |
| `anchors-visible` | Hidden if the anchor is fully clipped by its scroll container or by other content, so nothing of it can be seen. |
| `no-overflow` | Hidden if the element would overflow its containing block after fallbacks are tried. |
You can combine anchors-visible and no-overflow by listing both, separated by a space.
Hiding a tooltip when its anchor scrolls away
Suppose a tooltip is attached to a button inside a scrolling panel. When the button scrolls out of the panel, the tooltip should disappear with it.
<div class="panel">
<p>Scroll the panel to move the button out of view.</p>
<button class="trigger">Hover target</button>
<div class="tooltip">Helpful hint</div>
<p style="height: 600px">More content below.</p>
</div>.panel {
height: 200px;
overflow: auto;
position: relative;
}
.trigger {
anchor-name: --tip;
}
.tooltip {
position: absolute;
position-anchor: --tip;
position-area: top;
margin-bottom: 8px;
position-visibility: anchors-visible;
}With anchors-visible, the tooltip is hidden once the button is scrolled out of the panel's visible area. Without it, the tooltip would stay painted at its last reachable position.
Hiding an element that cannot fit
Sometimes no fallback placement fits. Showing a clipped or overlapping tooltip can be worse than showing none. Use no-overflow for that case.
@position-try --below {
position-area: bottom;
margin-top: 8px;
}
.tooltip {
position: absolute;
position-anchor: --tip;
position-area: top;
margin-bottom: 8px;
position-try-fallbacks: --below, flip-inline;
position-visibility: no-overflow;
}The browser first tries the top placement, then --below, then the flipped version. If every option overflows the containing block, no-overflow hides the element. If any option fits, the element is shown there.
Common errors
- Expecting it to hide content that was never anchored. The property applies to elements positioned with
position: absoluteorfixedthat use anchor positioning. On other elements it does nothing useful. - Using it for state changes. Do not use
position-visibilityto open and close a menu. Use thepopoverattribute or a class for that. This property reacts to geometry, not to user intent. - Hiding something the user needs. If the element holds essential information, hiding it on overflow loses that information. Prefer fallbacks first and treat hiding as the last resort. A hidden element is also removed from view for assistive technology, so do not hide critical messages this way.
- Forgetting that the default is
always. Without this property, nothing is hidden automatically.
Exercise
A tooltip is anchored to a button inside a scrolling panel, and you have defined fallbacks with position-try-fallbacks. You want the tooltip hidden when the button scrolls out of the panel, and also hidden when no fallback placement fits. Which declaration do you write?
Show answer
position-visibility: anchors-visible no-overflow;. The anchors-visible keyword hides the tooltip when the anchor is scrolled out of view. The no-overflow keyword hides it when it would overflow its containing block after all fallbacks have been tried.