css - 使用 flexbox 的图像自动调整高度不起作用

标签 css flexbox

我正在尝试使用 css flexbox 实现以下解决方案:

schematic overview

我在 https://plnkr.co/edit/TbJIdOrJLtDfczno4Kpu?p=preview 创建了一个 Plunkr这显示了我目前拥有的东西。

  • grid row 有一个固定的高度(Plunkr 中的 grid-row 类)
  • 红色容器 应该自动调整高度(Plunkr 中的文章)
  • headerfooter 有固定的高度(Plunkr 中的header 和footer)
  • 图像应自动适应剩余空间并设置为背景图像(Plunkr 中的图片)

无论我尝试什么,图像都会自动调整网格行的高度。有人看到我做错了什么吗?

我的 HTML

<div class="grid-row">

  <article>

    <header>
      Header text
    </header>

    <picture class="image">

    </picture>

    <footer>
      footer text
    </footer>

  </article>

</div>

我的 CSS

.grid-row {
  background: orange;
  height: 200px;
}

.image {
  background-image: url('http://lorempixel.com/400/200/sports/5/');
  background-size: cover;
  flex: auto;
}

article {
  display: flex;
  flex-direction: column;
}

最佳答案

问题是您没有通过结构向上/向下扩展 flexbox 样式。

.grid-row {
  background: orange;
  height: 200px;
  display: flex;
  flex-direction: column;
}
.image {
  background-image: url('http://lorempixel.com/400/200/sports/5/');
  background-size: cover;
  flex: 1; /* remaining height of article */
}
article {
  display: flex;
  flex-direction: column;
  flex: 1; /* 100% height of row */
}
header {
  background: red;
}
footer {
  background: green;
}
<div class="grid-row">

  <article>

    <header>
      Header text
    </header>

    <div class="image"></div>

    <footer>
      footer text
    </footer>

  </article>

</div>

关于css - 使用 flexbox 的图像自动调整高度不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35257862/

相关文章:

javascript - 是否可以像这样用 CSS 剪切 DIV 的一侧?

android - 使用 android 本地语言编写 css

javascript - 用 3 个按钮制作一个程序。文本输入将输出前的大小写改成p。如何进行?

css - cufon 文本显示不正确

css - 如何在没有媒体查询的情况下处理响应式表单布局

html - 如何让所有item都缩小到二维最大的item内容?

javascript - 选择选项 1 时禁用选项 2

html - Bootstrap 4,如何使用 "no scroll"在 SPA 上获得响应高度?

css - Flex wrap 不适用于固定宽度的容器

css - flexbox 中的边距折叠