css高度设置占满页面,《CSS》关于高度铺满的问题

场景一:

我想让这个div铺满整个页面。

方法一:

先让根元素和body铺满,再然div铺满,这样div就会继承body的高度了。

:root,

body {

height: 100%;

}

.wrap {

width: 100%;

height: 100%;

background-color: skyblue;

}

方法二:

让div脱离文档流,再铺满

.wrap {

width: 100%;

height: 100%;

background-color: skyblue;

position: fixed;

}

方法三:

让div脱离文档流,再吸收剩余空间

.wrap {

background-color: skyblue;

position: absolute;

left: 0;

top: 0;

right: 0;

bottom: 0;

}

场景二:

.box1 { width: 100%; height: 60px; background: red; }

在场景一的基础上(wrap满屏),box1 已知高度,我想让 box2 撑满剩余空间

方法一:

让 box2 脱离文档流,top隔开60px,再吸收剩余空间

.box2 {

position: absolute;

left: 0;

top: 60px;

right: 0;

bottom: 0;

background-color: plum;

}

方法二:

利用 calc 计算

.box2 {

position: absolute;

left: 0;

top: 60px;

width: 100%;

height: calc(100% - 60px);

background-color: plum;

}

方法三:

利用弹性盒

.wrap {

background-color: skyblue;

position: absolute;

width: 100%;

height: 100%;

display: flex;

flex-direction: column; /* 纵向排列 */

}

.box1 {

width: 100%;

height: 60px;

background-color: red;

flex: 0 0 1; /*不伸不缩,基本值占1份(60px) */

}

.box2 {

background-color: plum;

flex: 1; /* 吸收剩余空间 */

}

场景三:

Lorem ipsum dolor sit

box1 高度不知道(由内容决定),要求 box2 撑满剩余空间。

依然可以使用场景二的方法二(弹性盒)解决

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值