Sub topic 9
Hiding anchored elements with position-visibility
This chapter covers position-visibility, the property that hides an anchored element when its position stops making sense. It assumes you can already declare an anchor with anchor-name, link an element with position-anchor, and place it with position-area or anchor(). It also assumes you know what a containing block is and how position-try-fallbacks works.
The problem: an anchored element with nowhere to go
An anchored element follows its anchor. The anchor can scroll out of view inside a scrollable container, or the element can be pushed off the screen after every fallback has been tried. In those cases the element can end up in a place that looks wrong. A tooltip may float over unrelated content, or a menu may hang at the edge of a panel while its button has scrolled away.
position-visibility lets the browser hide the element in these cases. It changes only visibility. Layout, the anchor link, and the computed position are not affected. The element is still positioned, and it is still in the DOM and the accessibility tree unless something else removes it.
The values
The property accepts these keywords.
| Value | Effect |
|---|---|
| `always` | The default. The element is always shown. |
| `anchors-visible` | The element is hidden if its anchor is completely hidden. Hiding comes from clipping by scrolling ancestors, or from the anchor being covered by other content in the same clipping setup. |
| `no-overflow` | The element is hidden if it overflows its containing block after all fallbacks have been tried. |
The values can be combined where the specification allows it. In practice you choose between anchors-visible and no-overflow, or you list both so that either condition hides the element.
Hiding when the anchor scrolls away
Take a table whose rows each hold a button. Each button is an anchor, and a tooltip is attached to the button. The table sits in a scrolling container.
<div class="scroller">
<button class="trigger">Row 1</button>
<p class="spacer">Lots of content</p>
<p class="spacer">Lots of content</p>
<p class="spacer">Lots of content</p>
</div>
<div class="tooltip">Shown above the trigger</div>.scroller {
height: 200px;
overflow: auto;
position: relative;
}
.trigger {
anchor-name: --tip;
}
.tooltip {
position: fixed;
position-anchor: --tip;
position-area: top;
margin-bottom: 8px;
position-visibility: anchors-visible;
}Without the last declaration, the tooltip stays in the viewport at the edge of the scroller after the button scrolls out. With anchors-visible, the tooltip disappears at the point where the button is no longer visible in the scroller. When the button scrolls back, the tooltip returns.
Here the tooltip is position: fixed and sits outside the scroller. The anchor can still be tracked because anchor positioning accounts for the scroll offset of the anchor's scrolling ancestors. Chapter topics on scroll tracking explain how this works. For this chapter, remember that the visibility check uses the same tracking.
Hiding when nothing fits
The no-overflow value deals with a different failure. Suppose a tooltip has fallbacks, and none of them fits.
.tooltip {
position: absolute;
position-anchor: --tip;
position-area: top;
margin-bottom: 8px;
position-try-fallbacks: bottom, left, right;
position-visibility: no-overflow;
}The browser tries top first. If that overflows the containing block, it tries bottom, then left, then right. If every option overflows, it uses the last option it evaluated or the original one, depending on the fallback algorithm. Without no-overflow, the element is then drawn in a position that is partly cut off. With no-overflow, the element is hidden. Hiding a tooltip is often better than showing a clipped one. For a menu with important actions it may be worse, so choose by what the element does.
How the two checks differ
The checks answer different questions.
anchors-visibleasks about the anchor. Is the thing I point to still visible?no-overflowasks about the anchored element. Can I be shown inside my containing block?
An element can pass one check and fail the other. A tooltip whose anchor is fully visible can still have no room. A tooltip with plenty of room can have an anchor that has scrolled out of view.
flowchart TDThe diagram below summarizes the decision.
What "hidden" means
When position-visibility hides an element, the browser treats it like visibility: hidden. It keeps its box but is not painted, and it cannot be hit by the pointer. Three consequences follow.
- Layout does not change, because the element is out of flow already when it is absolutely or fixed positioned.
- Transitions and animations still run on the hidden element. If you animate opacity on show, the animation can restart when the element reappears, depending on how you wrote it.
- Focus is a concern. A hidden element cannot receive focus. If a focused element becomes hidden, the browser behaves as it does for
visibility: hidden, and focus can be lost.
Interaction with popovers
A popover that is open lives in the top layer. Its anchor can still scroll out of view. In that case anchors-visible hides the popover while it remains open from the point of view of the Popover API. The :popover-open state stays on, and light dismiss and the Escape key still apply. When the anchor returns, the popover is painted again.
#menu {
position-anchor: --menu-btn;
position-area: bottom span-right;
inset: auto;
margin: 0;
position-try-fallbacks: flip-block;
position-visibility: anchors-visible;
}This is useful for a menu that belongs to a button inside a scroller. It avoids a menu detached from its trigger. Consider whether a user who scrolled away from a menu still wants it, because a hidden but open popover can be confusing. Some interfaces close the popover on scroll instead, using a small script, and that is a legitimate alternative.
Choosing a value
Use this guide.
- Pick
alwayswhen the element must remain available regardless of position, such as a toast with an anchor fallback. - Pick
anchors-visiblefor tooltips and hovercards that explain the anchor. If the anchor is not visible, the explanation has no subject. - Pick
no-overflowfor decorations where a clipped result is worse than nothing. - List both values when both failure modes matter.
Common mistakes
- Expecting
position-visibilityto remove the element from the accessibility tree in every case. Assistive technology treatment follows the browser's handling of hidden content, so test it. - Setting it on an element that is not anchored. It has no effect without a link to an anchor for
anchors-visible, and it needs a containing block forno-overflow. - Forgetting fallbacks.
no-overflowis evaluated after fallbacks, so addingposition-try-fallbacksfirst reduces how often the element hides. - Assuming it is available everywhere. Anchor positioning support varies by browser and version, so wrap the code in a feature check.
@supports (position-visibility: anchors-visible) {
.tooltip {
position-visibility: anchors-visible;
}
}Unsupported browsers ignore the unknown property. The tooltip then stays visible when its anchor scrolls away, which is acceptable as a baseline.
Exercise
A tooltip is anchored to a button inside a scrollable panel. The user scrolls until the button is completely out of view, and the tooltip is left floating at the panel edge. Which value of position-visibility fixes this, and what does it do when the user scrolls back?
Show answer
Use position-visibility: anchors-visible. The tooltip is hidden while the anchor is fully hidden, and it is painted again when the button scrolls back into view.
Exercise
You set position-try-fallbacks: bottom, left, right and position-visibility: no-overflow. Every placement overflows the containing block. What happens, and how does that differ from leaving position-visibility at its default?
Show answer
With no-overflow, the element is hidden once all fallbacks have been tried and it still overflows. With the default value, always, it is shown in a position where part of it is cut off.