This demo shows the effect of position-try
.
HTML
The HTML includes two <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>
CSS
In the CSS, the anchor is given an anchor-name
and has a position
value of absolute
set on it. We position it in the top-half of the viewport using top
and left
values:
.anchor {
anchor-name: --myAnchor;
position: absolute;
top: 100px;
left: 45%;
}
We then include a custom position option — --custom-bottom
— which positions the element below the anchor and gives it an appropriate margin:
@position-try --custom-bottom {
top: anchor(bottom);
bottom: unset;
margin-top: 10px;
}
We initially position the element above its anchor, and then set a position-try
value on it that gives it a position-try-order
of most-height
, and a position-try-fallbacks
list that just includes our custom fallback option:
.infobox {
position: fixed;
position-anchor: --myAnchor;
bottom: anchor(top);
margin-bottom: 10px;
justify-self: anchor-center;
position-try: most-height --custom-bottom;
}
Result
The element appears below its anchor, even though it is initially positioned above it. This occurs because there is more vertical space below the anchor than there is above it. The most-height
try order causes the --custom-bottom
try fallback option to be applied, placing the positioned element in the position that gives its containing block the most height.