原理
- 父元素設定
position: relative 建立定位基準。
- 子元素設定
position: absolute; bottom: 0; left: 0; 貼齊父元素左下角。
程式碼範例
<div class="container">
<div class="bottom-badge"><img src="icon.gif" width="20" height="20" alt=""></div>
</div>
.container {
position: relative;
width: 100px;
height: 100px;
border: 1px solid red;
}
.bottom-badge {
position: absolute;
bottom: 0;
left: 0;
width: 100px;
text-align: center;
}
應用場景
- 卡片右下角標籤(NEW、HOT)。
- 圖片疊加水印/版權資訊。
- 模態視窗關閉按鈕固定右上/右下。
注意事項
- 父元素必須有具體高度(或由內容撐開),否則
bottom:0 參考點不準。
- 若父元素有
padding,子元素會貼齊 padding 內緣;需配合 box-sizing 或調整座標。