@position-try - CSS At-rule

@position-try

Summary of characteristics of the @position-try at-rule

Quick description
Defines a set of rules for each anchor position and gives them a name (anchoring positioning).
Status
Standard
W3C Module
CSS Anchor Positioning
Document status: WD (Working Draft)

Description of the @position-try at-rule.

Anchor positioning allows you to position one element relative to another (the anchor). The latter has been given an identifier through the anchor-name property. The element or elements being positioned are linked to it through the position-anchor property.

The directive @position-try defines an identifier for a set of properties. This name can then be used by the property position-try-fallbacks or the shorthand property position-try. All properties of @position-try are then applied. The identifier must start with a double dash.

The basic syntax of@position-try is as follows:

@position-try --id { ... ... Liste de propriétés ... }

Les seules propriétés autorisées dans la liste sont :

  • The dimension properties: width, height, min-height, max-height, etc.
  • The properties that define margins: margin-top, margin-right, margin-block-start, margin-inline-start, etc. and the corresponding shorthand properties.
  • Positioning properties: top, inset-block-start, etc. and the corresponding shorthand properties.
  • The alignment properties of the element itself: align-self, justify-self, and place-self.
  • The position-anchor property and the position-area property.

The !important clause is invalid in a @position-try directive. Using it makes the property invalid, but the rest of the directive remains valid.

For more information on anchor positioning, the page anchor-name provides a list of useful terms and a tutorial on anchor positioning.

Examples of use of the @position-try at-rule.

The text is first positioned below the anchor, with a top margin of 5 pixels. By slowly scrolling the content down, you'll move the text above the anchor, with a bottom margin of 5 pixels. To put the text back below the anchor, you'll need to do the opposite: scroll the page down until the positioned text starts to go off the screen at the top.

All these features are defined by two directives @position-try. The first one sets the name --bottom, which applies when the positioned text is below the anchor. The second sets the name --top, which applies when the positioned text is above the anchor.
The switch from one position to the other is defined by position-try-fallbacks.

⚓
Positioned text
It is first below the anchor,
then when it is about to disappear,
it passes above the anchor.
*

Browsers compatibility with the @position-try at-rule.

The directive @position-try is well recognized by browsers. But there are a few subtleties not described in the standard:

  • When there are multiple anchors on the page, each element (anchors and positioned texts) must have an ID even if it's not used.
  • There's a difference between position:absolute and position:fixed.
Column 1
General support for anchor positioning.
Column 2
Support for the directive @position-try to define a set of alternative positions (anchoring positioning).

Notes:

(1) Samsung Internet does not support the position-anchor property with the value normal. It also doesn't fully support the anchor-visibility property.

(2) Android browser does not support the position-anchor property with the value normal.
It also doesn't fully support the anchor-visibility property.

(3) Doesn't fully support the anchor-visibility property.

(4) Firefox does not allow animation of the anchor() and anchor-size() functions.
It does not fully support the anchor-visibility property.

(5) Opera does not support the position-anchor property with the value normal.
It also doesn't fully support the anchor-visibility property.

1
Anchor
positionning
2
@position-try
at-rule
Estimated overall support.
86%
71%

Browsers on computers :

Mobile browsers :

Outdated or marginal browsers :

Internet Explorer

UC Browser pour Androïd

Opéra Mobile

QQ Browser

Baidu Browser

Samsung Internet

Androïd Brower

Chrome

Chrome pour Androïd

Edge

Firefox

Firefox pour Androïd

KaiOS Browser

Opéra

Safari

Safari sur IOS

Opéra mini

Evolution of the @position-try at-rule.

  • CSS Anchor Positioning - Level 1

    Absolute positioning relative to the page or screen can prove to be limited when you want to position an element relative to another.
    This specification describes the properties necessary to achieve this, as well as the functions anchor() and anchor-size() that allow you to set the position and dimensions of an element based on another.
    It also describes how to test several positioning possibilities to adapt to the context, particularly to the state of the page scrolling.

    Regarding @position-try Introduction of the @position-try at-rule, as well as all the properties involved in the anchoring positioning.
    WD
    June 29, 2023
    Working Draft.
    CR
    PR
    REC

See also, on anchor positioning.

The directive @position-try is the only one defined in the CSS Anchor Positioning. But there are quite a few properties:

Properties:

anchor-name
Sets an ID for the item, which can then become an anchor, in the context of anchor positioning.
anchor-scope
Defines the scope of the property anchor-name.
position-anchor
Defined in relation to which anchor the element should be positioned.
position-area
Defines how an element is positioned relative to the anchor (above, beside, etc.).
position-try
Shorthand property that summarizes the values for position-try-fallbacks and position-try-order.
position-try-fallbacks
Specifies a list of positions for the positioned element (anchoring positioning) so that the browser can choose the one that is most suitable at any given moment, taking into account scrolls or zooms.
position-try-order
Defines a different order for the values listed by position-try-fallbacks.
position-visibility
Defines whether the positioned element is visible or not, in the context of anchoring positioning.

Functions:

anchor()
Returns the position of one of the edges of the element designated as the anchor (anchor-based positioning).
anchor-size()
Returns the size (height or width) of the element designated as the anchor (anchor-based positioning).

At-rules:

@position-try
Defines a set of rules for each anchor position and gives them a name (anchoring positioning).