CSS position-try Property
The CSS position-try property is a shorthand that controls how an anchor-positioned element tries alternative placements when its preferred position does not fit.
It combines position-try-order and position-try-fallbacks, allowing browsers to select suitable positions for tooltips, dropdown menus, popovers, and other floating elements. This helps prevent anchored content from overflowing its available display area.
position-try Syntax
The position-try shorthand accepts an optional fallback selection order followed by zero or more fallback options.
Basic Syntax
selector {
position-try: flip-block;
}
Common Values
.tooltip {
position-try: normal;
}
.tooltip {
position-try: flip-block;
}
.tooltip {
position-try: flip-inline;
}
.tooltip {
position-try: flip-block flip-inline;
}
.tooltip {
position-try: most-width flip-block;
}
.tooltip {
position-try: most-height flip-inline;
}
Named Fallbacks
.tooltip {
position-try: --alternate-position;
}
.tooltip {
position-try: flip-block, --alternate-position;
}
Named fallback options must be defined using the @position-try at-rule.
CSS-Wide Keywords
.tooltip {
position-try: initial;
}
.tooltip {
position-try: inherit;
}
.tooltip {
position-try: unset;
}
.tooltip {
position-try: revert;
}
.tooltip {
position-try: revert-layer;
}
Property Values
| Value | Description |
|---|---|
normal |
Uses the normal fallback selection order. The browser prefers the original position when it fits. |
most-width |
Prefers the fitting position option that provides the greatest available width. |
most-height |
Prefers the fitting position option that provides the greatest available height. |
most-block-size |
Prefers the fitting position option with the greatest available block-axis size. |
most-inline-size |
Prefers the fitting position option with the greatest available inline-axis size. |
flip-block |
Generates a fallback that flips the preferred placement along the block axis. |
flip-inline |
Generates a fallback that flips the preferred placement along the inline axis. |
flip-start |
Generates a fallback by swapping the relevant start and end alignment directions. |
<dashed-ident> |
References a custom fallback defined using @position-try. |
initial |
Resets the shorthand to its initial longhand values. |
inherit |
Inherits the corresponding longhand values from the parent. |
unset |
Resets the shorthand to its initial longhand values because they are not inherited. |
revert |
Reverts the shorthand according to CSS cascade origin rules. |
revert-layer |
Reverts the shorthand according to CSS cascade layer rules. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | normal for position-try-order and none for position-try-fallbacks |
| Inherited | No |
| Applies to | Absolutely positioned elements |
| Computed value | Determined by the constituent longhand properties |
| Animation type | Determined by the constituent longhand properties |
| Percentages | Not applicable to the shorthand itself |
Valid Values
.tooltip {
position-try: flip-block;
}
.menu {
position-try: flip-inline, flip-block;
}
.popover {
position-try: most-height flip-block;
}
.label {
position-try: --custom-fallback;
}
Invalid Values
.tooltip {
position-try: 20px;
}
.tooltip {
position-try: left;
}
.tooltip {
position-try: most-width most-height;
}
.tooltip {
position-try: flip-top;
}
.tooltip {
position-try: auto;
}
The shorthand accepts supported selection-order keywords and fallback options. It does not accept lengths, arbitrary direction words, or multiple selection-order keywords.
How It Works
Anchor-positioned elements may overflow the available display area when their preferred placement is close to a viewport boundary.
The position-try property provides alternative positioning options that the browser can evaluate when the original placement does not fit.
Step 1: Establish an Anchor
.help-button {
anchor-name: --help-anchor;
}
Step 2: Define the Preferred Placement
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: bottom;
}
The tooltip is initially positioned below the button.
Step 3: Add a Fallback
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: bottom;
position-try: flip-block;
}
If the tooltip does not fit below the anchor, the browser can try placing it above the anchor.
Step 4: Evaluate Available Space
The browser evaluates the original placement and applicable fallback options against the available space.
Step 5: Select a Position
With the normal selection order, the browser uses the first position option that fits. Other selection-order values can prioritize available width or height.
Important Distinction
The position-area property establishes the preferred placement. The position-try property controls which alternative placements can be attempted and how they are prioritized.
Shorthand Properties
The position-try shorthand combines two longhand properties.
| Longhand Property | Purpose |
|---|---|
position-try-order |
Controls the order in which the browser considers the preferred placement and fallback options. |
position-try-fallbacks |
Defines the alternative positions the browser can attempt. |
Shorthand Example
.tooltip {
position-try: most-width flip-block, flip-inline;
}
This is equivalent to:
.tooltip {
position-try-order: most-width;
position-try-fallbacks: flip-block, flip-inline;
}
Fallbacks Only
.tooltip {
position-try: flip-block;
}
This sets the fallback list to flip-block and resets the selection order to normal.
Selection Order Only
.tooltip {
position-try: most-height;
}
This sets position-try-order to most-height and resets position-try-fallbacks to none.
Resetting the Shorthand
.tooltip {
position-try: normal;
}
This restores the normal selection order and clears the fallback list.
Important Consideration
Like other CSS shorthands, position-try resets omitted longhand values to their initial values.
Position Fallbacks
Position fallback options describe how the browser can modify an anchored element's preferred placement.
Flip Along the Block Axis
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: bottom;
position-try: flip-block;
}
In a typical horizontal writing mode, the fallback changes a placement below the anchor to one above it.
Flip Along the Inline Axis
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: right;
position-try: flip-inline;
}
In left-to-right horizontal writing mode, the fallback can move the tooltip from the right side to the left side.
Flip Start and End Alignment
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: bottom right;
position-try: flip-start;
}
The flip-start option can reverse the relevant start and end alignment while retaining the general placement.
Multiple Fallbacks
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: bottom;
position-try: flip-block, flip-inline;
}
The comma-separated list defines separate fallback options that the browser can evaluate.
Combined Flip Options
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: bottom right;
position-try: flip-block flip-inline;
}
Without a comma, the flip keywords form one combined fallback option rather than two separate alternatives.
Important Consideration
Fallback options are evaluated according to the applicable position selection rules. They do not guarantee that a fitting position will always be available.
Fallback Selection Order
The position-try-order component determines how the browser prioritizes candidate positions.
Normal Selection
.tooltip {
position-try: normal flip-block, flip-inline;
}
The browser prefers the original placement if it fits, then considers the fallback options in their listed order.
Prefer Available Width
.tooltip {
position-try: most-width flip-block, flip-inline;
}
The browser can prioritize the candidate with the greatest available width, rather than simply using the first fitting option.
Prefer Available Height
.tooltip {
position-try: most-height flip-block, flip-inline;
}
This is useful for vertically oriented menus or panels that need additional height.
Prefer Block-Axis Space
.tooltip {
position-try: most-block-size flip-block;
}
The browser considers available space along the logical block axis.
Prefer Inline-Axis Space
.tooltip {
position-try: most-inline-size flip-inline;
}
The browser considers available space along the logical inline axis.
Important Consideration
Available-space ordering is particularly useful when the positioned element's dimensions vary with its content or viewport size.
Custom Fallback Positions
The @position-try at-rule allows developers to define custom fallback positioning options.
Define a Custom Fallback
@position-try --above-anchor {
position-area: top;
margin-bottom: 8px;
}
This named fallback places an element above its anchor and adds spacing below the positioned element.
Reference the Fallback
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: bottom;
position-try: --above-anchor;
}
If the preferred position does not fit, the browser can try the custom fallback.
Combine Built-In and Custom Fallbacks
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: bottom;
position-try: flip-block, --above-anchor;
}
The browser can evaluate the built-in flip option and the custom fallback as separate alternatives.
Multiple Custom Fallbacks
@position-try --place-left {
position-area: left;
}
@position-try --place-right {
position-area: right;
}
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-area: bottom;
position-try: --place-left, --place-right;
}
The fallback list provides alternative placements to the left and right of the anchor.
Important Consideration
Custom fallback names must begin with two hyphens and must correspond to defined @position-try rules. Only properties permitted by the at-rule can be used inside its declaration block.
position-try Example
The following example places a tooltip below a button and allows it to move above the button when there is insufficient space below.
HTML Example
<div class="position-try-demo">
<button class="demo-button" type="button">Help Button</button>
<div class="demo-tooltip">This tooltip can flip above the button.</div>
</div>
CSS Example
.position-try-demo {
position: relative;
min-height: 180px;
padding: 25px;
text-align: center;
border: 2px solid #94a3b8;
background-color: #f1f5f9;
}
.position-try-demo .demo-button {
anchor-name: --demo-anchor;
padding: 10px 18px;
border: 0;
background-color: #2563eb;
color: #ffffff;
}
.position-try-demo .demo-tooltip {
position: absolute;
position-anchor: --demo-anchor;
position-area: bottom;
position-try: flip-block;
margin: 8px;
padding: 10px 14px;
background-color: #1e293b;
color: #ffffff;
}
@supports not ((position-anchor: --demo-anchor) and (position-area: bottom) and (position-try: flip-block)) {
.position-try-demo .demo-tooltip {
top: 85px;
left: 50%;
transform: translateX(-50%);
}
}
The tooltip's preferred placement is below the button. When that placement does not fit within the applicable overflow constraints, the browser can try a block-axis flip.
The fallback is conditional, so the tooltip may remain below the button when sufficient space is available.
Adding Another Fallback
.position-try-demo .demo-tooltip {
position-try: flip-block, flip-inline;
}
The browser can try the block-axis flip first, followed by the inline-axis flip if needed.
Prioritizing Available Height
.position-try-demo .demo-tooltip {
position-try: most-height flip-block;
}
This configuration prioritizes the candidate position with the greatest available height.
Accessibility Considerations
The position-try property changes visual placement but does not change an element's semantics or document order.
Maintain Logical Source Order
Keep related controls and anchored content in a meaningful HTML sequence.
Preserve Keyboard Navigation
Interactive popovers and menus must remain accessible through keyboard navigation regardless of their fallback position.
Avoid Obscuring Controls
Fallback positioning can help reduce overflow, but positioned elements should still be tested for overlap with important controls.
Support Zoom and Text Resizing
Test anchored content at increased zoom levels and with larger text to ensure fallback positions remain usable.
Use Appropriate Semantics
Tooltips, menus, and popovers should use suitable HTML and ARIA relationships for their intended interactions.
Maintain Focus Visibility
Ensure focus indicators remain visible when an anchored element changes position.
Important Consideration
Fallback positioning improves visual placement but does not automatically manage focus, keyboard behavior, or accessibility relationships.
Common Mistakes
Using position-try Without an Anchor
.tooltip {
position: absolute;
position-try: flip-block;
}
Position fallbacks require a suitable anchor-positioning relationship to produce the intended anchor-relative behavior.
Forgetting the Preferred Placement
.tooltip {
position: absolute;
position-anchor: --help-anchor;
position-try: flip-block;
}
Define a preferred placement using position-area or appropriate anchor-based inset properties.
Confusing Fallback Options With Selection Order
flip-block defines an alternative placement. most-width controls how candidate positions are prioritized.
Using Multiple Selection-Order Keywords
.tooltip {
position-try: most-width most-height;
}
Only one selection-order keyword is permitted in the shorthand.
Forgetting Commas Between Alternatives
.tooltip {
position-try: flip-block flip-inline;
}
This is one combined fallback option. Use a comma when two separate alternatives are intended.
Referencing an Undefined Custom Fallback
.tooltip {
position-try: --missing-fallback;
}
A named fallback requires a corresponding @position-try definition.
Expecting Guaranteed Overflow Prevention
The browser can only select from the available position options. If none fits, overflow may still occur.
Ignoring Writing Modes
Block-axis and inline-axis flips depend on writing mode and direction. They do not always correspond to fixed physical directions.
Assuming Universal Browser Support
CSS Anchor Positioning is a newer feature. Provide appropriate fallback styling for browsers without support.
Important Considerations
Keep the following points in mind when using position-try:
- It is a shorthand for
position-try-orderandposition-try-fallbacks. - It controls fallback positioning for absolutely positioned elements.
- It is part of CSS Anchor Positioning.
- The property is not inherited.
- The initial selection order is
normal. - The initial fallback list is
none. - The shorthand resets omitted longhand values to their initial values.
- Use
position-areaor anchor-based inset properties to establish preferred placement. - Use
position-anchorto select a named default anchor when needed. flip-blockgenerates a block-axis fallback.flip-inlinegenerates an inline-axis fallback.flip-startadjusts relevant start and end alignment.- Comma-separated fallback options are evaluated as separate alternatives.
- Space-separated flip keywords can form a combined fallback option.
most-widthprioritizes available physical width.most-heightprioritizes available physical height.most-block-sizeprioritizes available logical block size.most-inline-sizeprioritizes available logical inline size.- Custom fallback positions can be defined using
@position-try. - Custom fallback names begin with two hyphens.
- The browser evaluates candidate positions against applicable overflow constraints.
- The original position is preferred under the normal selection order when it fits.
- Fallback positioning does not guarantee that all overflow will be prevented.
- Writing mode and direction affect logical flip behavior.
- Browser support varies across CSS Anchor Positioning features.
- Provide conventional positioning fallbacks when necessary.
- Test anchored content near viewport boundaries.
- Test positioned elements with different content lengths and viewport sizes.
- Maintain logical HTML source order and accessible interactions.
Browser Support
The CSS position-try property has Limited availability and is not supported consistently across all widely used browsers.
It belongs to the CSS Anchor Positioning feature set. Support for related properties such as position-anchor, position-area, and the @position-try at-rule may vary by browser and version.
For current compatibility information, visit Can I Use? and search for position-try.
Use feature queries and conventional positioning fallbacks when supporting browsers without the required anchor-positioning features.
Specifications
The position-try property is defined in CSS Anchor Positioning Module Level 1.
CSS Anchor Positioning Module Level 1
The specification defines the shorthand, fallback options, and position selection behavior for anchor-positioned elements.
For additional documentation and examples, consult MDN Web Docs.
