【问题标题】:Image height inside flexbox not working in Chromeflexbox内的图像高度在Chrome中不起作用
【发布时间】:2017-04-15 03:09:11
【问题描述】:

我有一个 div 使用 flexbox 将其项目居中。在这个 div 里面我有 3 个元素,其中一个是图像。

<div id="flex-container">
    <div id="container1"></div>
    <img src="#" alt="">
    <div id="container2"></div>
</div>

#container1 和#container2 有自己的高度,img 应该使用#flex-container 内部的剩余高度。

此 sn-p 适用于 Firefox,但不适用于 Chrome。 (jsfiddle)

#flex-container{
  height: 300px;
  width: 500px;
  display: flex;
  display: -webkit-flex;
  flex-flow: column nowrap;
  -webkit-flex-flow: column nowrap;
  justify-content: center;
  -webkit-justify-content: center;
  align-items: center;
  -webkit-align-items: center;
  border: 5px solid black;
}

#container1, #container2{
  height: 100px;
  width: 300px;
  background: orange;
  flex: 1 0 auto;
  -webkit-flex: 1 0 auto;
}
<div id="flex-container">

  <div id="container1">300x100 px</div>

  <img src="http://i.imgur.com/RRUe0Mo.png" alt="">

  <div id="container2">300x100 px</div>

</div>

Chrome 需要为 flexbox 加上 -webkit- 前缀,但问题似乎不是这个。

会发生什么?是浏览器错误还是我忘记了什么?

【问题讨论】:

  • 它们应该从左到右还是从上到下流动?
  • @LGSon 从上到下,项目在flex-container 居中。
  • 顺便说一句,在 Chrome 中运行 flexbox 不需要前缀。 caniuse.com/#search=flex
  • @Michael_B 很高兴知道,我用过这个shouldiprefix。现在我看到 Chrome 是受支持的,但是像 safari 这样的其他人仍然需要前缀 :(
  • 这里是浏览器支持的完整评论:stackoverflow.com/a/35137869/3597276

标签: css google-chrome flexbox responsive-images


【解决方案1】:

你需要克服两个问题:

Firefox 自己解决了这两个问题,但 Chrome 需要帮助。

问题 #1

第一个问题是弹性项目,默认情况下,不能小于它们的内容。弹性项目的初始设置是min-height: auto。

因此,带有替换元素的弹性项目(如图像)将默认为图像的固有大小。该项目不能变小,除非您覆盖初始设置(使用min-height: 0)。

#flex-container {
  height: 300px;
  width: 500px;
  display: flex;
  flex-flow: column nowrap;
  justify-content: center;
  align-items: center;
  border: 5px solid black;
}
#container1, #container2 {
  height: 100px;
  width: 300px;
  background: orange;
  flex: 1 0 auto;
}
img { min-height: 0; } /* NEW */
<div id="flex-container">

  <div id="container1">300x100 px</div>

  <img src="http://i.imgur.com/RRUe0Mo.png" alt="">

  <div id="container2">300x100 px</div>

</div>

可以在此处找到有关此问题的完整说明:


问题 #2

然后你遇到了第二个问题:保持纵横比。这是弹性容器中的常见问题。一种选择是定义图像的高度:

#flex-container {
  height: 300px;
  width: 500px;
  display: flex;
  flex-flow: column nowrap;
  justify-content: center;
  align-items: center;
  border: 5px solid black;
}
#container1, #container2 {
  height: 100px;
  width: 300px;
  background: orange;
  flex: 1 0 auto;
}
img { min-height: 0; height: 100px; } /* NEW */
<div id="flex-container">

  <div id="container1">300x100 px</div>

  <img src="http://i.imgur.com/RRUe0Mo.png" alt="">

  <div id="container2">300x100 px</div>

</div>

【讨论】:

  • 我仍然不明白为什么需要 min-height 才能使其在 Chrome 上运行,但它在您的答案中得到了广泛的解释,我会在可能的时候再看一下。并且没有办法在保持纵横比的同时缩小图像高度?将其包裹在 div 中可能吗?
  • 是的,我的链接答案提供了详细的解释。关于纵横比,就像我说的,这是一个常见问题,我已经看到了很多解决方案。 div 包装器以前为我工作过。测试一下。干杯!
  • 它可能关注的对象:可以使用object-fit: contain; CSS 属性在保持纵横比的同时缩小图像。
猜你喜欢
  • 2016-11-20
  • 2015-09-11
  • 2015-11-10
  • 1970-01-01
  • 1970-01-01
  • 2017-02-25
  • 2022-01-21
  • 2012-05-03
  • 2018-01-30
相关资源
最近更新 更多