How can a HTML/CSS structure be designed, that splits a fixed size container horizontally into three parts. The first part should be as tall as it's content needs. The second and the third part would share the remaining space fifty-fifty - regardless of their contents. If the size of their contents exceeds this limitation, the section should be scrollable.
如何设计HTML / CSS结构,将固定大小的容器水平分成三个部分。第一部分应该与内容需求一样高。第二部分和第三部分将共享剩下的空间五十五 - 无论其内容如何。如果其内容的大小超出此限制,则该部分应该是可滚动的。
The HTML part of it is trivial: A div
container with three div
's as children.
它的HTML部分是微不足道的:一个div容器,有三个div作为子项。
I tried to solve this with flexbox - however there might be a better choice for this:
我尝试使用flexbox解决这个问题 - 但是可能有更好的选择:
The css part:
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; }
Unfortunately this only works if the contents of item 2 or 3 is not too large.
不幸的是,只有当项目2或3的内容不是太大时,这才有效。
See this fiddle for a bit more detailed implementation of the problem.
请参阅此小提琴,了解问题的更详细实现。
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>
1 个解决方案
#1
1
Yes, you can use flex. Here's a little improvement for your code. Item1 doesn't need to have a flex rule and item2 and item3 will have flex: 1
.
是的,你可以使用flex。这是对您的代码的一点改进。 Item1不需要具有flex规则,item2和item3将具有flex:1。
I also added the overflow-y: auto;
rule to make it scrollable.
我还添加了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;}
#1
1
Yes, you can use flex. Here's a little improvement for your code. Item1 doesn't need to have a flex rule and item2 and item3 will have flex: 1
.
是的,你可以使用flex。这是对您的代码的一点改进。 Item1不需要具有flex规则,item2和item3将具有flex:1。
I also added the overflow-y: auto;
rule to make it scrollable.
我还添加了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;}