footer.php置底,CSS五种方式实现Footer置底

本文介绍了实现页脚置底的四种常见方法,包括通过负margin、padding调整、calc()函数计算以及使用Flexbox和Grid布局。每种方法都有其适用场景,例如内容区域高度不固定时,Flexbox和Grid布局能更好地适应变化。这些技术对于前端开发者优化网页布局至关重要。
摘要由CSDN通过智能技术生成

页脚置底(Sticky footer)就是让网页的footer部分始终在浏览器窗口的底部。

当网页内容足够长以至超出浏览器可视高度时,页脚会随着内容被推到网页底部;

但如果网页内容不够长,置底的页脚就会保持在浏览器窗口底部。

662b612076df153b02073e92399089c1.png

方法一:将内容部分的margin-bottom设为负数

html, body {

margin: 0;

padding: 0;

height: 100%;

}

.wrapper {

min-height: 100%;

margin-bottom: -50px; /* 等于footer的高度 */

}

.footer, .push {

height: 50px;

}

这个方法需要容器里有额外的占位元素(div.push)。

div.wrapper的margin-bottom需要和div.footer的-height值一样,注意是负height。

方法二:将页脚的margin-top设为负数

给内容外增加父元素,并让内容部分的padding-bottom与页脚的height相等。

html, body {

margin: 0;

padding: 0;

height: 100%;

}

.content {

min-height: 100%;

}

.content-inside {

padding: 20px;

padding-bottom: 50px;

}

.footer {

height: 50px;

margin-top: -50px;

}

方法三:使用calc()设置内容高度

.content {

min-height: calc(100vh - 70px);

}

.footer {

height: 50px;

}

这里假设div.content和div.footer之间有20px的间距,所以70px=50px+20px

方法四:使用flexbox弹性盒布局

以上三种方法的footer高度都是固定的,如果footer的内容太多则可能会破坏布局。

html {

height: 100%;

}

body {

min-height: 100%;

display: flex;

flex-direction: column;

}

.content {

flex: 1;

}

方法五:使用Grid网格布局

html {

height: 100%;

}

body {

min-height: 100%;

display: grid;

grid-template-rows: 1fr auto;

}

.footer {

grid-row-start: 2;

grid-row-end: 3;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值