css sticky footer实现,css sticky footer经典布局

本文介绍了两种实现StickyFooter布局的方法:使用Flex弹性盒子布局和通过padding-bottom与负margin-top组合。这两种方法都能确保页脚始终粘附于屏幕底部。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

什么是sticky footer布局?

一般指手机页面中,当内容高度撑不满一屏时,页脚紧贴屏幕底部;当内容高度超过一屏时,页脚紧随其后。

1460000021883258

方法一:flex弹性盒子布局

父容器container的display为flex,并规定项目排列顺序是纵向的

content元素的flex为1,即有多余空间就增大

footer定义一个高度

内容

body {

margin: 0;

}

.container {

display: flex;

flex-direction: column;

min-height: 100vh;

}

.content {

flex: 1;

/*非必须*/

width: 100%;

height: 300px;

line-height: 300px;

text-align: center;

color: #fff;

font-size: 30px;

font-weight: bold;

background-color: #71a8da;

/*非必须*/

}

.footer {

height: 60px;

/*非必须*/

width: 100%;

line-height: 60px;

text-align: center;

color: #fff;

font-size: 30px;

font-weight: bold;

background-color: #f85f2f;

/*非必须*/

}

方法二:padding-bottom+负margin-top

容器wrapper需要指定min-height为100vh(vh:视窗高度)

内容写在content容器里,指定padding-bottom为footer容器的高度

footer指定高度和margin-top,并且margin-top为高度的负值

内容

body {

margin: 0;

}

.wrapper {

width: 100%;

min-height: 100vh;

}

.content {

/*padding-bottom应等于footer的高度*/

padding-bottom: 60px;

/*非必须*/

width: 100%;

height: 400px;

line-height: 400px;

text-align: center;

color: #fff;

font-size: 30px;

font-weight: bold;

background-color: #71a8da;

/*非必须*/

}

.footer {

/*margin-top应等于footer高度的负值*/

margin-top: -60px;

height: 60px;

/*非必须*/

width: 100%;

line-height: 60px;

text-align: center;

color: #fff;

font-size: 30px;

font-weight: bold;

background-color: #f85f2f;

/*非必须*/

}

提示:两个/**非必须**/之间的部分不是实现sticky footer布局的必要代码,只是一些辅助样式,可以删除

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值