Sub topic 7
Writing custom fallbacks with @position-try
This chapter assumes you know position: absolute, inset properties, position-anchor, and position-area. It also assumes you have seen position-try-fallbacks with keywords such as flip-block.
Why built-in flips are not always enough
The keywords flip-block, flip-inline, and flip-start mirror the current placement. That covers many cases. Sometimes the alternative placement is not a mirror image. You may want a different position-area, a different margin, or a different width. For those cases you define your own fallback with @position-try.
What an @position-try rule is
An @position-try rule is a named set of position declarations. The name starts with two dashes, like a custom property. The browser applies the block only when it tries that fallback.
@position-try --below {
position-area: bottom;
margin-top: 8px;
}Defining the rule does nothing by itself. You must list its name in position-try-fallbacks on the anchored element.
.tooltip {
position: absolute;
position-anchor: --tip;
position-area: top;
margin-bottom: 8px;
position-try-fallbacks: --below, flip-inline;
}How the browser chooses
The browser first lays out the element with its normal styles. If the element overflows its containing block, the browser tries each fallback in the order listed. It uses the first one that fits. If none fits, it keeps the original placement.
In the list above, --below is a custom fallback and flip-inline is a built-in one. You can mix both kinds in any order.
What a fallback block may contain
The declarations in @position-try replace the ones on the element while that fallback is active. Only a limited set of properties is allowed:
- Inset properties such as
top,left,inset-block-start - Margin properties
- Sizing properties such as
width,min-width,max-height - Self-alignment properties such as
align-selfandjustify-self position-anchorposition-area
Other properties, such as color or padding, are ignored inside the rule. Declarations marked !important are also ignored.
Using inset values instead of position-area
A fallback can use anchor() just as the base styles do. This is useful when the base placement uses insets.
@position-try --left-of {
bottom: auto;
top: anchor(top);
right: anchor(left);
left: auto;
margin: 0 8px 0 0;
}
.tooltip {
position: absolute;
position-anchor: --tip;
bottom: anchor(top);
left: anchor(center);
translate: -50% 0;
position-try-fallbacks: --left-of;
}Reset any inset that the base styles set but the fallback does not use. Here bottom: auto and left: auto stop the base values from conflicting with top and right.
Common errors
- Typo in the name. If
position-try-fallbacksnames--blowbut the rule is--below, the fallback is ignored with no error. - Mixing placement methods. A fallback with
position-areacombined with base insets can behave unexpectedly. Keep the base and its fallbacks on the same method where you can. - Expecting margins to carry over. A base
margin-bottom: 8pxdoes not move to the opposite side. Set the margin you need inside each fallback. - No overflow, no fallback. If the base placement fits, no fallback runs. Test by shrinking the viewport or moving the anchor near an edge.
Exercise
A tooltip uses position-area: top and margin-bottom: 8px. You add @position-try --below { position-area: bottom; } and list it in position-try-fallbacks. When it flips below the anchor, there is no gap between the tooltip and the anchor. What is missing, and where do you fix it?
Show answer
The base margin applies only on the bottom edge, which does not help when the tooltip is below the anchor. Add margin-top: 8px; inside the @position-try --below block so the gap exists in that state.
Exercise
Why does a tooltip ignore color: red; written inside an @position-try rule?
Show answer
Only position-related properties (insets, margins, sizing, self-alignment, position-anchor, and position-area) are allowed in @position-try. Other properties are ignored.