Geo*_*rge 11 html javascript css jquery flexbox
我试图在某些元素的剩余空间中设置一个垂直可滚动的 div。它们都没有固定的高度。
我得到的只是一个可以根据其内容进行调整的 Flexbox 元素,但我真正需要的是该元素填充空白空间,并且其内容只是滚动到那里。
超文本标记语言
<div class="wrapper-1">
<div>some stuff</div>
<div>some other stuff</div>
</div>
<div class="wrapper-2">
<div>more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more and more stuff</div>
</div>
<div class="wrapper-3">
<div id="header">
<div>My</div>
<div>Header than can grow</div>
</div>
<div id="content">
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
CSS
html, body {
margin: 0;
height: 100%;
}
.wrapper-1 {
background: white;
}
.wrapper-2 {
background: lime;
}
.wrapper-3 {
display: flex;
flex-direction: column;
}
#header {
background: #edc;
flex: 1 1 40px;
}
#content {
flex: 1 1 auto;
background: yellow;
overflow: auto;
}
#content div {
min-height: 50px;
}
Run Code Online (Sandbox Code Playgroud)
#content 的高度应为从 #header 到页面底部的高度。
完美的解决方案是仅使用 CSS,但替代的 JS 解决方案可能会有所帮助,问题是即使在调整浏览器窗口大小时这也必须起作用。
小智 3
查看以下小提琴中的解决方案,只需确保将窗口大小调整得更大,以便滚动条可以出现。 这个小提琴的解决方案,点击这里
即使标题变大,标题仍然适合其大小,无需指定其大小,内容 div 将填充剩余空间。
HTML、CSS
html, body {
margin: 0;
height: 100%;
}
.all-wrapper{
height: 100%;
display:flex;
flex-direction:column;
}
.wrapper-1 {
background: white;
}
.wrapper-2 {
background: lime;
}
.wrapper-3 {
flex: 1;
display: flex;
flex-direction: column;
overflow:hidden;
}
#header {
background: #edc;
}
#content {
flex: 1;
background: yellow;
overflow: auto;
}
#content div {
min-height: 50px;
}Run Code Online (Sandbox Code Playgroud)
<div class="all-wrapper">
<div class="wrapper-1">
<div>some stuff</div>
<div>some other stuff</div>
</div>
<div class="wrapper-2">
<div>more and more and more and more and more and more more stuff</div>
</div>
<div class="wrapper-3">
<div id="header">
<div>My</div>
<div>Header than can grow</div>
</div>
<div id="content">
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
<div>My Content that will fill remaining space untill page has to scroll</div>
</div>
</div>
</div>Run Code Online (Sandbox Code Playgroud)