position-try

Baseline 2026
Newly available

Since January 2026, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.

Thuộc tính position-try của CSS là thuộc tính viết tắt tương ứng với các thuộc tính position-try-orderposition-try-fallbacks.

Các thuộc tính thành phần

Thuộc tính này là thuộc tính viết tắt cho các thuộc tính CSS sau:

Cú pháp

css
/* Chỉ position-try-fallbacks */
position-try: normal flip-block;
position-try: top;
position-try: --custom-try-option;
position-try: flip-block flip-inline;
position-try: top, right, bottom;
position-try: --custom-try-option1, --custom-try-option2;
position-try:
  normal flip-block,
  right,
  --custom-try-option;

/* position-try-order và position-try-fallbacks */
position-try: normal none;
position-try:
  most-width --custom-try-option1,
  --custom-try-option2;
position-try:
  most-height flip-block,
  right,
  --custom-try-option;

/* Giá trị toàn cục */
position-try: inherit;
position-try: initial;
position-try: revert;
position-try: revert-layer;
position-try: unset;

Giá trị

Xem position-try-orderposition-try-fallbacks để biết mô tả các giá trị.

Thuộc tính viết tắt position-try có thể chỉ định giá trị cho position-try-fallbacks, hoặc position-try-orderposition-try-fallbacks, theo thứ tự đó. Nếu position-try-order bị bỏ qua, nó sẽ được đặt thành giá trị khởi tạo của thuộc tính, là normal, nghĩa là các tùy chọn dự phòng position-try được thử theo thứ tự xuất hiện trong thuộc tính.

Định nghĩa hình thức

Initial valueas each of the properties of the shorthand:
Applies toabsolutely positioned elements
Inheritedno
Percentagesas each of the properties of the shorthand:
Computed valueas each of the properties of the shorthand:
Animation typeas each of the properties of the shorthand:

Cú pháp hình thức

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}

Ví dụ

Sử dụng position-try cơ bản

Bản demo này minh họa hiệu ứng của position-try.

HTML

HTML bao gồm hai phần tử <div> sẽ trở thành một neo và một phần tử được định vị theo neo.

html
<div class="anchor">⚓︎</div>

<div class="infobox">
  <p>This is an information box.</p>
</div>

CSS

Trong CSS, neo được đặt tên anchor-name và có giá trị positionabsolute. Chúng ta định vị nó ở nửa trên của viewport bằng các giá trị topleft:

css
.anchor {
  anchor-name: --my-anchor;
  position: absolute;
  top: 100px;
  left: 45%;
}

Tiếp theo, chúng ta thêm tùy chọn vị trí tùy chỉnh --custom-bottom để định vị phần tử bên dưới neo và đặt lề phù hợp:

css
@position-try --custom-bottom {
  top: anchor(bottom);
  bottom: unset;
  margin-top: 10px;
}

Ban đầu chúng ta định vị phần tử phía trên neo, sau đó đặt giá trị position-try với position-try-ordermost-height và danh sách position-try-fallbacks chỉ bao gồm tùy chọn dự phòng tùy chỉnh của chúng ta:

css
.infobox {
  position: fixed;
  position-anchor: --my-anchor;

  bottom: anchor(top);
  margin-bottom: 10px;
  justify-self: anchor-center;

  position-try: most-height --custom-bottom;
}

Kết quả

Phần tử xuất hiện bên dưới neo dù ban đầu được định vị phía trên. Điều này xảy ra vì có nhiều không gian dọc hơn bên dưới neo. Thứ tự thử most-height khiến tùy chọn dự phòng --custom-bottom được áp dụng, đặt phần tử được định vị vào vị trí mang lại nhiều chiều cao nhất cho khối chứa của nó.

Thông số kỹ thuật

Specification
CSS Anchor Positioning Module Level 1
# position-try-prop

Khả năng tương thích trình duyệt

Xem thêm