如何设计 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>
最佳答案
是的,你可以使用 flex。这是对您的代码的一些改进。 Item1 不需要有 flex 规则,item2 和 item3 将有 flex: 1
。
我还添加了 overflow-y: auto;
规则以使其可滚动。
#item1 {background-color: Bisque ; }
#item2 { flex: 1; background-color: DarkOrange ; overflow-y: auto;}
#item3 { flex: 1; background-color: MediumAquaMarine ; overflow-y: auto;}
关于html - 平均分割固定大小容器的空间。 flex 盒子的案例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39258903/