父元素設定 position:relative (或 absolute/fixed/sticky) 建立**定位上下文**,子元素 position:absolute 時,top/right/bottom/left 即相對於**最近的已定位祖先**而非 body。
<div id="container" style="position:relative; width:400px; height:300px; border:1px solid #ccc;">
<div id="nav" style="position:absolute; left:30px; top:5px; background:#1976d2; color:#fff; padding:.5rem 1rem;">
導航欄 (相對 container 定位)
</div>
</div>
position: relative | absolute | fixed | stickytransform / filter / perspective / clip-path / mask / contain 等非 none 值will-change 指定上述屬性position:absolute 時)relative,子 absolute; top:100%; left:0。relative,標籤 absolute 百分比座標。fixed 相對視窗,或 absolute 相對 relative 容器。body 或 html 定位,導致位置錯亂。.parent{transform:translateZ(0)} 會讓內部 fixed 子元素相對父層而非視窗定位 (規格規定)。height:100% 才生效。/* 開發時暫時加上,視覺化定位上下文 */
*[style*="position:relative"],
*[style*="position:absolute"],
*[style*="position:fixed"] {
outline: 1px dashed red;
}