在許多網頁版面中,頁尾通常需要固定在瀏覽器可視區域的最下方,不隨內容捲動而消失。若想達到這個效果,可以使用 position: fixed,再搭配 bottom 與寬度設定即可。
.bottom {
position: fixed;
right: 0;
bottom: 0;
width: 100%;
background: #f1f1f1;
color: #fff;
border-top: 1px solid #fff;
text-align: center;
}
.bottom span {
position: relative;
display: block;
padding: 5px;
}
在實際頁面中,可以使用一個 class 為 bottom 的容器,裡面再放入多個 span 來承載文字內容,例如公司資訊、地址、電話或版權宣告。由於 span 是行內元素,若要讓它呈現區塊般的排列與內距效果,通常會搭配 display: block 使用。
在較舊的 IE 環境中,fixed 定位可能無法完全生效,因此常見的相容做法會加上底線前綴的 hack,讓舊版 IE 改用 absolute 定位模擬。
.bottom {
position: fixed;
_position: absolute;
bottom: 0;
}
如果頁尾固定在畫面最下方,可能會遮住頁面底部的內容。此時可以在主要內容區塊加上足夠的底部內距或外距,避免最後一段文字、按鈕或表格被頁尾覆蓋。此外,若頁尾含有多行文字,也建議先確認實際高度,再決定是否要預留更多空間。