粘性(sticky)底部的幾種實現方式
阿新 • • 發佈:2021-12-17
html結構
position:sticky 粘性定位的幾種巧妙應用
<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-top2. calc 計算: -50px; }
.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: grid5. 絕對定位; grid: 1fr auto; } .footer{ grid-row: 2 / 3 }
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; position1.父元素及祖先元素不能overflow:hidden或者overflow:auto屬性。 2.必須指定top、bottom、left、right4個值之一,否則只會處於相對定位 3.父元素的高度不能低於sticky元素的高度 4.sticky元素僅在其父元素內生效: sticky; bottom: 0; }
position:sticky 粘性定位的幾種巧妙應用