html - flex child 从 parent 那里长大

标签 html css flexbox

如何在不设置静态高度值且不设置绝对位置的情况下强制将绿色框包含在红色框内?

我想缩小内容以适应父级。

允许内容(在本例中为视频)缩小并允许滚动条。

.my-box {
  height: 300px;
  width: 600px;
  background: red;
  padding: 5px;
}
.content-box {
  background: blue;
}
.col {
  display: flex;
  flex-direction: column;
  justify-content: space-between
}
.box-shrink {
  flex: 0 1 auto;
  background: green;
  padding: 5px;
  margin: 5px;
}
.box-grow {
  flex: 1 0 auto;
  background: green;
  padding: 5px;
  margin: 5px;
}
video {
  max-height: 100%;
  max-width: 100%;
  margin: auto;
  display: block;
}
<div class="my-box col">
  <div class="box-shrink">
    small sized static content
  </div>
  <div class="content-box box-grow">
    <video controls>
      <source src="http://techslides.com/demos/sample-videos/small.webm" type="video/webm">
    </video>
  </div>
  <div class="box-shrink">
    small sized static content
  </div>
</div>

最佳答案

解决方案 #1 - 无滚动

不要在视频容器上使用 flex: 1 0 auto,只需使用 flex: 1。这会根据可用空间而不是内容的固有高度来调整元素的大小。

然后,因为 flex 元素不能小于其内容的大小——min-height: auto 是默认值——添加 min-height: 0 以允许元素缩小以适合容器。

.box-grow {
  flex: 1; /* formerly flex: 1 0 auto; */
  background: green;
  padding: 5px;
  margin: 5px;
  min-height: 0; /* new */
}

.my-box {
  height: 300px;
  width: 600px;
  background: red;
  padding: 5px;
}
.content-box {
  background: blue;
}
.col {
  display: flex;
  flex-direction: column;
  justify-content: space-between
}
.box-shrink {
  flex: 0 1 auto;
  background: green;
  padding: 5px;
  margin: 5px;
}
.box-grow {
  flex: 1; /* formerly flex: 1 0 auto; */
  background: green;
  padding: 5px;
  margin: 5px;
  min-height: 0; /* new */
}
video {
  max-height: 100%;
  max-width: 100%;
  margin: auto;
  display: block;
}
<div class="my-box col">
  <div class="box-shrink">
    small sized static content
  </div>
  <div class="content-box box-grow">
    <video controls>
      <source src="http://techslides.com/demos/sample-videos/small.webm" type="video/webm">
    </video>
  </div>
  <div class="box-shrink">
    small sized static content
  </div>
</div>

解决方案 #2 - 使用滚动

或者,给视频容器 overflow: auto,它的作用与上面相同,只是它保持视频全宽。您需要启用 flex-shrink 才能正常工作。

.box-grow {
  flex: 1 1 auto; /* formerly flex: 1 0 auto; */
  background: green;
  padding: 5px;
  margin: 5px;
  overflow: auto; /* new */
}

.my-box {
  height: 300px;
  width: 600px;
  background: red;
  padding: 5px;
}
.content-box {
  background: blue;
}
.col {
  display: flex;
  flex-direction: column;
  justify-content: space-between
}
.box-shrink {
  flex: 0 1 auto;
  background: green;
  padding: 5px;
  margin: 5px;
}
.box-grow {
  flex: 1 1 auto; /* formerly flex: 1 0 auto; */
  background: green;
  padding: 5px;
  margin: 5px;
  overflow: auto; /* new */
}
video {
  max-height: 100%;
  max-width: 100%;
  margin: auto;
  display: block;
}
<div class="my-box col">
  <div class="box-shrink">
    small sized static content
  </div>
  <div class="content-box box-grow">
    <video controls>
      <source src="http://techslides.com/demos/sample-videos/small.webm" type="video/webm">
    </video>
  </div>
  <div class="box-shrink">
    small sized static content
  </div>
</div>

此处对这两种解决方案进行了更详细的解释:

关于html - flex child 从 parent 那里长大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41674979/

相关文章:

html - Firefox 无法正确呈现 HTML

javascript - 使用脚本/标签将 IE 文档模式更新到最新版本

javascript - 更改滚动行为 : Scroll in Increments of 100vh

wordpress - CSS 中的横向和纵向图像尺寸

css - flexbox 或 react-css-grid 可以实现这种布局吗?

javascript - jQuery 窗口滚动关闭后重新打开

javascript - 使用 JavaScript 设置 'autofocus' 属性不起作用

javascript - 新 Twitter 小部件的 iframe CSS 覆盖

html - Chrome 不会对图像调整大小使用react

html - 如何在 bootstrap 4 的容器中垂直和水平居中放置文本?