在知乎上看到下面的題目:
有一個(gè)高度自適應(yīng)的div,里面有兩個(gè)div翔曲,一個(gè)高度100px迫像,希望另一個(gè)填滿(mǎn)剩下的高度。(外層div自適應(yīng)于它的父級(jí)高度)
覺(jué)得挺有意思的瞳遍,想到了六種方法:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
* {
padding: 0;
margin: 0;
}
.box1 {
height: 100px;
background: red;
}
.box2 {
background: green;
}
.wrap {
height: 100vh;
}
.flex-wrap {
display: flex;
flex-direction: column;
}
.flex-wrap > .box2 {
flex: 1;
}
.position-wrap {
position: relative;
}
.position-wrap > .box2 {
position: absolute;
top: 100px;
bottom: 0;
left: 0;
right: 0;
}
.calc-wrap > .box2 {
height: calc(100vh - 100px);
}
.grid-wrap {
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 100px 1fr;
}
.border-box-wrap {
box-sizing: border-box;
padding-top: 100px;
position: relative;
}
.border-box-wrap > .box1 {
position: absolute;
top: 0;
width: 100%;
}
.border-box-wrap > .box2 {
height: 100%;
}
.margin-wrap {
box-sizing: border-box;
padding-top: 100px;
}
.margin-wrap > .box1 {
margin: -100px 0 0 0;
}
.margin-wrap > .box2 {
height: 100%;
}
</style>
</head>
<body>
<!--有一個(gè)高度自適應(yīng)的div闻妓,里面有兩個(gè)div,一個(gè)高度100px掠械,
希望另一個(gè)填滿(mǎn)剩下的高度(外層div自適應(yīng)于它的父級(jí)高度)由缆。-->
方法一注祖、 flex方法
實(shí)現(xiàn)原理:該方法主要部分就是使用了flex:1屬性來(lái)實(shí)現(xiàn)box2的自動(dòng)伸縮
注意點(diǎn):注意兼容性
<!--<section>-->
<!--<div class="wrap flex-wrap ">-->
<!--<div class="box1 ">box1 flex</div>-->
<!--<div class="box2 ">box2 flex</div>-->
<!--</div>-->
<!--</section>-->
方法二 、絕對(duì)定位方法
實(shí)現(xiàn)原理:box2采用absolute 絕對(duì)定位后均唉,再使用bottom:0屬性是晨,會(huì)使得其緊貼其包裹的元素的底部。
(準(zhǔn)確說(shuō)是其含有position:relative的父級(jí)或以上的包裹元素 )
注意點(diǎn):父級(jí)元素別忘記使用position:relative舔箭,否則將定位至html上
<!--<section>-->
<!--<div class="wrap position-wrap ">-->
<!--<div class="box1 ">box1 position</div>-->
<!--<div class="box2" >box2 position</div>-->
<!--</div>-->
<!--</section>-->
方法三罩缴、 calc方法
實(shí)現(xiàn)原理:就是通過(guò)計(jì)算結(jié)果設(shè)置高度
注意點(diǎn):注意兼容性
<!--<section>-->
<!--<div class="wrap calc-wrap ">-->
<!--<div class="box1">box1 calc</div>-->
<!--<div class="box2">box2 calc</div>-->
<!--</div>-->
<!--</section>-->
方法四、 grid方法
實(shí)現(xiàn)原理:利用網(wǎng)格布局中的fr單位
注意點(diǎn):注意兼容性
<!--<section>-->
<!--<div class="wrap grid-wrap ">-->
<!--<div class="box1">box1 grid</div>-->
<!--<div class="box2">box2 grid</div>-->
<!--</div>-->
<!--</section>-->
方法五 层扶、border-box + 絕對(duì)定位方法
實(shí)現(xiàn)原理:由于父元素被設(shè)置成了IE盒模型箫章,且父元素的padding-top與子元素box1的高度是一樣的,
因此絕對(duì)定位的子元素box1屬性設(shè)為top:0時(shí)镜会,其自然而然就處于父元素的padding-top區(qū)域了檬寂。
(高度相等 正好遮蓋padding-top),而非絕對(duì)定位的子元素box2的高度設(shè)為100%時(shí)稚叹,
其高度就是父元素盒模型中的content里的高度(不包含padding及margin值)焰薄,因此正好也就填充了
剩余空間了。
注意點(diǎn):父元素記得設(shè)置position:relative及box-sizing:border-box
<!--<section>-->
<!--<div class="wrap border-box-wrap ">-->
<!--<div class="box1">box1 border-box</div>-->
<!--<div class="box2">box2 border-box</div>-->
<!--</div>-->
<!--</section>-->
方法六 border-box + 負(fù)margin方法
實(shí)現(xiàn)原理:有部分跟方法五有點(diǎn)類(lèi)似扒袖,都是設(shè)置父元素為IE盒模型塞茅,且其padding-top為100px,
非絕對(duì)定位的子元素box2高度為100%季率;負(fù)margin-top的子元素box1為100px野瘦,導(dǎo)致其會(huì)整體上移100px,
也就正好遮擋了父元素的padding-top部分了飒泻。
注意點(diǎn):父元素記得設(shè)置box-sizing:border-box
<section>
<div class="wrap margin-wrap ">
<div class="box1">box1 負(fù)margin</div>
<div class="box2">box2 負(fù)margin</div>
</div>
</section>
</body>
</html>
在線(xiàn)展示:https://codepen.io/anon/pen/vpNPRQ