當父元素設定 position: relative(或 absolute/fixed/sticky),子元素設定 position: absolute 時,子元素的定位基準點變為**父元素的內邊距邊界**,而非視窗。
/* CSS */
#container { position: relative; width: 500px; height: 300px; border: 1px solid #ccc; }
#navigation { position: absolute; left: 30px; top: 5px; background: #f0f0f0; padding: 10px; }
<div id="container">
<div id="navigation">導航選單</div>
<p>其他內容...</p>
</div>
結果:#navigation 距離容器左邊 30px、上邊 5px。
html。z-index 控制層級,避免被遮擋。