【发布时间】:2021-01-23 19:51:59
【问题描述】:
我有一个非常简单的页面设置,使用弹性框的方式如下:
蓝色 div 应该占高度的 25%,紫色 div 应该占 75%。如果蓝色div 中有太多行,它应该保持相同大小并显示滚动条。这适用于几行,但在某些时候中断,蓝色div 溢出并长成紫色。我是 flexbox 的新手,所以我真的不明白为什么会这样。不使用弹性盒会更好吗?感谢您在这一点上的任何提示或指针。
这是我使用的代码(整页运行):
function lines(noLines) {
var text = "line</br>".repeat(noLines);
document.getElementById("lower").innerHTML = text;
}
* {
box-sizing: border-box;
}
.body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
#static1 {
height: 30px;
width: 100%;
background-color: red;
}
#static2 {
height: 40px;
width: 100%;
background-color: orange;
}
#content {
display: flex;
flex: 1;
}
#left {
width: 40%;
background-color: yellow;
}
#right {
width: 60%;
display: flex;
flex-direction: column;
}
#upper {
flex: 3 0;
background-color: violet;
}
#lower {
flex: 1;
background-color: blue;
overflow: auto;
}
<div class="body">
<div id="static1">Some static div</div>
<div id="static2">Another static div. Flexbox below fills rest of remaining screen.</div>
<div id="content">
<div id="left">
Left part, fixed width in percentage.</br>
Click to enter lines into the bottom right:</br>
<button onclick=lines(20)>Few Lines</button>
<button onclick=lines(200)>Many Lines</button>
</div>
<div id="right">
<div id="upper">Flexbox with flex=3.</div>
<div id="lower">Flexbox with flex=1.</div>
</div>
</div>
</div>
【问题讨论】: