絕對定位元素(position:absolute)脫離文檔流,相對最近的已定位祖先元素定位。核心模式:
| 方向 | 貼齊屬性 | 對應反向屬性 |
|---|---|---|
| 左 | left: 0 | right: auto |
| 右 | right: 0 | left: auto |
| 頂 | top: 0 | bottom: auto |
| 底 | bottom: 0 | top: auto |
.abs-left { position:absolute; left:0; right:auto; margin-left: 20px; /* 右移 20px */ }
.abs-right { position:absolute; left:auto; right:0; margin-right: -10px; /* 左移 10px */ }
.abs-top { position:absolute; top:0; bottom:auto; margin-top: 15px; /* 下移 15px */ }
.abs-bottom{ position:absolute; top:auto; bottom:0; margin-bottom: -5px; /* 上移 5px */ }
width: 200px; height: 100px;width: auto; height: auto;(收縮包裹)left: 0; right: 0; width: auto; → 橫向滿鋪所有元素。注意:絕對定位元素的百分比尺寸相對「定位最近的祖先元素」,而非視窗或父元素。