position: absolute 讓元素:
<html>(初始包含塊)。top/right/bottom/left 精確控制偏移。.tooltip {
position: absolute;
left: 100px; /* 距定位上下文左邊緣 100px */
top: 43px; /* 距定位上下文上邊緣 43px */
width: 200px;
}
px, rem, %(相對定位上下文寬/高)、calc() 等。width 時,內容撐開寬度(收縮包裹)。| 祖先元素 position 值 | 是否建立定位上下文 |
|---|---|
static (默認) | ❌ 否 |
relative | ✅ 是 |
absolute | ✅ 是 |
fixed | ✅ 是(但固定定位相對視口) |
sticky | ✅ 是 |
transform / filter / perspective ≠ none | ✅ 是(CSS Transforms 規範) |
💡 實務技巧:父級加 position: relative 不改變佈局,卻能建立定位上下文,是「絕對定位錨點」最常用手法。
position: fixed; inset: 0; 覆蓋視口top:50%; left:50%; transform:translate(-50%,-50%)% / vw/vh / clamp() 避免固定像素在不同斷點崩壞。aria-hidden 控制)。絕對定位是「手術刀」而非「瑞士軍刀」——精準、強大,但濫用會導致佈局脆弱。掌握「定位上下文」與「脫流特性」,在正確場景精準出擊,才是高手用法。