问题:当父元素没有height,而且被子元素撑大,另外的子元素的height百分比不起作用
代码示例:
<style>
.parent {
width: 300px;
background-color: aqua;
display: flex;
}
.child1 {
width: 100px;
height: 300px;
background-color: bisque;
}
.child2 {
width: 100px;
height: 100%;
background-color: brown;
}
</style>
<body>
<div class="parent">
<div class="child1"></div>
<div class="child2"></div>
</div>
</body>
看一下显示的效果:发现child2虽然有height:100%,但是实际高度是0px,说明height:100%未生效;
解决方案:可以用position去将父元素作为参照物
代码示例:
<style>
.parent {
width: 300px;
background-color: aqua;
display: flex;
position: relative;
}
.child1 {
width: 100px;
height: 300px;
background-color: bisque;
}
.child2 {
width: 100px;
height: 100%;
background-color: brown;
left: 100px;
position: absolute;
}
</style>
<body>
<div class="parent">
<div class="child1"></div>
<div class="child2"></div>
</div>
</body>
看一下显示的效果:发现child2的height:100%生效了