To use custom position fallback options that aren't available via the above mechanisms, you can create your own with the @position-try
at-rule. The syntax is:
@position-try --try-fallback-name {
descriptor-list
}
The --try-fallback-name
is a developer-defined name for the position try fallback option. This name can then be specified within the comma-separated list of try fallback options within the position-try-fallbacks
property value. If multiple @position-try
rules have the same name, the last one in the document order overrides the others. Avoid using the same name for your try fallback options and your anchor or custom property names; it doesn't invalidate the at-rule, but it will make your CSS very difficult to follow.
The descriptor-list
defines the property values for that individual custom try fallback option, including how the positioned element should be placed and sized, and any margins. The limited list of property descriptors allowed includes:
The values you include in the at-rule get applied to the positioned element if the named custom-try fallback option gets applied. If any of the properties were previously set on the positioned element, those property values get overridden by the descriptor values. If the user scrolls, causing a different try fallback option or no try fallback option to be applied, the values from the previously-applied try fallback option are unset.
In this example, we set up and use several custom try fallback options. We use the same base HTML and CSS code as in the previous examples.
We start by defining four custom try fallback options using @position-try
:
@position-try --custom-left {
position-area: left;
width: 100px;
margin: 0 10px 0 0;
}
@position-try --custom-bottom {
position-area: bottom;
margin: 10px 0 0 0;
}
@position-try --custom-right {
position-area: right;
width: 100px;
margin: 0 0 0 10px;
}
@position-try --custom-bottom-right {
position-area: bottom right;
margin: 10px 0 0 10px;
}
Once our custom try fallback options are created, we can include them in the position list by referencing their names:
.infobox {
position: fixed;
position-anchor: --myAnchor;
position-area: top;
width: 200px;
margin: 0 0 10px 0;
position-try-fallbacks:
--custom-left, --custom-bottom,
--custom-right, --custom-bottom-right;
}
Note that our default position is defined by position-area: top
. When the infobox isn't overflowing the page in any direction, the infobox sits above the anchor, and the position-try fallback options set in the position-try-fallbacks
property are ignored. Also, note that the infobox has a fixed width and bottom margin set. These values will change as different position-try fallback options are applied.
If the infobox starts to overflow, the browser tries the position options listed in the position-try-fallbacks
property:
- The browser first tries the
--custom-left
fallback position. This moves the infobox to the left of the anchor, adjusts the margin to suit, and also gives the infobox a different width. - Next, the browser tries the
--custom-bottom
position. This moves the infobox to the bottom of the anchor, and sets an appropriate margin. It doesn't include a width
descriptor, so the infobox returns to its default width of 200px
set by the width
property. - The browser next tries the
--custom-right
position. This works much the same as the --custom-left
position, with the same width
descriptor value applied, but the position-area
and margin
values are mirrored to place the infobox appropriately to the right. - If none of the other fallbacks succeed in stopping the positioned element from overflowing, the browser tries the
--custom-bottom-right
position as a last resort. This works in much the same way as the other fallback options, but it places the positioned element to the bottom-right of the anchor.
If none of the fallbacks succeed in stopping the positioned element from overflowing, the position will revert to the initial position-area: top;
value.
Note: When a position try fallback option is applied, its values will override the default values set on the positioned element. For example, the default width
set on the positioned element is 200px
, but when the --custom-right
position try fallback option is applied, its width is set to 100px
.
Scroll the page and check out the effect of these position-try fallback options as the anchor nears the edge of the viewport: