Sub topic 6
Staying in view with position-try-fallbacks
This chapter assumes you know how position: absolute and inset properties work, and that you can declare an anchor with anchor-name, link an element with position-anchor, and place it with position-area. If those are new, review the earlier chapters first.
The problem: overflow
An anchored tooltip placed above its trigger works until the trigger sits near the top of the viewport. The tooltip then extends off-screen and gets clipped. Before anchor positioning, you fixed this with JavaScript that measured rectangles and recalculated coordinates.
position-try-fallbacks moves that decision into CSS. You give the browser a list of alternative placements. It uses your normal placement if the element fits. If not, it tries each alternative in order and keeps the first one that fits.
How the browser decides
"Fits" means the element's margin box stays inside its containing block. For an absolutely positioned element, that is usually the viewport or the nearest positioned ancestor. The browser checks the base placement first, then each fallback in the order listed. If none fit, it uses the base placement.
Built-in fallback keywords
The simplest fallbacks are keywords that flip the existing placement:
flip-blockmirrors it along the block axis (top becomes bottom in horizontal writing).flip-inlinemirrors it along the inline axis (left becomes right in left-to-right text).flip-startswaps the start-side properties, which is useful for corner placements.
You can combine them, for example flip-block flip-inline, to try the opposite corner.
.tooltip {
position: absolute;
position-anchor: --tip;
position-area: top;
margin-bottom: 8px;
position-try-fallbacks: flip-block;
}With this rule the tooltip appears above the anchor. If there is no room above, the browser flips it below. The margin-bottom flips with it and becomes margin-top, so the gap stays the same.
Custom fallbacks with `@position-try`
Keywords only mirror. When you want a specific alternative, define it with @position-try. The name must start with two dashes, like a custom property.
@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;
}Here the browser tries the base top placement, then --below, then a horizontally flipped version of the base placement. You can mix custom names and keywords in one list, separated by commas.
A @position-try block accepts only a limited set of properties: inset properties, margins, sizing properties, align-self, justify-self, position-anchor, and position-area. Other properties are ignored. A fallback's values replace the element's own values for those properties while the fallback is active.
Common errors
- Margins that only suit one side. A
margin-bottomfor a top placement does nothing useful after a manual switch to the bottom. Useflip-block, or set the margin inside your@position-tryblock. - Missing
position: absoluteorfixed. Fallbacks apply only to positioned anchored elements. - An element larger than the space on every side. No fallback fits, so the base placement stays. Constrain the size, for example with
max-height, or add a placement with more room. - Putting
@position-tryrules inside a selector. It is a top-level at-rule, like@keyframes. - Expecting a fallback to apply after you move it. The browser re-evaluates on scroll and resize. You do not need to trigger it.
Try it
Place a button close to the top edge of a page and give it anchor-name: --tip. Add a tooltip with the rules above. Scroll so the button is near the top, then near the bottom, and watch the tooltip switch sides.
Exercise
A tooltip has position-area: top and position-try-fallbacks: --below, flip-inline. The space above the anchor is too small. The space below is large enough, and --below uses position-area: bottom. Which placement does the browser use, and is flip-inline ever tried?
Show answer
The browser uses --below. It tries fallbacks in order and keeps the first one that fits, so flip-inline is never tried.
Exercise
Why is position-try-fallbacks: flip-block often better than a custom @position-try rule for a simple top-to-bottom switch?
Show answer
The keyword mirrors the whole base placement, including margins, so you write no extra rule and the spacing stays consistent. A custom rule is worth using when the alternative is not a mirror image of the base placement.