Sticky-footer 布局

在一般的页面中,footer应该是置于内容底部,如果内容高度不足,也应该让footer紧贴底部

demo

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no">
    <title>Document</title>
    <style>
        *{margin: 0;padding: 0;}
        body,html{height: 100%;}
        .detail{height:100%;background:#ddd;width: 100%;overflow:auto;}
        .detail-wrapper{width:100%;min-height: 100%;}
        .detail-main{margin-top: 64px;padding-bottom: 64px;}
        p{word-wrap: break-word;}
        .clearfix{display: inline-block;}
        .clearfix:after{
            display: block;content: '';height: 0;line-height: 0;clear: both;visibility: hidden;
        }

        .footer{text-align:center;margin:-64px auto 0;width: 32px;}
    </style>
</head>
<body>
<div class="detail"><!-- 首先需要让最外层容器的宽高为100%,并且可以随内容高度自动滚动 -->
  <div class="detail-wrapper clearfix"><!-- 内层容器我们需要设置的是最小高度为100%,目的是让内容不足时,也至少能充满整个屏幕 -->
      <div class="detail-main"><!-- 这一层容器的主要作用是为footer预留空间 -->
        <p>123</p>
        <p>123</p>
        <p>123</p>
        <p>123</p>
        <p>123</p>
        <p>123</p>
        <p>123</p>
        <p>123</p>
        <p>123</p>
        <p>123</p>
        <p>123</p>
        
    
      </div>
    
  </div>
  <div class="footer"><!-- 这里需要让footer的往上移动,填充到上面为footer预留的位置上 -->
    footer
    </div>
    
</body>
</html>

使用flex布局

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1,minimum-scale=1,user-scalable=no">
    <title>Document</title>
    <style>
        *{margin: 0;padding: 0;}
        body,html{height: 100%;}
        .wrap{display: flex;flex-direction: column;min-height: 100%;}
        .content{flex: 1;}
        .footer{text-align:center;flex: 0;}
    </style>
</head>
<body>
    <div class="wrap">
        <div class="content">
            <p>123</p>
            <p>123</p>
            <p>123</p>
            <p>123</p>
            <p>123</p>
            <p>123</p>
            <p>123</p>
            <p>123</p>
            <p>123</p>
            <p>123</p>
        </div>
        <div class="footer">footer</div>
    </div>
    
</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Sticky footer布局是什么? 我们所见到的大部分网站页面,都会把一个页面分为头部区块、内容区块和页脚区块...
    陌路黄昏后阅读 498评论 0 7
  • 1. position: fixed实现 ①顶栏和③底栏都设置position:fixed,分别置于页面的顶部和底...
    自度君阅读 1,133评论 0 2
  • 在网页设计中,sticky footer设计是最古老的和常见的效果之一,大多数人都应该经历过。它可以概括如下:当页...
    放飞吧自我阅读 512评论 0 0
  • 单色果实
    Passport111阅读 154评论 0 0
  • 责任是从现在开始就要承担的,父母不再年轻,能回报的时候及时回报,不要总觉得时间还很多,岁月不等人。 在岁月中跋...
    洋氵羊阅读 267评论 0 0