CKTIS / CSS
@position-try — named fallback position option
The @position-try rule defines a fallback position for an anchored box, selected when the preferred option causes overflow.
Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required
Syntax
@position-try <dashed-ident> {
<declaration-list>
}@position-try <dashed-ident> { <declaration-list> } gives a custom position option a name. The position-try-fallbacks property references that name; the base style is considered first, then alternatives are tried in list order when the box overflows its inset-modified containing block. Rules with the same name cascade in the same way as keyframe rules.
The block accepts only inset, margin, sizing, self-alignment, anchor, and position-area properties. An !important flag makes that declaration invalid without invalidating the whole rule. These declarations use the Position Fallback cascade origin between the ordinary author and animation origins, so they can replace base positioning without restyling the component’s colors or content.
A tooltip, menu, or label near a viewport edge is a typical use. The example prefers block-start, defines --below with block-end, and then measures both boxes. Lack of room above the anchor forces the lower option; reading CSSPositionTryRule also confirms that the engine retained the rule in CSSOM.
Examples
const style = document.createElement('style');
style.textContent = `.anchor { position: fixed; inset: 0 auto auto 100px; inline-size: 60px; block-size: 20px; anchor-name: --trigger; }
.popup { position: fixed; position-anchor: --trigger; position-area: block-start; position-try-fallbacks: --below; inline-size: 80px; block-size: 30px; }
@position-try --below { position-area: block-end; }`;
document.head.append(style);
const anchor = document.createElement('div');
anchor.className = 'anchor';
const popup = document.createElement('div');
popup.className = 'popup';
document.body.append(anchor, popup);
await new Promise(requestAnimationFrame);
const anchorRect = anchor.getBoundingClientRect();
const popupRect = popup.getBoundingClientRect();
const rule = [...style.sheet.cssRules].find(item => item.cssText.startsWith('@position-try'));
console.log('rule=' + rule.constructor.name + '; fallback=' + (popupRect.top >= anchorRect.bottom ? 'below' : 'above') + '; anchor-bottom=' + anchorRect.bottom + '; popup-top=' + popupRect.top);Result
rule=CSSPositionTryRule; fallback=below; anchor-bottom=20; popup-top=20
The @position-try rule defines a fallback position for an anchored box, selected when the preferred option causes overflow.