1. 程式人生 > 其它 >粘性(sticky)底部的幾種實現方式

粘性(sticky)底部的幾種實現方式

html結構
<body>
    <div class="content">
    </div>
    <div class="footer">
    </div>
</body>
1. 負margin拉動
.content{
    min-height: 100%;
    margin-bottom: -50px;
}
.footer{
    height: 50px;
}
.content{
    min-height: 100vh;
}
.footer{
    height: 50px;
    margin-top
: -50px; }
2. calc 計算
.content{
    min-height: calc(100vh - 50px);
}
.footer{
    height: 50px;
}
3. flex 自適應
body{
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}
.conent{
    flex: 1;
}
.footer{
    height: 50px;
}
4. grid 自適應
body {
    min-height: 100vh;
    display: grid
; grid: 1fr auto; } .footer{ grid-row: 2 / 3 }
5. 絕對定位
body{
    height: 100%;
}
.content{
    padding-bottom: 100px;
}
.footer{
    height: 100px;
    position: absolute;
    bottom: 0;
}
5. sticky 佈局 (已經成熟)
.content{
    min-height: 100vh;
    padding-bottom: 100px;
}
.footer{
    height: 100px;
    position
: sticky; bottom: 0; }
1.父元素及祖先元素不能overflow:hidden或者overflow:auto屬性。 2.必須指定top、bottom、left、right4個值之一,否則只會處於相對定位 3.父元素的高度不能低於sticky元素的高度 4.sticky元素僅在其父元素內生效
position:sticky 粘性定位的幾種巧妙應用