【发布时间】:2017-03-17 09:20:49
【问题描述】:
我想制作一个盒子(在本例中为弹性项目),它始终位于其容器的中间。在那个框中,有一个页眉、页脚和内容部分。如果内容的高度变得太大,我希望内容部分可以滚动。页眉和页脚应始终可见,并且框应始终保留在其容器中。
这是我能写的:
HTML
<div class="flex-container">
<div class="flex-item">
<header>Header</header>
<div class="content">
A
<br>B
<br>C
<br>D
<br>E
<br>F
<br>G
<br>H
<br>I
<br>J
<br>K
</div>
<footer>Footer</footer>
</div>
</div>
CSS
body {
margin: 120px;
}
.flex-container {
display: flex;
width: 200px;
height: 200px; /* We can assume that the container's height is hardcoded in this example, but the end solution should work even if this value is changed*/
border: 1px solid black;
justify-content: center;
}
.flex-item {
box-sizing: border-box;
width: 150px;
border: 5px solid blue;
align-self: center;
background-color: red;
display: flex;
flex-flow: column;
max-height: 100%;
}
.content {
/* It should be possible to scroll this element when it get too big in height*/
background-color: grey;
flex: 1;
}
代码托管在 JSFiddle 上:https://jsfiddle.net/9fduhpev/3/
这就是我想要的:
【问题讨论】: