Sub topic 8
Picking the best fallback with position-try-order
This chapter assumes you know how position-anchor, position-area, and position-try-fallbacks work. It also assumes you know what an inset-modified containing block is. By the end you will be able to use position-try-order to choose a fallback by how much room it gives, instead of by list order.
The problem: first fit is not always best fit
position-try-fallbacks takes a list of placements. The browser tries the element's normal placement first. If that overflows its containing block, the browser walks the list in order and uses the first option that fits.
That rule is simple, but it can pick a poor placement. Suppose a tooltip prefers to open above its anchor, with --below listed as a fallback. If the tooltip is 120px tall and there are only 130px of room below, the first fit is --below. The tooltip fits, but only just. Meanwhile there may be 400px of room to the left or right.
position-try-order changes how the browser chooses. Instead of "first option that fits", it can mean "the option that leaves the most space in a given dimension".
The syntax
position-try-order: normal | most-width | most-height | most-block-size | most-inline-size;normalis the default. The browser uses the order of theposition-try-fallbackslist.most-widthpicks the option whose available area is widest.most-heightpicks the option whose available area is tallest.most-inline-sizeandmost-block-sizedo the same in logical terms. They follow the writing mode, so they are the better choice for sites that support vertical or right-to-left text.
The property sets how the options are ranked. The list in position-try-fallbacks still decides which options exist.
How the ranking works
For each candidate placement, including the element's normal placement, the browser works out the area available to the element. This is the inset-modified containing block for that placement. The browser then compares the candidates by the dimension you named and applies the one with the largest value.
This has a few consequences.
- The ranking looks at available space, not at the size of the element. An option can win even when the element would fit comfortably in another one.
- The normal placement takes part in the ranking. It is not automatically preferred. If a fallback has more room, the fallback wins.
- Only candidates that you list, plus the base placement, compete. The browser does not invent new ones.
- If
position-try-orderis notnormal, the order of the list matters much less. It matters only to break ties.
A worked example: a tooltip that picks the roomier side
Here is a tooltip that normally opens above its trigger. If that is cramped, it may open below, to the left, or to the right. We want the side with the most height available, since a tooltip with a long message grows taller.
<button id="help" class="trigger">Help</button>
<div class="tooltip" id="help-tip">
This setting controls how often your data is synced. Lower values use more
battery but keep your devices closer together.
</div>.trigger {
anchor-name: --help;
}
.tooltip {
position: absolute;
position-anchor: --help;
position-area: top;
margin: 8px;
max-width: 24ch;
position-try-fallbacks: bottom, left, right;
position-try-order: most-height;
}When position-try-order is most-height, the browser compares the height of the area available to each of the four placements: top, bottom, left, and right. Left and right areas span the full height of the containing block, so they usually win when the trigger sits near a vertical edge. For a trigger in the middle of the page, top or bottom may win, depending on where it is.
This is not what normal would have produced. With normal, the tooltip would stay on top whenever it fit, even by one pixel.
Choosing between `most-width` and `most-height`
Pick the dimension that your content is most likely to run out of.
| Content | Likely constraint | Suggested value |
|---|---|---|
| Tall menu or list that scrolls | Height | `most-height` or `most-block-size` |
| Wide table or toolbar in a popover | Width | `most-width` or `most-inline-size` |
| Short tooltip with wrapping text | Either, but height shrinks first | `most-height` |
| Select-like dropdown | Height | `most-block-size` |
Use the logical values (most-inline-size, most-block-size) when the page may be translated into a language with vertical writing, such as traditional Japanese layouts. The physical values (most-width, most-height) always refer to the screen axes.
Combining it with custom fallbacks
position-try-order works with @position-try rules as well. Each named rule defines a placement and may also change margins or size. The available area is measured after the rule's declarations are applied.
@position-try --side-end {
position-area: right;
margin: 0 0 0 8px;
}
@position-try --side-start {
position-area: left;
margin: 0 8px 0 0;
}
.panel {
position: absolute;
position-anchor: --help;
position-area: bottom;
margin-top: 8px;
position-try-fallbacks: --side-end, --side-start;
position-try-order: most-width;
}Here the panel starts below the trigger. The browser compares the width available for the base placement and for the two side placements. If the trigger is near the left edge, the right-hand side has more width, so --side-end wins.
The shorthand
position-try is a shorthand that sets both properties together. The order value comes first and is optional.
.tooltip {
position-try: most-height bottom, left, right;
}This is equivalent to setting position-try-order: most-height and position-try-fallbacks: bottom, left, right. Be careful: using the shorthand resets the order to normal if you leave it out. A later position-try: flip-block; will undo a position-try-order you set earlier in the cascade. When you only want to change one of the two, use the longhand.
Edge cases and common errors
- Nothing fits. If no candidate fits,
position-try-orderdoes not rescue the element. The browser falls back to the base placement, as it does withnormal. Ranking only chooses among options. It does not invent room. - Ties. When two candidates offer exactly the same space, the browser uses list order. The base placement comes first.
- Order has no visible effect. This usually means the base placement already has the most space, or the list has only one entry. Add a second option and test with a trigger near an edge.
- Mixing axes. Ranking by
most-widthon a placement list made only of top and bottom options gives similar widths for all of them, so the result looks arbitrary. Choose a dimension that differs between your options. - Overflow still visible.
position-try-orderpicks a placement. It does not resize the element. Combine it withmax-height,overflow: auto, oranchor-size()limits if content may exceed the area. - Unsupported browsers. Browsers that do not know the property ignore it and use list order. Treat it as an enhancement. Your layout should still be usable with
normal.
Testing your choice
You can verify the behavior without scrolling the whole page by moving the trigger around a fixed-size container.
.stage {
position: relative;
height: 400px;
border: 1px dashed gray;
overflow: auto;
}
.trigger {
position: absolute;
anchor-name: --help;
top: 20px;
left: 20px;
}Change the trigger's top and left between a few values, such as 20px, 180px, and 360px. Watch where the tooltip lands with normal, then with most-height. Also open the browser developer tools and inspect the element. Current Chrome and Edge builds show the applied try option in the element's styles.
Exercise
A tooltip has position-area: top and position-try-fallbacks: bottom, left, right. It sits near the top of the page, with 60px above it and 300px below. The tooltip needs 80px of height. With position-try-order: normal, where does it go, and what changes with position-try-order: most-height?
Show answer
With normal, the top placement does not fit (60px is less than 80px), so the browser tries the list in order and uses bottom, the first that fits. With most-height, the browser compares the available height of all placements, including the base. Left and right areas span the full height of the containing block, so one of them likely has more height than the 300px below, and it wins. The exact winner depends on the containing block, but the choice is made by space, not by list order.
Exercise
You set position-try-order: most-width, then later in the same rule write position-try: flip-block;. The order seems to be ignored. Why, and how do you fix it?
Show answer
The position-try shorthand resets position-try-order to normal when the order is omitted. Because it appears later, it overrides the earlier longhand. Fix it by using position-try-fallbacks: flip-block; instead of the shorthand, or by including the order in the shorthand: position-try: most-width flip-block;.