【问题标题】:Scaling images in flexbox在 flexbox 中缩放图像
【发布时间】:2019-03-04 21:36:06
【问题描述】:

我在一行中有几张不同尺寸的图片。图像应自动调整大小以填充可用空间,同时保持其纵横比。

如果可能,它们的实际视觉尺寸应与其原始尺寸相关。我的意思是:在缩放版本中,大图像应该比小图像更大。

.Container {
    width: 100%;
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    flex: 1 1 auto;
}

img {
    max-width: 100%;
}
<div class="Container">
  <img src="https://klotzen-statt-kleckern.de/tmp/logo.png">
  <img src="https://klotzen-statt-kleckern.de/tmp/logo.png">
  <img src="https://klotzen-statt-kleckern.de/tmp/logo-g.png">
  <img src="https://klotzen-statt-kleckern.de/tmp/logo-g.png">
  <img src="https://klotzen-statt-kleckern.de/tmp/logo.png">
  <img src="https://klotzen-statt-kleckern.de/tmp/logo.png">
  <img src="https://klotzen-statt-kleckern.de/tmp/logo-g.png">
  <img src="https://klotzen-statt-kleckern.de/tmp/logo-g.png">
</div>

可以在这里找到小提琴:http://jsfiddle.net/ej5au8os/

在 Firefox 62 中,我得到了我想要的结果。

Chrome 和 Edge 似乎根本无法缩放图像。

我错过了什么?

【问题讨论】:

标签: html image css flexbox


【解决方案1】:

图像作为弹性项目是出了名的古怪。不要让它们成为弹性物品。将它们包装在 div 中。

这就是你所需要的(你的 CSS 很好,只是去掉了一些不必要的规则):

.Container {
  display: flex;
  justify-content: center;
  align-items: center;
}

img {
  max-width: 100%;
}
<div class="Container">
  <div>
    <img src="https://klotzen-statt-kleckern.de/tmp/logo.png">
  </div>
  <div>
    <img src="https://klotzen-statt-kleckern.de/tmp/logo.png">
  </div>
  <div>
    <img src="https://klotzen-statt-kleckern.de/tmp/logo-g.png">
  </div>
  <div>
    <img src="https://klotzen-statt-kleckern.de/tmp/logo-g.png">
  </div>
  <div>
    <img src="https://klotzen-statt-kleckern.de/tmp/logo.png">
  </div>
  <div>
    <img src="https://klotzen-statt-kleckern.de/tmp/logo.png">
  </div>
  <div>
    <img src="https://klotzen-statt-kleckern.de/tmp/logo-g.png">
  </div>
  <div>
    <img src="https://klotzen-statt-kleckern.de/tmp/logo-g.png">
  </div>
</div>

jsFiddle demo

【讨论】:

  • 感谢您的支持。使用包装器工作正常。将昆汀的答案标记为正确,仅仅因为他是第一个(相同的解决方案)。
  • 接受您喜欢的答案。这完全取决于你。但请注意,这不是相同的解决方案。 CSS 是不同的。我的工作。他没有。在我发布我的答案后,他更新了他的 CSS 以匹配我的(检查编辑历史)。没什么大不了。仅作记录。
  • 我没有编辑我的答案以匹配你的答案,而是 OP 的问题。我对他的期望是错误的,所以我“纠正”了我的答案。对不起,你看错了。
  • 我没有看错。再次阅读我的评论,尤其是我说“没什么大不了”的部分。我正在回应 OP 的评论并指出实际发生的事情。事实是您确实编辑了答案以匹配我的答案。这只是事实。你是否故意这样做是一个不同的问题。 @QuentinVeron
【解决方案2】:

将您的图像包装在块内。
您可能希望使用自动前缀来实现跨浏览器兼容性。

全球支持:95.71% (source)

.Container{
  width: 100%;
  display: flex;
  align-items: center;
}

img {
  max-width: 100%;
}
<div class="Container">
  <div class="image-wrapper">
    <img src="https://klotzen-statt-kleckern.de/tmp/logo.png">
  </div>
  <div class="image-wrapper">
    <img src="https://klotzen-statt-kleckern.de/tmp/logo.png">
  </div>
  <div class="image-wrapper">
    <img src="https://klotzen-statt-kleckern.de/tmp/logo-g.png">
  </div>
  <div class="image-wrapper">
    <img src="https://klotzen-statt-kleckern.de/tmp/logo-g.png">
  </div>
  <div class="image-wrapper">
    <img src="https://klotzen-statt-kleckern.de/tmp/logo.png">
  </div>
  <div class="image-wrapper">
    <img src="https://klotzen-statt-kleckern.de/tmp/logo.png">
  </div>
  <div class="image-wrapper">
    <img src="https://klotzen-statt-kleckern.de/tmp/logo-g.png">
  </div>
  <div class="image-wrapper">
    <img src="https://klotzen-statt-kleckern.de/tmp/logo-g.png">
  </div>
</div>

使用 ChromeFirefoxEdge

测试

【讨论】:

  • 哇,答案真的很快!谢谢昆汀。使用您的解决方案,图像都具有相同的高度,这不是我想要的。但是使用您建议的包装器和我的原始 CSS 看起来就像我正在寻找的解决方案。
  • 哦,我以为您希望所有图像都具有相同的大小。
  • 我编辑了我的答案。只需删除 flex property 即可!
猜你喜欢
  • 2015-10-05
  • 1970-01-01
  • 2018-03-30
  • 2019-03-12
  • 1970-01-01
  • 1970-01-01
  • 2015-06-15
  • 2019-05-10
  • 2018-07-08
相关资源
最近更新 更多