所以我想我不能在 % 中设置高度,如果父级没有设置高度,即使其中一个孩子有静态高度并且正在拉伸父级,但现在我意识到如果孩子是绝对位置,它可以。为什么位置绝对子级可以有 % 高度,而块、内联或浮动子级不能?http://jsfiddle.net/4vmeqf16/
body > div{
background: black;
}
div div:nth-child(1){
width: 50px;
height: 50px;
background: red;
}
div div:nth-child(2){
width: 20%;
height: 20%;
background: green;
}
div div:nth-child(3){
width: 20%;
height: 20%;
position: absolute;
background: yellow;
}
<div>
<div></div>
<div></div>
<div></div>
</div>
因为position:absolute
元素对其包含块的高度没有影响。因此,与其他情况不同,没有高度循环依赖性。包含块可以使用其流入子元素进行布局,其高度已确定,然后才需要布局绝对定位元素。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句