sticky footer布局

很有意思的布局方式,很实用,总有需求遇到说footer固定在页面下方,内容过长时跟着向下推送。
而sticky footer布局就是解决这个问题。这个名词很有意思,想自己记一下,就像是黏在底部的footer,自己想象着玩的哈

html

<body>
     <!-- 注意点: wrap和footer是平级的,并且wrap要加clearfix清除浮动 -->
     <div class="wrap clearfix">
         <!-- 注意点: content的padding-bottom和footer的高度一样高 -->
         <div class="content">
             <p>我是内容</p>
         </div>
     </div>
     <!-- 注意点: footer的margin-top的高度是footer的高度取负值 -->
     <footer>我是底部</footer>
 </body>

css

<style type="text/css">
     body,html {
         height: 100%;
     }
     * {
         margin: 0;
         padding: 0;
     }
     .wrap {
         min-height: 100%;
     }
     .clearfix {
         display: inline-block;
         zoom: 1;
     }
     .clearfix:after {
         content: '';
         display: block;
         height: 0px;
         clear: both;
         visibility: hidden;
     }
     .content {
         padding-bottom: 80px;
     }
     footer {
         position: relative; 
         clear: both;
         height: 80px;
         margin-top: -80px; /* footer高度的负值 */
         background: deeppink;
     }
 </style>

第二种方法

使用flex布局,这个使用起来代码量少,但是可能有兼容性,使用时应有取舍。

html

<div class="box">
    <!-- 注意点: wrap和footer是平级的 -->
    <div class="wrap clearfix">
        <!-- 注意点: content的padding-bottom和footer的高度一样高 -->
        <div class="content">
            <p>我是内容</p>
        </div>
    </div>
    <!-- 注意点: footer的margin-top的高度是footer的高度取负值 -->
    <footer>我是底部</footer>
</div>

css

.box {
      height: 100%;
      display: flex;
      flex-direction: column;
  }
  .wrap {
      flex: 1;
  }
  footer {
      height: 60px;  /* 或者flex:0 */
  }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值