CSS Anchor Positioning API 可讓您將元素定位在其他元素 (稱為「錨點」) 的相對位置。這個 API 可簡化許多介面功能 (例如選單和子選單、工具提示、選取器、標籤、資訊卡、設定對話方塊等) 的複雜版面配置需求。瀏覽器內建錨點定位功能,因此您不必依賴第三方程式庫,就能建構分層使用者介面,開創無限創意可能。
核心概念:錨點和定位元素
這項 API 的核心是錨點和定位元素之間的關係。錨點是使用 anchor-name 屬性指定為參考點的元素。定位元素是指使用 position-anchor 屬性相對於錨點放置的元素,或是在定位邏輯中明確使用 anchor-name 的元素。
設定錨點
建立錨點的過程很簡單。將 anchor-name 屬性套用至所選元素,並指派專屬 ID。這個專屬 ID 必須加上雙破折號前置字元,就像 CSS 變數一樣。
.anchor-button {
anchor-name: --anchor-el;
}
指派錨點名稱後,.anchor-button 會做為錨點,引導其他元素放置位置。您可以透過下列兩種方式,將這個錨點連結至其他元素:
隱含錨點
如要將錨點連結至其他元素,第一種方法是使用隱含錨點,如下列程式碼範例所示。position-anchor 屬性會新增至要連結至錨點的元素,並以錨點名稱 (本例中為 --anchor-el) 做為值。
.positioned-notice {
position-anchor: --anchor-el;
}
使用隱含錨點關係時,您可以使用 anchor() 函式放置元素,而不必在第一個引數中明確指定錨點名稱。
.positioned-notice {
position-anchor: --anchor-el;
top: anchor(bottom);
}
明確錨點
或者,您也可以直接在錨點函式中使用錨點名稱 (例如 top: anchor(--anchor-el bottom)。這稱為「明確錨點」,如果您想錨定多個元素,這會很方便 (請繼續閱讀範例)。
.positioned-notice {
top: anchor(--anchor-el bottom);
}
將元素定位在錨點的相對位置
錨點定位功能是以 CSS 絕對定位為基礎建構而成。如要使用定位值,您需要在已定位的元素中新增 position: absolute。然後使用 anchor() 函式套用定位值。舉例來說,如要將錨定元素放在錨定元素的左上角,請使用下列定位方式:
.positioned-notice {
position-anchor: --anchor-el;
/* absolutely position the positioned element */
position: absolute;
/* position the right of the positioned element at the right edge of the anchor */
right: anchor(right);
/* position the bottom of the positioned element at the top edge of the anchor */
bottom: anchor(top);
}
現在,您已將一個元素錨定至另一個元素,如下所示:

如要對這些值使用邏輯定位,對應值如下:
top=inset-block-startleft=inset-inline-startbottom=inset-block-endright=inset-inline-end
使用 anchor-center 將已定位的元素置中
為方便將錨定位置元素置中於錨點,我們新增了名為 anchor-center 的值,可用於 justify-self、align-self、justify-items 和 align-items 屬性。
這個範例會修改先前的範例,使用 justify-self: anchor-center 將定位元素置於錨點上方中央。
.positioned-notice {
position: absolute;
/* Anchor reference */
position-anchor: --anchor-el;
/* Position bottom of positioned elem at top of anchor */
bottom: anchor(top);
/* Center justification to the anchor */
justify-self: anchor-center;
}
justify-center 將錨點置中的示範。
多個錨點
元素可繫結至多個錨點。也就是說,您可能需要設定相對於多個錨點的位置值。如要這麼做,請使用 anchor() 函式,並在第一個引數中明確指出要參照的錨點。在下列範例中,定位元素的左上角會錨定至其中一個錨點的右下角,而定位元素的右下角會錨定至第二個錨點的左上角:
.anchored {
position: absolute;
top: anchor(--one bottom);
left: anchor(--one right);
right: anchor(--two left);
bottom: anchor(--two top);
}

使用 position-area 調整位置
錨定 API 包含使用 position-area 屬性的新版面配置機制。
這項屬性可讓您將錨定位置的元素放置在各自的錨點相對位置,並在以錨定元素為中心的 9 格網格上運作。
如要使用 position-area 而非絕對定位,請使用 position-area 屬性,並搭配實體或邏輯值。例如:
- 頂端中央:
position-area: top或position-area: block-start - 左中:
position-area: left或position-area: inline-start - 正下方:
position-area: bottom或position-area: block-end - 右中:
position-area: right或position-area: inline-end

如要進一步瞭解這些職位,請參閱下列工具:
使用 anchor-size() 調整元素大小
anchor-size() 函式也是錨點定位 API 的一部分,可用於根據錨點的大小 (寬度、高度或內嵌和區塊大小),調整錨點定位元素的大小或位置。
以下 CSS 範例說明如何將這項功能用於高度,在 calc() 函式中使用 anchor-size(height),將工具提示的高度上限設為錨點高度的兩倍。
.positioned-notice {
position-anchor: --question-mark;
/* set max height of the tooltip to 2x height of the anchor */
max-height: calc(anchor-size(height) * 2);
}
anchor-size 示範
使用錨點搭配頂層元素,例如快顯視窗和對話方塊
錨點定位功能與頂層元素 (例如 popover) 搭配使用時,效果極佳。和 <dialog>。雖然這些元素會放置在與其餘 DOM 子樹狀結構不同的圖層中,但錨點定位功能可讓您將這些元素繫結回不在頂層的元素,並與這些元素一起捲動。這對分層介面來說是一大進展。
在下列範例中,系統會使用按鈕觸發開啟一組工具提示快訊。按鈕是錨點,工具提示則是定位元素。您可以像設定其他錨定元素一樣,設定定位元素的樣式。在這個特定範例中,anchor-name 和 position-anchor 是按鈕和工具提示的內嵌樣式。由於每個錨點都需要專屬錨點名稱,因此產生動態內容時,內嵌是達成這項要求的簡單方法。
popover
使用 @position-try 調整錨點位置
取得初始錨點位置後,如果錨點到達所含區塊的邊緣,您可能需要調整位置。如要建立替代錨點位置,可以使用 @position-try 指示和 position-try-fallbacks 屬性。
在下列範例中,選單右側會顯示子選單。選單和子選單很適合搭配使用錨點定位 API 和 popover 屬性,因為這些選單通常會錨定至觸發按鈕。
如果水平空間不足,這個子選單可以改為顯示在選單下方。如要這麼做,請先設定初始位置:
#submenu {
position: absolute;
position-anchor: --submenu;
/* initial position */
margin-left: var(--padding);
position-area: right span-bottom;
}
接著,使用 @position-try 設定備用錨定位置:
/* alternate position */
@position-try --bottom {
margin: var(--padding) 0 0 var(--padding);
posotion-area: bottom;
}
最後,使用 position-try-fallbacks 連接兩者。總而言之,看起來會像這樣:
#submenu {
position: absolute;
position-anchor: --submenu;
/* initial position */
margin-left: var(--padding);
position-area: right span-bottom;
*/ connect with position-try-fallbacks */
position-try-fallbacks: --bottom;
}
/* alternate position */
@position-try --bottom {
margin: var(--padding) 0 0 var(--padding);
position-area: bottom;
}
popover 的示範錨點位置自動翻轉關鍵字
如果您要進行基本調整,例如從上到下或從左到右翻轉 (或兩者皆是),甚至可以略過建立自訂 @position-try 宣告的步驟,並使用瀏覽器支援的內建翻轉關鍵字,例如 flip-block 和 flip-inline。這些函式可做為自訂 @position-try 宣告的替代項目,且可搭配使用:
position-try-fallbacks: flip-block, flip-inline, flip-block flip-inline;
翻轉關鍵字可大幅簡化錨點程式碼。只要幾行程式碼,就能建立具有替代位置的完整錨點:
#my-tooltip {
position-anchor: --question-mark;
position-area: top;
position-try-fallbacks: flip-block;
}
position-try-fallbacks: flip-blockposition-visibility,適用於子捲軸中的錨點
在某些情況下,您可能想將元素錨定在網頁的子捲軸中。在這些情況下,您可以使用 position-visibility 控制錨點的顯示設定。錨點何時會保持在視野內?何時會消失?您可以透過這項功能控管這些選項。當您希望定位元素保持在檢視區塊中,直到錨點超出檢視區塊時,請使用 position-visibility: anchors-visible:
#tooltip {
position: fixed;
position-anchor: --anchor-top-anchor;
position-visibility: anchors-visible;
bottom: anchor(top);
}
position-visibility: anchors-visible 示範或者,您也可以使用 position-visibility: no-overflow,防止錨點溢出容器。
#tooltip {
position: absolute;
position-anchor: --anchor-top-anchor;
position-visibility: no-overflow;
bottom: anchor(top);
}
position-visibility: no-overflow 示範特徵偵測和 Polyfill
由於目前瀏覽器支援有限,您可能需要採取一些預防措施,才能使用這項 API。首先,您可以使用 @supports 功能查詢,直接在 CSS 中檢查支援情形。如要這樣做,請將錨點樣式包裝在下列項目中:
@supports (anchor-name: --myanchor) {
/* Anchor styles here */
}
此外,您可以使用 Oddbird 的 CSS 錨點定位 Polyfill,填補錨點定位功能,適用於 Firefox 54、Chrome 51、Edge 79 和 Safari 10。這個 Polyfill 支援大多數基本錨點位置功能,但目前的實作方式並不完整,且包含一些過時的語法。您可以使用 unpkg 連結,或直接在套件管理員中匯入。
無障礙功能注意事項
雖然錨點定位 API 可讓元素相對於其他元素定位,但不會在兩者之間建立任何有意義的語意關係。如果錨定元素和定位元素之間確實存在語意關係 (例如定位元素是錨定文字的側欄註解),其中一種做法是使用 aria-details 從錨定元素指向定位元素。螢幕閱讀器軟體仍在學習如何處理 aria-details,但支援功能已有所提升。
<div class="anchor" aria-details="sidebar-comment">Main content</div>
<div class="positioned" id="sidebar-comment">Sidebar content</div>
.anchor {
anchor-name: --anchor;
}
.positioned {
position: fixed;
position-anchor: --anchor;
}
如果您使用 popover 屬性或 <dialog> 元素進行錨點定位,瀏覽器會處理焦點導覽修正,確保無障礙功能正常運作,因此您不必依 DOM 順序排列快顯視窗或對話方塊。如要進一步瞭解無障礙功能,請參閱規格中的附註。