flexbox 中最小尺寸的内容

Content in flexbox with minimum size

我正在使用 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>

非常感谢!

保持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>