Sub topic 6
Staying in view with position-try-fallbacks
An anchored tooltip or menu works until the page scrolls or the window shrinks. Then the element can run off the edge of the screen. This chapter covers position-try-fallbacks, the property that lets the browser pick another placement when the first one does not fit.
This chapter assumes you know the following:
- How
position: absoluteand containing blocks work. - How to declare an anchor with
anchor-nameand link an element withposition-anchor. - How
position-areaplaces an element on the anchor's 3x3 grid.
The problem: a placement that does not fit
Suppose a tooltip sits above its trigger with position-area: top. If the trigger is near the top of the viewport, the tooltip has no room above it. Without a fallback, it is clipped or pushed off screen.
Before anchor positioning, you would measure the element in JavaScript and move it. With position-try-fallbacks, you list alternative placements in CSS. The browser runs the check each time layout changes, including during scrolling and resizing.
How the browser chooses
The browser works through a list of position options in order:
- The base placement, which is the element's own
position-area, insets, and margins. - Each entry in
position-try-fallbacks, from left to right.
For each option, it checks whether the element fits inside its containing block. For an absolutely positioned element, that is the containing block you already know. For a popover or a fixed element, it is the viewport. The first option that fits wins. If no option fits, the browser keeps the base placement.
The check is not a one-time decision. The browser repeats it whenever the anchor, the element, or the containing block changes size or position. A tooltip can switch from above to below as the user scrolls, and switch back again.
Fallback values
position-try-fallbacks takes a comma-separated list. Each entry can be one of three kinds.
| Kind | Example | What it does |
|---|---|---|
| Try tactic | `flip-block` | Mirrors the base placement along an axis |
| Custom option | `--below` | Applies the declarations from an `@position-try` rule |
| Bare `position-area` value | `bottom` | Replaces the `position-area` for that attempt |
The default value is none, which means no fallbacks.
Try tactics
A try tactic transforms the base placement without you writing new values.
flip-blockswaps the block-axis sides. In horizontal text, top becomes bottom.flip-inlineswaps the inline-axis sides. In left-to-right text, left becomes right.flip-startswaps the start sides with each other, so a placement on the top-left diagonal moves to the left-top diagonal.
You can combine tactics in one entry, such as flip-block flip-inline, to mirror along both axes. Separate entries with commas when you want them tried one after another.
Here is a complete example. The dropdown opens below its button and flips above it when there is no room.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Flip fallback</title>
<style>
body { margin: 0; min-height: 300vh; font-family: sans-serif; }
#menu-btn {
anchor-name: --menu-btn;
position: fixed;
top: 40%;
left: 2rem;
}
#menu {
position: fixed;
position-anchor: --menu-btn;
position-area: bottom span-right;
margin: 4px 0;
width: max-content;
height: 12rem;
background: #eef;
position-try-fallbacks: flip-block;
}
</style>
</head>
<body>
<button id="menu-btn">Menu</button>
<div id="menu">Menu content</div>
</body>
</html>Resize the window so less than 12rem of space remains below the button. The menu jumps above the button.
Margins follow the flip
The margin 4px 0 is symmetric, so it behaves the same after a flip. When you use an asymmetric margin such as margin-bottom: 8px, the tactic flips it too. A flip-block turns it into margin-top: 8px. This is why tactics are usually enough for simple tooltips: the gap between the tooltip and its anchor follows the placement.
Bare position-area values
You can list a position-area value directly. It replaces the base position-area for that attempt only.
.tooltip {
position: absolute;
position-anchor: --tip;
position-area: top;
position-try-fallbacks: bottom, right, left;
}This tries top, then bottom, then right, then left. Use this form when the order of attempts matters more than mirroring.
Writing custom fallbacks with @position-try
A tactic only mirrors. When the fallback needs different margins, sizes, or a different area, define it with @position-try. The rule has a name starting with two dashes, and you refer to that name in the list.
@position-try --below {
position-area: bottom;
margin: 8px 0 0;
}
.tooltip {
position: absolute;
position-anchor: --tip;
position-area: top;
margin-bottom: 8px;
position-try-fallbacks: --below, flip-inline;
}The browser tries the base placement (top), then --below, then the base placement mirrored left to right.
Three rules govern what happens inside a custom option.
- Only the properties you list are overridden. Everything else keeps the element's normal value. This is why
--belowsetsmargin: 8px 0 0. The shorthand replaces all four margins, so the basemargin-bottom: 8pxdoes not linger. - Only some properties are allowed. These are the inset properties, margin properties, sizing properties,
align-self,justify-self,position-anchor, andposition-area. Other properties, such ascolor, are ignored inside the rule. - Names must be dashed identifiers, like
--below. The name is a custom identifier, in the same style as custom properties.
Changing size in a fallback
A fallback can change dimensions, which is useful when the element is simply too big for one side.
@position-try --narrow-left {
position-area: left;
max-width: 14rem;
margin: 0 8px 0 0;
}When the placement uses a different area, the element's available space changes. A fixed width that fit above the anchor may not fit beside it. Giving a fallback its own size is often the cleanest answer.
Ordering fallbacks well
The list is an ordered preference, not a set. Put the placements you want most first, and end with the safest one. Some practical patterns follow.
- Tooltips:
flip-block, flip-inlinecovers most layouts. The element prefers one side, then the opposite side, then the opposite corner. - Dropdown menus:
flip-blockalone is often enough, because users expect the menu to stay aligned with the button's left edge. - Side panels: a list of bare values such as
right, left, bottomkeeps the order explicit.
If you need the browser to pick by available space instead of list order, the position-try-order property can rank the options. It accepts values such as most-width and most-height. A later chapter covers it.
What counts as "fits"
The overflow check compares the element with the containing block, adjusted by the element's inset values. Three consequences follow.
- Fitting means the whole margin box is inside. A large margin can make an option fail even when the visible box fits.
- The element's size matters. An element with
width: max-contentand a long label may fit nowhere beside the anchor, so aleftorrightfallback keeps failing. Check the size first. - The containing block of an absolutely positioned element is its positioned ancestor, not the viewport. If that ancestor is small or clipped, the browser flips because of the ancestor, not the screen. Move the element to a fixed context or a popover if you want the viewport to decide.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Fallback never applies | The list is empty or has a typo in the `--name` | Check the name matches the `@position-try` rule exactly |
| Element stays clipped | No option fits, so the base placement is kept | Add a smaller or differently sized fallback |
| Gap changes after a flip | Margin was set only on one side | Use a symmetric margin or set it in the fallback |
| Flips because of a parent, not the screen | Containing block is a small positioned ancestor | Use `position: fixed` or a popover |
| Property ignored in `@position-try` | The property is not allowed in the rule | Move it to the element's normal style |
Putting it together
The next example combines a base placement, a custom option, and a tactic. The tooltip prefers the top, then a custom bottom placement with a narrower width, then the mirror of the base placement.
.trigger {
anchor-name: --tip;
}
@position-try --below-narrow {
position-area: bottom;
max-width: 16rem;
margin: 8px 0 0;
}
.tooltip {
position: absolute;
position-anchor: --tip;
position-area: top;
margin-bottom: 8px;
max-width: 24rem;
position-try-fallbacks: --below-narrow, flip-inline;
}Notice that --below-narrow sets max-width, which overrides the base 24rem only while that option is active. When the browser returns to the base placement, the original value applies again.
Exercise
A tooltip uses position-area: top; margin-bottom: 8px; position-try-fallbacks: flip-block;. The trigger is at the very top of the viewport. Where does the tooltip appear, and what happens to the margin?
Show answer
The tooltip appears below the trigger. The flip-block tactic mirrors the placement along the block axis, so the area becomes bottom and the 8px margin moves from the bottom side to the top side, keeping the gap between the tooltip and the trigger.
Exercise
You write @position-try --side { position-area: right; color: red; } and list --side as a fallback. Why is the text never red, and does the right placement still work?
Show answer
color is not one of the properties allowed inside @position-try, so the browser ignores it. The position-area: right declaration is allowed, so the placement still works. Set the color in the element's normal style instead.