html - flexbox 中最小尺寸的内容

标签 html css flexbox vertical-alignment

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

关于html - flexbox 中最小尺寸的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48704171/

相关文章:

javascript - 如何将数据/变量从 file_controller.rb 传递到 application.js

css - JQuery Mobile 中的外部面板正在失去其 CSS 样式

html - 以不同的顺序排列div

html - 根据 child 的尺寸自动调整 parent 的高度( flex 元素)

html - 为什么 flexbox 在包装元素时不将后续内容向下推?

ios - iOS 中的 iCloud 备份/恢复 HTML5 Cordova 应用程序

python - 选择什么 HTML 解析器以及为什么 BeautifulSoup 不起作用?

html - 如何使用媒体查询更改 Bootstrap 网格系统

CSS 列 : target last child in each column?

javascript - 我在 IOS 中遇到了 UIWebview 对象的奇怪问题