【发布时间】:2016-04-07 01:00:18
【问题描述】:
在以下示例中,绝对子 div 包含在相对父级中。父 div 之前的 div 有条件地显示和隐藏,因此无论标题是否显示,父 div 都应与顶部对齐。
问题是在显示header的时候,即使滚动也隐藏了子div的内容(滚动条的底部看不到)。子 div 的底部如何成为父 div 的底部(在这种情况下是浏览器窗口)?
html {
height: 100%;
margin: 0;
overflow: hidden;
}
body {
margin: 0;
height: 100%;
}
#parentDiv
{
position:relative;
background: yellow;
height: 100%;
}
#childDiv
{
position:absolute;
left:50px;
top:20px;
bottom: 0;
background: blue;
overflow: auto;
}
<div id='header'>This header causes childDiv to scroll outside of the browser window (bottom scroll thumb isn't visible).
This toggles so parentDiv should adjust up when not visible.
</div>
<div>
<div id='parentDiv'>
This is the parent div
<div id='childDiv'>
This is the child div <br>
This is the child div <br>
(Repeat to extend beyond browser window and cause a scroll bar to be displayed)
...
This is the child div <br>
</div>
</div>
</div>
【问题讨论】:
-
那是因为
#parentDiv的高度是100%,但是他的父母没有高度,所以#parentDiv的高度只能由他的内容This is the parent div设置。例如,如果您将#parentDiv的高度设置为350px,您将看到#childDiv的内容。 -
我说错了。内容没有隐藏——只是标题大小的内容被隐藏了。如何在不明确考虑标题高度的情况下显示(或滚动)所有子 div。标题的内容可能会有所不同,从而使高度动态化。它可以显示或隐藏。