Sub topic 7
Writing custom fallbacks with @position-try
This chapter assumes you know position: absolute, inset properties, anchor-name and position-anchor, the anchor() function, and position-area. You should also know that position-try-fallbacks accepts keywords such as flip-block and flip-inline. Here you will learn how to define your own fallback placements with @position-try.
Why keywords are not always enough
The built-in fallbacks flip a placement across an axis. They work well when the alternative is a mirror image of the original. Many real layouts need something else:
- A tooltip that sits above its trigger should move below it and also get a smaller offset.
- A dropdown that opens to the right should fall back to a different
position-areaand shrink its width. - A popover that cannot fit anywhere should anchor to the viewport edge instead.
A custom fallback is a named set of declarations. When the browser tries it, those declarations replace the matching ones on the element.
The @position-try rule
An @position-try rule has a name that starts with two dashes, like a custom property. Its body holds a limited list of properties.
@position-try --below {
position-area: bottom;
margin-top: 8px;
margin-bottom: 0;
}You then list the name in position-try-fallbacks:
.tooltip {
position: absolute;
position-anchor: --tip;
position-area: top;
margin-bottom: 8px;
position-try-fallbacks: --below;
}The tooltip is first laid out with its normal styles. If it overflows its containing block, the browser applies the declarations from --below and checks again.
Properties allowed inside the rule
Only properties that affect placement and size are valid inside @position-try. Others are ignored.
| Group | Properties |
|---|---|
| Inset | `top`, `right`, `bottom`, `left`, `inset`, `inset-block`, `inset-inline` and their longhands |
| Margin | `margin` and its logical and physical longhands |
| Sizing | `width`, `height`, `min-*`, `max-*`, `block-size`, `inline-size` |
| Alignment | `align-self`, `justify-self`, `place-self`, `align-items`, `justify-items` |
| Anchoring | `position-anchor`, `position-area` |
Properties such as color or background do nothing here. A fallback changes where and how big the element is. It does not change how the element looks.
How a fallback is applied
The browser follows these steps each time it lays out the element:
- It lays out the element with its normal styles.
- If the element overflows its inset-modified containing block, it moves to the next entry in
position-try-fallbacks. - It applies that entry's declarations over the normal styles and lays out again.
- It uses the first entry that does not overflow.
- If none fit, it uses the first layout, with no fallback applied.
Step 5 surprises many people. A failed search does not leave the element in the last fallback it tried. It returns to the original placement.
Fallbacks replace, they do not merge
A fallback overrides only the properties it names. Everything else on the element still applies. That is the source of the most common bug.
Suppose the original placement uses anchor() insets:
.tooltip {
position: absolute;
position-anchor: --tip;
bottom: anchor(top);
left: anchor(center);
translate: -50% 0;
margin-bottom: 8px;
position-try-fallbacks: --below;
}
@position-try --below {
bottom: auto;
top: anchor(bottom);
margin-bottom: 0;
margin-top: 8px;
}Look at what the fallback must do. The element has bottom: anchor(top). If the fallback only sets top: anchor(bottom), the element would then have both top and bottom set, and its height would stretch between them. Setting bottom: auto removes the old constraint. The same applies to margins: the old margin-bottom: 8px stays unless you reset it.
The translate property is not allowed in @position-try, so it stays at -50% 0. That works here because both placements are horizontally centered.
Mixing insets and position-area
You can switch an element from inset-based placement to position-area in a fallback, but resolve the conflicts yourself. A simpler approach is to use position-area in both the original and the fallbacks, since each value fully describes a placement.
@position-try --below {
position-area: bottom;
margin: 8px 0 0;
}
@position-try --right {
position-area: right;
margin: 0 0 0 8px;
}
.tooltip {
position: absolute;
position-anchor: --tip;
position-area: top;
margin: 0 0 8px;
position-try-fallbacks: --below, --right;
}The margin shorthand in each rule sets all four sides. That avoids leftover margins from the original rule.
Combining custom and built-in fallbacks
position-try-fallbacks takes a comma-separated list. Custom names and keywords can be mixed, and the browser tries them in order.
@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;
}The order is: original, then --below, then the original flipped horizontally. Put your most preferred alternatives first.
Combining keywords in a single entry
One entry may combine several keywords separated by spaces, such as flip-block flip-inline. That entry flips on both axes at once and counts as one try.
.menu {
position-try-fallbacks: flip-block, flip-inline, flip-block flip-inline;
}Custom names can also follow a keyword in the same entry. For example, flip-inline --compact applies the flip and then the declarations from --compact. Use this when you want a fallback that is a mirror image with one adjustment.
Tactics: the `try-tactic` combinations
The keywords flip-block, flip-inline and flip-start are called try tactics. flip-start swaps the start-side insets, which turns a placement into its diagonal mirror. Because tactics act on the element's current placement, they work with both position-area and inset values without any rule you write yourself.
Fallbacks that change size
Overflow often happens because the element is too big, not because it is in the wrong place. A fallback can change size at the same time as position.
@position-try --narrow-below {
position-area: bottom;
margin-top: 8px;
max-width: 16rem;
}
.card {
position: absolute;
position-anchor: --trigger;
position-area: right;
margin-left: 8px;
width: 24rem;
position-try-fallbacks: flip-inline, --narrow-below;
}The card prefers the right side at 24rem wide. If that fails, it tries the left side. If that fails too, it moves below the trigger and caps its width at 16rem. Because max-width in the fallback wins over the 24rem width, the card shrinks.
Order matters: position-try-order
By default the browser takes the first fallback that fits. The position-try-order property changes that. Set it to most-height, most-width, most-block-size or most-inline-size, and the browser compares every option and picks the one that gives the element the most room in that dimension.
.menu {
position-try-fallbacks: --above, --below;
position-try-order: most-height;
}This is useful for menus with variable content, where the side with more space is the better choice even if the first side technically fits.
Debugging custom fallbacks
When a fallback does not behave, check these causes in order:
- The name does not match.
--belowand--Beloware different names, and a misspelled name is silently skipped. - A property is not allowed. Declarations outside the allowed list are dropped.
- A leftover constraint remains. Reset opposing insets with
autoand reset margins you do not want. - The original is not actually overflowing. The browser only tries fallbacks when the element overflows its containing block. Test by shrinking the window or scrolling the anchor near an edge.
- The rule is declared after use. This is fine. Order between
@position-tryand the rules that use it does not matter. If a rule appears inside a cascade layer, however, the name resolves according to layer order.
A complete example
This example combines an anchor, a custom fallback and a built-in fallback. Open it in a browser that supports anchor positioning, then scroll the page so the button sits near the top edge.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Tooltip with custom fallbacks</title>
<style>
body { margin: 0; padding: 4rem 2rem; font-family: sans-serif; }
.trigger {
anchor-name: --tip;
padding: 0.5rem 1rem;
}
@position-try --below {
position-area: bottom;
margin: 8px 0 0;
}
.tooltip {
position: fixed;
position-anchor: --tip;
position-area: top;
margin: 0 0 8px;
padding: 0.5rem;
background: #222;
color: #fff;
position-try-fallbacks: --below, flip-inline;
}
</style>
</head>
<body>
<button class="trigger">Hover target</button>
<div class="tooltip">Helpful tip</div>
</body>
</html>The tooltip uses position: fixed, so its containing block is the viewport. When the button is near the top of the window, the space above is too small. The browser then applies --below and the tooltip appears under the button.
Exercise
A tooltip has bottom: anchor(top) and margin-bottom: 8px. Its fallback --below contains only top: anchor(bottom). The tooltip stretches tall when the fallback is used. Why, and what two declarations would you add to the fallback?
Show answer
The original bottom inset and margin-bottom still apply, because a fallback overrides only the properties it names. With both top and bottom set, the height is stretched between them. Add bottom: auto to remove the old constraint, and margin-bottom: 0 (plus margin-top: 8px for the new gap) to clear the old margin.
Exercise
A position-try-fallbacks list is --a, --b, --c, and the element overflows with all three. Which layout does the browser use?
Show answer
The original layout, with no fallback applied. The browser does not stay in the last fallback it tried.
Exercise
Write a fallback list that tries a custom --above placement first, then the original flipped on both axes, and picks whichever option gives the element the most height.
Show answer
position-try-fallbacks: --above, flip-block flip-inline; together with position-try-order: most-height;. The browser compares the options by available height instead of taking the first that fits.