No, the CSS Anchored Positioning specification (which includes position-try-options and position-try-fallbacks) does not directly provide a mechanism to animate the process of trying positions or the switch between fallbacks itself.
position-try-options and position-try-fallbacks are declarative rules that tell the browser how to calculate the final position of an anchored element. The browser evaluates these rules, picks the first one that fits (or the last fallback if none fit), and then applies the resulting top, left, right, bottom, or inset properties.
However, you can animate or transition the resulting position of the element.
Here's how it works:
- Selection: The browser determines which anchor position to use based on
position-try-options and position-try-fallbacks. This results in specific computed values for top, left, right, bottom, etc.
- Animation/Transition: If you have CSS
transition or animation applied to the properties that are being set by the chosen anchor position (e.g., top, left, transform), then any change to these computed values will be animated or transitioned.
Example:
.anchor {
width: 100px;
height: 100px;
background: lightblue;
position: relative; /* Or any containing block */
}
.popup {
position: absolute; /* Required for anchored positioning */
width: 150px;
height: 50px;
background: salmon;
/* Anchoring rules */
anchor-name: --my-anchor;
position-anchor: --my-anchor;
position-try-options: --try-below, --try-above;
/* Animate the properties that get set by the anchor rules */
transition: top 0.3s ease, left 0.3s ease, bottom 0.3s ease, right 0.3s ease;
}
@position-try --try-below {
top: anchor(bottom, --my-anchor);
left: anchor(center, --my-anchor);
transform: translateX(-50%);
}
@position-try --try-above {
bottom: anchor(top, --my-anchor);
left: anchor(center, --my-anchor);
transform: translateX(-50%);
}
/* For demo: trigger a change that might cause a fallback switch */
.anchor.scrolled-down { /* imagine this class is added on scroll */
/* This might cause --try-below to go off-screen,
triggering --try-above */
}
<div class="anchor">
Anchor Element
<div class="popup">Popup</div>
</div>
In this example:
- The popup will try to position itself below the anchor (
--try-below).
- If that position doesn't fit (e.g., the anchor is too close to the bottom of the viewport), it will try to position itself above the anchor (
--try-above).
- If such a switch occurs (e.g., due to scrolling or viewport resize), the
transition on top, left, bottom, right will cause the popup to smoothly move from its old position to its new position.
Key takeaway:
You don't animate the "trying" or "fallbacking" logic. You animate the CSS properties (top, left, transform, etc.) whose values are determined by that logic. When the logic results in a new set of values for these properties, the standard CSS transition/animation mechanisms will apply to those changes.
The CSS Anchored Positioning API is still somewhat experimental and browser support is evolving, so always check caniuse.com for the latest status.