files/en-us/web/css/reference/properties/position-try-fallbacks/index.md
The position-try-fallbacks CSS property enables you to specify a list of one or more alternative position try fallback options for anchor-positioned elements to be placed relative to their associated anchor elements. When the element would otherwise overflow its inset-modified containing block, the browser will try placing the positioned element in these different fallback positions, in the order provided, until it finds a value that stops it from overflowing its container or the viewport.
[!NOTE] The {{cssxref("position-try")}} shorthand property can be used to specify {{cssxref("position-try-order")}} and
position-try-fallbacksvalues in a single declaration.
[!NOTE] This property was originally named and supported in Chromium browsers as
position-try-options, with the same property values. Untilposition-try-fallbacksis supported, use the {{cssxref("position-try")}} shorthand instead.
/* Default value: no try fallback options */
position-try-fallbacks: none;
/* Single try option */
position-try-fallbacks: flip-block;
position-try-fallbacks: top;
position-try-fallbacks: --custom-try-option;
/* Multiple value combination option */
position-try-fallbacks: flip-block flip-inline;
/* Multiple values */
position-try-fallbacks: flip-block, flip-inline;
position-try-fallbacks: top, right, bottom;
position-try-fallbacks: --custom-try-option1, --custom-try-option2;
position-try-fallbacks:
flip-block,
flip-inline,
flip-block flip-inline;
position-try-fallbacks:
flip-block,
--custom-try-option,
--custom-try-option flip-inline,
right;
/* Global values */
position-try-fallbacks: inherit;
position-try-fallbacks: initial;
position-try-fallbacks: revert;
position-try-fallbacks: revert-layer;
position-try-fallbacks: unset;
The position-try-fallbacks property may be specified as either the keyword value none or as a comma-separated list of one or more space-separated custom position option names or <try-tactic>s or a position-area value.
none
<try-tactic>
flip-block
flip-inline
flip-start
start properties with each other, and the end properties with each other.dashed-ident. If no custom position option exists with that name, the option is ignored.[!NOTE] Multiple options can be specified, separated by commas.
Anchor-positioned elements should always appear in a convenient place for the user to interact with, if at all possible, regardless of where their anchor is positioned. To stop the positioned element from overflowing the viewport, it is often necessary to change its location when its anchor gets close to the edge of its containing element or the viewport.
This is achieved by providing one or more position-try fallback options in the position-try-fallbacks property. If the positioned element's initial position would overflow, the browser will try each fallback position option; the first fallback option that doesn't cause the element to overflow its containing block is applied. By default, the browser will try them in the order they appear in the list, applying the first one it finds that will stop the positioned element from overflowing.
If no option can be found that will place the positioned element completely on-screen, the browser will revert to displaying the positioned element at its default position before any try fallback options were applied.
[!NOTE] In some situations you might want to just hide overflowing positioned elements, which can be achieved using the {{cssxref("position-visibility")}} property. In most cases however it is better to keep them on-screen and usable.
For detailed information on anchor features and position try fallback usage, see the CSS anchor positioning module and the Fallback options and conditional hiding for overflow guide.
Referred to as a <try-tactic> in the specification, the predefined values move the positioned element by taking its computed position and transforming it across a particular axis of the anchor. The predefined values are:
flip-block
flip-inline
flip-start
A single position-try fallback option can include more than one <try-tactic> or dashed-ident options, or a combination of both by declaring them as a single space-separated option:
<try-tactic> options, their transformations are composed together.<try-tactic> and a <dashed-ident> named @position-try option, the custom position option is applied first, then the <try-tactic> transformation is applied.position-area values cannot be combined like this.
{{cssinfo}}
{{csssyntax}}
This example shows the basic usage of a couple of predefined <try-tactic> fallback options.
The HTML includes two {{htmlelement("div")}} elements that will become an anchor and an anchor-positioned element:
<div class="anchor">⚓︎</div>
<div class="infobox">
<p>This is an information box.</p>
</div>
We style the <body> element to be very large, to enable both horizontal and vertical scrolling.
The anchor is given an {{cssxref("anchor-name")}} and large margins to place it somewhere near the center of the visible section of the <body>:
.anchor {
font-size: 1.8rem;
color: white;
text-shadow: 1px 1px 1px black;
background-color: hsl(240 100% 75%);
width: fit-content;
border-radius: 10px;
border: 1px solid black;
padding: 3px;
}
body {
width: 1500px;
height: 500px;
}
.anchor {
anchor-name: --my-anchor;
margin: 100px 350px;
}
The infobox is given fixed positioning, a {{cssxref("position-anchor")}} property that references the anchor's anchor-name, to associate the two together, and it is tethered to the anchor's top-left corner using a position-area.
We include a position-try-fallbacks list (and re-declare it with the position-try shorthand in case the longhand property name is not yet supported), providing two predefined position-try fallback options to prevent it from overflowing when the anchor gets near the edge of the viewport, by flipping it along the inline or block axis of the anchor.
.infobox {
color: darkblue;
background-color: azure;
border: 1px solid #dddddd;
padding: 10px;
border-radius: 10px;
font-size: 1rem;
}
.infobox {
position: fixed;
position-anchor: --my-anchor;
position-area: top left;
position-try-fallbacks: flip-block, flip-inline;
position-try: flip-block, flip-inline;
}
This gives us the following result:
{{ EmbedLiveSample("Predefined try options", "100%", "250") }}
Try scrolling so the anchor nears the edges:
Depending on the browser, once the positioned element moves to the fallback position, it may remain in the fallback position even if the fallback positioning is no longer necessary, such as when the space allows it to return to the position defined by the {{cssxref("position-area")}}.
However, if you move the anchor towards the top-left corner of the viewport, you'll notice a problem — as the positioned element starts to overflow in the block and inline direction, it flips back to its default top-left position and overflows in both directions, which is not what we want.
This is because we only gave the browser position options of flip-block or flip-inline. We didn't give it the option of trying both at the same time. The next example will show you how to fix this issue.
Let's use a combined try fallback option to fix the problem we found with the previous demo.
All of the HTML and CSS in this demo is the same, except for the positioned element code. In this case, it is given a third position try fallback option: flip-block flip-inline:
<div class="anchor">⚓︎</div>
<div class="infobox">
<p>This is an information box.</p>
</div>
body {
width: 1500px;
height: 500px;
}
.anchor {
font-size: 1.8rem;
color: white;
text-shadow: 1px 1px 1px black;
background-color: hsl(240 100% 75%);
width: fit-content;
border-radius: 10px;
border: 1px solid black;
padding: 3px;
}
.anchor {
anchor-name: --my-anchor;
margin: 100px 350px;
}
.infobox {
color: darkblue;
background-color: azure;
border: 1px solid #dddddd;
padding: 10px;
border-radius: 10px;
font-size: 1rem;
}
.infobox {
position: fixed;
position-anchor: --my-anchor;
position-area: top left;
position-try:
flip-block,
flip-inline,
flip-block flip-inline;
position-try-fallbacks:
flip-block,
flip-inline,
flip-block flip-inline;
}
{{ EmbedLiveSample("Combining multiple values into one option", "100%", "250") }}
The third position-try fallback option means that the browser will try flip-block then flip-inline to avoid overflow, and if those fallbacks fail, it will combine the two, flipping the element's position in the block and inline directions at the same time. Now when you scroll the anchor towards the top and left edges of the viewport, the positioned element will flip over to the bottom-right.
position-area try fallback optionsThis example shows some position-area position-try fallback options in action.
All of the HTML and CSS in this demo is the same, except for the positioned element code. In this case, our position try fallback options are all position-area values — top, top right, right, bottom right, bottom, bottom left, and left.
This means that the positioned element will find a reasonable position to display in, whatever viewport edges the anchor is near. This approach is a bit more longwinded than the predefined values approach, but it is also more granular and flexible.
<div class="anchor">⚓︎</div>
<div class="infobox">
<p>This is an information box.</p>
</div>
body {
width: 1500px;
height: 500px;
}
.anchor {
font-size: 1.8rem;
color: white;
text-shadow: 1px 1px 1px black;
background-color: hsl(240 100% 75%);
width: fit-content;
border-radius: 10px;
border: 1px solid black;
padding: 3px;
}
.anchor {
anchor-name: --my-anchor;
margin: 100px 350px;
}
.infobox {
color: darkblue;
background-color: azure;
border: 1px solid #dddddd;
padding: 10px;
border-radius: 10px;
font-size: 1rem;
}
.infobox {
position: fixed;
position-anchor: --my-anchor;
position-area: top left;
position-try:
top, top right, right,
bottom right, bottom,
bottom left, left;
position-try-fallbacks:
top, top right, right,
bottom right, bottom,
bottom left, left;
}
{{ EmbedLiveSample("position-area try fallback options", "100%", "250") }}
Scroll the page and check out the effect of these position-try fallback options as the anchor nears the edge of the viewport.
See the {{cssxref("@position-try")}} reference page.
{{Specifications}}
{{Compat}}
<position-area> value