CSS Sticky footers 布局设计

在网页设计中,我们经常遇到的布局场景:如果页面内容不够长的时候,页脚块粘贴在视窗底部;如果内容足够长时,页脚块会被内容向下推送。如下图:

sticky footer 布局简易图

需求描述:无论A容器中的内容如何增长,X都会紧紧粘贴在A模块的底部。

以下是在实际运用中,我所使用的一种比较简单易懂的code方式。

<body>
    <div class="content-wrapper clearfix">
        <div class="content-main">
            This plugin will cause hashes to be based on the relative path of the module, generating a four character string as the module id. Suggested for use in production.
        </div>
    </div>
    <div class="close">X</div>
</body>

首先,给根元素设置样式

html, body {
  height: 100%; // **必需code**
  overflow: auto;
}

其次,给高度不固定的内容包裹器content-wrapper添加样式,
⚠️ content-wrapper容器还是清除浮动的,我们加上清除浮动样式clearfix及其伪类:

.clearfix{ // **必需code**
    display : inline-block;
}
.clearfix:after{ // **必需code**
    display: block;
    content : ".";
    height : 0;
    line-height : 0;
    clear: both;
    visibility : hidden;
}
.content-wrapper{
    min-height : 100%; // **必需code**
}

再次,我们给它的变化内容content-main添加样式

.content-main{
    margin : 64px 10px 0 10px; 
    padding-bottom : 64px; // **必需code**
}

最后,我们给粘贴块close添加样式
⚠️ close是和content-wrapper 并列的

.close{
    position : relative;
    width : 40px;
    height : 40px;
    margin : -64px auto 20px auto; 
    // **margin-top: -64px 是必需的**
    clear : both;
    font-size : 32px;
    border: 1px solid #000;
    text-align: center;
}

此时,如果动态内容没有超过一屏幕时,展现如下

动态内容较少时的展示

内容比较多的时候显示如下图:

动态内容较多的情况

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值