实现移动端的 css stickyfooter

描述:在移动端经常有这样的效果,当页面内容高度不足视口高度时footer固定在底部,而页面内容高度高于视口高度时,footer随内容向下移动。

方法一: 使用 calc 实现(不推荐)
// html
<body>
  <div class="container">
    <div class="header">header</div>
    <div class="main">main</div>
    <div class="footer">footer</div>
  </div>
</body>
// css
.main {
  min-height: calc(100vh - header高度 - footer高度)
  box-sizing: border-box; // 如果main有设置padding和border则须设置
}
方法二:使用 flex 布局(推荐)
// css
.container {
  min-height: 100vh; /*高度最小为视口的高度*/
  display: flex;
  flex-direction: column;
}
.main {
  flex: 1;
}
方法三:处理兼容性

缺点:必须指定 footer 的高度

// css
 body, html {
   height: 100%;
 }
 .container {
   height: 100%;
 }
 .main {
   min-height: 100%;
   margin-bottom: -50px;
 }
 .footer {
   height: 50px;
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值