我正在使用flexbox在容器中垂直对齐具有可变高度的div(我打开其他选项)。 但是当容器小于内容时,我很难获得可靠的滚动行为。
html,
body,
.app {
height: 100%;
}
.app {
background-color: blue;
color: white;
}
.app header {
height: 80px;
background-color: red;
}
.app .container {
display: flex;
flex-direction: column;
background-color: black;
height: calc(100% - 80px);
align-items: center;
justify-content: center;
overflow: scroll;
}
.app .container .content {
width: 300px;
height: 200px;
background-color: yellow;
color: black;
}

<link rel="stylesheet prefetch" href="https://cdnjs.cloudflare.com/ajax/libs/meyer-reset/2.0/reset.min.css">
<div class="app">
<header>Header</header>
<div class="container">
<div class="content">
Variable content height
</div>
</div>
</div>
&#13;
非常感谢!
答案 0 :(得分:1)
保留overflow:auto
,无需使用列作为方向,只需将其保留为行。然后依靠margin:auto
来保持元素居中:
无溢出且居中的示例:
html, body, .app {
height: 100%;
}
body {
margin:0;
}
.app {
background-color: blue;
color: white;
}
.app header {
height: 80px;
background-color: red;
}
.app .container {
display: flex;
flex-direction: row;
background-color: black;
height: calc(100% - 80px);
justify-content: center;
overflow: auto;
}
.app .container .content {
width: 300px;
height:100px;
background-color: yellow;
color: black;
margin:auto;
}
<div class="app">
<header>Header</header>
<div class="container">
<div class="content">
Variable content height
</div>
</div>
</div>
溢出和滚动的示例
html, body, .app {
height: 100%;
}
body {
margin:0;
}
.app {
background-color: blue;
color: white;
}
.app header {
height: 80px;
background-color: red;
}
.app .container {
display: flex;
flex-direction: row;
background-color: black;
height: calc(100% - 80px);
justify-content: center;
overflow: auto;
}
.app .container .content {
width: 300px;
height:900px;
background-color: yellow;
color: black;
margin:auto;
}
<div class="app">
<header>Header</header>
<div class="container">
<div class="content">
Variable content height
</div>
</div>
</div>