GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||
GeezerVison is showing developer.mozilla.org — position-try CSS property - CSS | MDN
|
| Initial value | as each of the properties of the shorthand:
|
|---|---|
| Applies to | absolutely positioned elements |
| Inherited | no |
| Percentages | as each of the properties of the shorthand: |
| Computed value | as each of the properties of the shorthand:
|
| Animation type | as each of the properties of the shorthand:
|
position-try =
<'position-try-order'>? <'position-try-fallbacks'>
<position-try-order> =
normal |
<try-size>
<position-try-fallbacks> =
none |
[ [ <dashed-ident> || <try-tactic> ] | <position-area> ]#
<try-size> =
most-width |
most-height |
most-block-size |
most-inline-size
<try-tactic> =
flip-block ||
flip-inline ||
flip-start ||
flip-x ||
flip-y
<position-area> =
[ left | center | right | span-left | span-right | x-start | x-end | span-x-start | span-x-end | self-x-start | self-x-end | span-self-x-start | span-self-x-end | span-all ] || [ top | center | bottom | span-top | span-bottom | y-start | y-end | span-y-start | span-y-end | self-y-start | self-y-end | span-self-y-start | span-self-y-end | span-all ] |
[ block-start | center | block-end | span-block-start | span-block-end | span-all ] || [ inline-start | center | inline-end | span-inline-start | span-inline-end | span-all ] |
[ self-block-start | center | self-block-end | span-self-block-start | span-self-block-end | span-all ] || [ self-inline-start | center | self-inline-end | span-self-inline-start | span-self-inline-end | span-all ] |
[ start | center | end | span-start | span-end | span-all ]{1,2} |
[ self-start | center | self-end | span-self-start | span-self-end | span-all ]{1,2}
position-try usageThis demo shows the effect of position-try.
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>
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 {
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;
position: absolute;
top: 100px;
left: 45%;
}
.infobox {
color: darkblue;
background-color: azure;
border: 1px solid #dddddd;
padding: 10px;
border-radius: 10px;
font-size: 1rem;
text-align: center;
}
form {
position: fixed;
bottom: 2px;
right: 2px;
}
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: --my-anchor;
bottom: anchor(top);
margin-bottom: 10px;
justify-self: anchor-center;
position-try: most-height --custom-bottom;
}
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.
| Specification |
|---|
| CSS Anchor Positioning Module Level 1 # position-try-prop |
position-areaposition-try-fallbacksposition-try-order@position-try at-rule<position-area> value