sticky footer

一种完美的css绝对底部

手机端网页有时候会遇到这种布局要求:当内容很少时,footer位于窗口底部,当内容占满整个屏幕时,footer跟在内容末尾,这时候我们就可以用sticky footer

布局前提:footer要和主体内容div同级

        <div class="main-wrap">
            <div class="main-content  clearfix">  <!--clearfix清楚内容区域里的浮动-->
                <p>我是内容区域</p>
                <p>我是内容区域</p>
                <p>我是内容区域</p>
            </div>
        </div>
        <div class="footer">
            我是底部内容
        </div>

样式:

          * {
               margin: 0;
               padding: 0;
           }
           html,body {
               height: 100%;
           }
           .clearfix:after {
               content: '';
               display: block;
               height: 0;
               line-height: 0;
               clear: both;
               overflow: hidden;
               visibility: hidden;
           }
           .main-wrap {
               min-height: 100%;//设置最小高度
               background: #ccc;
           }
           .main-wrap .main-content {
               padding-bottom: 40px;//必须和footer高度相同
           }
           .footer {
               position: relative;
               z-index: 100;
               height: 40px;
               margin:-40px auto 0 auto;//matgin-top必须和footer高度相同    
               clear:both;
               line-height: 40px;
               text-align: center;
               background: rgba(0,0,0,.5);
               color: #fff;
           }

参考:CSS Sticky Footer: 完美的CSS绝对底部

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容