国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > CSS > 正文

詳解八種方法實現CSS頁面底部固定

2024-07-11 08:33:37
字體:
來源:轉載
供稿:網友

當我們在寫頁面時經常會遇到頁面內容少的時候,footer會戳在頁面中間或什么?反正就是不在最底部顯示,反正就是很難看,下面要講的布局就是解決如何使元素粘住瀏覽器底部,

方法一:footer高度固定+絕對定位

html


<div class="dui-container">
<header>Header</header>
<main>Content</main>
<footer>Footer</footer>
</div>

CSS


.dui-container{
position: relative;
min-height: 100%;
}
main {
padding-bottom: 100px;
}
header, footer{
line-height: 100px;
height: 100px;
}
footer{
width: 100%;
position: absolute;
bottom: 0
}

查看效果

方法二:在主體content上的下邊距增加一個負值等于底部高度

html


<header>Header</header>
<main>Content</main>
<footer>Footer</footer>

CSS


html, body {
height: 100%;
}
main {
min-height: 100%;
padding-top: 100px;
padding-bottom: 100px;
margin-top: -100px;
margin-bottom: -100px;
}
header, footer{
line-height: 100px;
height: 100px;
}

查看效果

方法三:將頁腳的margin-top設為負數

html


<header>Header</header>
<main>Content</main>
<footer>Footer</footer>

CSS


main {
min-height: 100%;
padding-top: 100px;
padding-bottom: 100px;
}
header, footer{
line-height: 100px;
height: 100px;
}
header{
margin-bottom: -100px;
}
footer{
margin-top: -100px;
}

查看效果

方法四: 通過設置flex,將footer的margin-top設置為auto

html


<header>Header</header>
<main>Content</main>
<footer>Footer</footer>

CSS


body{
display: flex;
min-height: 100vh;
flex-direction: column;
}
header,footer{
line-height: 100px;
height: 100px;
}
footer{
margin-top: auto;
}

查看效果

方法五: 通過函數calc()計算內容的高度

html代碼


<header>Header</header>
<main>Content</main>
<footer>Footer</footer>

CSS代碼


main{
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 巴中市| 贵德县| 铜川市| 苏州市| 汉阴县| 云南省| 汕头市| 郯城县| 界首市| 武城县| 龙胜| 林周县| 寿宁县| 瓦房店市| 扎鲁特旗| 平阳县| 阆中市| 格尔木市| 山阳县| 邮箱| 和田市| 马尔康县| 皮山县| 大竹县| 报价| 米林县| 如东县| 仁化县| 公安县| 昌图县| 隆林| 依兰县| 宕昌县| 萝北县| 宝坻区| 惠来县| 六安市| 邳州市| 马公市| 东莞市| 杂多县|