Sub topic 8
Picking the best fallback with position-try-order
This chapter assumes you can already declare an anchor with anchor-name, place an element with position-area, and list fallbacks with position-try-fallbacks. If any of that is unfamiliar, read the earlier chapters first.
The problem with "first one that fits"
By default, the browser tries the base placement first. If the element overflows its containing block, it walks through your position-try-fallbacks list in the order you wrote it. It uses the first option that fits.
That is fine for a small tooltip. It is a poor rule for a tall menu. Suppose a menu is placed below its button and does not fit. The first fallback is flip-block, which puts it above the button. That option might have even less room than the base placement. You cannot fix this by reordering the list, because the right choice depends on the viewport size and the scroll position at that moment.
position-try-order solves this. It tells the browser to sort the options by how much space each one offers, instead of by the order you listed them.
The values
| Value | Meaning |
|---|---|
| `normal` | Keep the written order. This is the default. |
| `most-width` | Prefer the option with the widest available area. |
| `most-height` | Prefer the option with the tallest available area. |
| `most-block-size` | Prefer the most space along the block axis. |
| `most-inline-size` | Prefer the most space along the inline axis. |
The two logical values, most-block-size and most-inline-size, follow the writing mode. In a horizontal language, block size is height and inline size is width.
How the browser applies it
When the value is not normal, the browser does the following:
- It collects the base placement and every entry in
position-try-fallbacks. - For each one, it measures the area available to the element. This is the size of the containing block that option produces, based on the anchor and the viewport.
- It sorts the options so the largest value in the chosen dimension comes first.
- It uses the first option in that sorted list that avoids overflow.
Because the base placement joins the sort, the browser may move away from it even when it would have fit. This happens when another option offers more room.
Worked example: a menu that opens where there is room
This menu opens below its button. When the space below is tight, it may move above the button, whichever side is taller.
<button id="menu-btn" popovertarget="menu">Menu</button>
<ul id="menu" popover>
<li>Profile</li>
<li>Settings</li>
<li>Billing</li>
<li>Help</li>
<li>Sign out</li>
</ul>#menu-btn {
anchor-name: --menu-btn;
}
#menu {
position-anchor: --menu-btn;
position-area: bottom span-right;
inset: auto;
margin: 0;
overflow: auto;
position-try-fallbacks: flip-block;
position-try-order: most-height;
}The browser compares two options: below the button, and above it. It measures the height of each area and puts the taller one first. If the button sits near the bottom of the viewport, the above placement wins. If it sits near the top, the base placement stays in front.
The overflow: auto line lets the menu scroll if neither side is tall enough for all the items.
The shorthand
position-try combines both properties. The order value comes first and is optional.
#menu {
position-try: most-height flip-block;
}This is equivalent to setting position-try-order: most-height and position-try-fallbacks: flip-block. The shorthand resets the order to normal if you omit it, so use it carefully when other rules already set position-try-order.
Common mistakes
- Expecting the list order to matter. With
most-height, the order of your fallbacks only breaks ties. Two options with equal space keep their written order. - Using the wrong dimension. A menu that opens above or below needs
most-height. A side panel that opens left or right needsmost-width. - Providing no fallbacks. With only the base placement, there is nothing to sort. The property has no visible effect.
- Forgetting that the base placement is a candidate. Your base
position-areais not guaranteed to be chosen, even when it fits.
Exercise
A side panel is anchored to a button and normally opens to the right with position-area: right. On narrow screens it should open on the left when that side is wider. Which declarations on the panel achieve this?
Show answer
Add position-try-fallbacks: flip-inline; and position-try-order: most-width;. The browser compares the width available on each side and tries the wider option first, so the panel moves to the left only when that side has more room.