position: static | relative | absolute | fixed | sticky
| 值 | 行為 | 脫離文件流 | 參考基準 |
|---|---|---|---|
static | 預設,正常文件流 | 否 | — |
relative | 相對自身原位置偏移 | 否(保留原空間) | 自身原位置 |
absolute | 相對最近「已定位」祖先元素 | 是 | 最近 position≠static 祖先,否則 body |
fixed | 相對視窗固定 | 是 | 視窗 |
sticky | 滾動至閾值時固定 | 否→是 | 視窗/最近滾動祖先 |
absolute/fixed 必須配合 top/right/bottom/left 至少一個,否則仍按 static 佈局。absolute 元素無 margin 疊加,但保有 padding/border。relative 後續內容不會佔據其偏移後的空間;absolute 後續內容會佔據其原空間。z-index: auto | <integer>
auto:繼承父層堆疊上下文。z-index 依 HTML 順序,後者上層。position: relative/absolute/fixed/sticky 生效。<select> 等視窗控制項(IE6-7),IE5.5+ <iframe> 支援。top/right/bottom/left: auto | <length> | <percentage>
auto:預設,瀏覽器自動計算。length:具體長度(px, em, rem...)。percentage:相對包含塊寬(left/right)或高(top/bottom)。position ≠ static 時生效。offsetTop/Left、getBoundingClientRect(),勿直接操作字串值。.tooltip {
position: absolute;
top: 100%; left: 50%;
transform: translateX(-50%);
z-index: 1000;
margin-top: 8px; /* 與觸發元素間距 */
}




父層建立堆疊上下文(如 opacity<1、transform≠none、filter≠none、z-index≠auto 的定位元素)會「封鎖」子元素的 z-index 跨層比較。子元素再大的 z-index 也無法超出父層上下文外的其他元素。