请帮我解决这个任务。我们有3个div区块。第一个div的高度设置为100px(但在现实世界中它是动态值)。第二块有固定的高度,并有另一块作为孩子。子块应该有高度向下延伸到屏幕底部。但由于我们的第二块是相对的,底部:0将意味着第二块的底部。这种情况下的最佳做法是什么,请纯粹使用CSS?将全高绝对元素定位在相对一个里面
body > div { height: 100px; }
.first { background: lightblue; }
.second {
background: lightgreen;
position: relative;
}
.second div {
position: absolute;
background: pink;
width: 50%;
height: 200px;
top: 100px;
}
<div class="first">The height of the block may vary greately.</div>
<div class="second">
<div>This DIV should take whole free space to the bottom of the screen.</div>
</div>
UPD:
我可以通过缠绕第二个div成额外的div达到的效果(宽度位置的绝对和底部:0)和具有透明背景离开它,但随后静态“背后的内容”变得无法使用。这里是an example。
哪个div应该占用剩余空间?第二个div或第二个div的孩子?孩子的身高:200px。我认为第二将采取? – jmag
@jmag孩子应该有剩余的空间。 200px是一个随机数字。 – Mike
为什么你不能在同一级别拥有3个div,而不是嵌套? – Stickers