CSS sticky footer布局

Sticky Footer布局: 当页面中的内容高度小于屏幕高度的时候, 让底部footer显示在底部, 当内容高度大于屏幕高度的时候, 底部footer会紧随内容其后, 滚动内容区域,footer会在内容的最后方显示。

利用flex布局实现sticky footer布局

<!DOCTYPE html>
<html>
<head>
    <title>CSS Sticky footer</title>
    <style type="text/css">
        body{
            display: flex;
            flex-flow: column;
            min-height: 100vh;
            overflow: auto;
        }
        h1{
            font-size: 60px;
            text-align: center;
        }
        p{
            font-size: 24px;
            text-align: center;
        }
        .main{
            flex: 1
        }
    </style>
</head>
<body>
    <header>
        <h1>Site name</h1>
    </header>
    <div class="main">
        <p>this is site content about the site description</p>
        <p>this is site content about the site description</p>
        <p>this is site content about the site description</p>
        <p>this is site content about the site description</p>
        <p>this is site content about the site description</p>
        <p>this is site content about the site description</p>
        <p>this is site content about the site description</p>
    </div>
    <footer>
        <p>©️ 2018</p>
        <p>made whith XXXX</p>
    </footer>
</body>
</html>

利用定位和负margin的方法实现

<!DOCTYPE html>
<html>
<head>
    <title>CSS sticky footer</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <style type="text/css">
        *{
            margin: 0;
            padding: 0;
        }
        /*实现css sticky footer布局的主要样式*/
        .detail{
            position: fixed;
            overflow: auto;
            width: 100%;
            height: 100%;
        }
        .wrapper{
            min-height: 100%;
            width: 100%;
        }
        .main{
            margin-top: 64px;
            padding-bottom: 44px; /* padding-bottom要和下面的footer的高度保持一致以保证footer能够显示出来 */
        }
        .footer{
            margin: -44px auto 0 auto;  /* margin-top 设置向上偏移,用来和main的padding-bottom相抵消保证main里面的内容不够占满全屏的时候让footer显示在页面的底部 */
        }
        .clearfix::after{
            display: block;
            height: 0;
            content: " ";
            clear: both;
            visibility: hidden;
        }
        /* 辅助样式 为了使页面美观一点,可以忽略 */
        .title h1{
            font-size: 60px;
            text-align: center;
        }
        .main p{
            font-size: 24px;
            text-align: center;
        }
        .footer p{
            text-align: center;
        }
    </style>
</head>
<body>
    <div class="detail">
        <div class="wrapper clearfix">
            <div class="title">
                <h1>Site name</h1>
            </div>
            <div class="main">
                <p>this is content about the site </p>
                <p>this is content about the site </p>
                <p>this is content about the site </p>
                <p>this is content about the site </p>
            </div>
        </div>
        <div class="footer">
            <p>©️ XXX</p>
            <p>made XXX</p>
        </div>
    </div>
</body>
</html>

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值