【发布时间】:2016-09-01 05:14:25
【问题描述】:
如何设计 HTML/CSS 结构,将固定大小的容器水平分成三部分。第一部分应该与内容需要一样高。第二部分和第三部分将共享剩余的空间 550——不管它们的内容如何。如果其内容的大小超过此限制,则该部分应该是可滚动的。
其中的 HTML 部分很简单:一个 div 容器,其中包含三个 div 作为子级。
我尝试使用 flexbox 解决这个问题 - 但是可能有更好的选择:
css部分:
#container {
position: absolute;
width: 100%; height: 100%;
display: flex;
flex-direction: column;
}
#item1 { flex: 0 0 auto; }
#item2 { flex: 1 1 auto; }
#item3 { flex: 1 1 auto; }
不幸的是,这仅在第 2 项或第 3 项的内容不太大时才有效。
有关问题的更详细实现,请参阅this fiddle。
body {
margin: 0;
overflow: hidden;
}
* {
box-sizing: border-box;
}
#container {
position: absolute;
width: 100%;
height: 100%;
border: 2px solid green;
display: flex;
flex-direction: column;
}
#item1 {
flex: 0 0 auto;
background-color: Bisque;
}
#item2 {
flex: 1 1 auto;
background-color: DarkOrange;
}
#item3 {
flex: 1 1 auto;
background-color: MediumAquaMarine;
}
<div id="container">
<div id="item1">I'll be as tall as my content takes.</div>
<div id="item2">From the rest, I'll take exactly 50%. No matter how short or long my content is. If needed there should be scrollbars.</div>
<div id="item3">I'll take the other 50% of the rest!
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
<br>right?
</div>
</div>
【问题讨论】: