【问题标题】:Flex in Firefox shrinks images automatically, but not in ChromeFirefox 中的 Flex 会自动缩小图像,但 Chrome 中不会
【发布时间】:2017-06-13 21:17:11
【问题描述】:

<h1>Window width:</h1>

<div style="display: flex">
  <img src="https://unsplash.it/400/225?image=10" alt="1">
  <img src="https://unsplash.it/400/225?image=11" alt="2">
  <img src="https://unsplash.it/400/225?image=12" alt="3">
</div>

<h1>Wrapped in 500px wide div:</h1>

<div style="width: 500px; overflow: auto">
  <div style="display: flex">
    <img src="https://unsplash.it/400/225?image=10" alt="1">
    <img src="https://unsplash.it/400/225?image=11" alt="2">
    <img src="https://unsplash.it/400/225?image=12" alt="3">
  </div>
</div>

这是 Firefox 中的结果:

这是 Chrome 中的结果:

如您所见,在 Firefox 中,图像已被很好地缩小和调整大小,因此所有图像都可以排成一行,无需换行或裁剪。在 Chrome 上,图像会保持其原始大小,这会导致在小窗口或 div 中进行裁剪。

这是预期的吗?难道我做错了什么?如何在 Firefox 和 Chrome 中获得相同的结果?

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    这些是弹性容器中的初始设置:

    • flex-grow: 0
    • flex-shrink: 1
    • flex-basis: auto

    简写为:

    • flex: 0 1 auto

    因此,即使您没有在代码中指定这些规则,它们也适用于图像。

    图像不能增长,它们可以缩小(同样且刚好足以避免溢出容器),并且它们最初的大小为自然宽度 (400 像素)。

    这就是您在 Firefox 中看到的。图像正在缩小以很好地适应容器。

    在 Firefox 中,弹性规则会覆盖图像的自然尺寸。

    然而,在 Chrome 中,情况正好相反。以图片尺寸为准。

    简单的跨浏览器解决方案是将图像包装在另一个元素中,因此这个新包装器成为 flex 项并采用默认的flex: 0 1 auto,无需覆盖任何内容。

    img {
      width: 100%;
    }
    <h1>Window width:</h1>
    
    <div style="display: flex">
      <span><img src="https://unsplash.it/400/225?image=10" alt="1"></span>
      <span><img src="https://unsplash.it/400/225?image=11" alt="2"></span>
      <span><img src="https://unsplash.it/400/225?image=12" alt="3"></span>
    </div>
    
    <h1>Wrapped in 500px wide div:</h1>
    
    <div style="width: 500px; overflow: auto">
      <div style="display: flex">
        <span><img src="https://unsplash.it/400/225?image=10" alt="1"></span>
        <span><img src="https://unsplash.it/400/225?image=11" alt="2"></span>
        <span><img src="https://unsplash.it/400/225?image=12" alt="3"></span>
      </div>
    </div>

    就遵循规范指南的浏览器而言,似乎是 Firefox。在 flex 容器中,应以 flex 规则为准:

    7.1. The flex Shorthand

    当一个盒子是一个弹性项目时,flex 被咨询而不是主尺寸属性来确定盒子的主要尺寸。

    弹性项目的main size propertywidthheight 属性。

    我说 Firefox“似乎”是正确的,因为规范说 flex 规则应该优于 CSS widthheight 属性。

    当然,在这种情况下,图像的尺寸并没有在 CSS 中定义。它们是图像的自然尺寸。因此,这种情况可能会留待解释,Chrome 可能不会违反任何准则。

    但是,在另一种情况下,height 属性是一个因素,Firefox 坚持使用flex,而 Chrome 坚持使用heightWhy is Firefox not honoring flexed div's height, but Chrome is?

    【讨论】:

    • "没有什么需要被覆盖。"除了img元素的width属性,需要设置为100%
    • 原代码中没有定义width属性。我的 img 规则是调整图像大小以适合其容器,并且没有任何内容被覆盖(至少在 CSS 中)。
    • 另外,图像不再是弹性项目。我指的是浏览器之间与弹性项目大小有关的不一致。这不再是问题。
    • 我向 Chrome 提交了一个错误,所以我们可以看看他们是否应该修复这个问题:bugs.chromium.org/p/chromium/issues/detail?id=982188
    【解决方案2】:

    在这种情况下,将 align-items: flex-start 添加到 flex 容器中,然后将此规则添加到图像中

    img {
      min-width: 0;
      width: 100%;
    }
    

    由于align-items 默认为stretch,所以它们会拉伸,然后min-width 默认为auto,这再次告诉它们是原始大小,最后,给它们width: 100%,以便它们水平适合并调整它的高度以保持纵横比。

    注意,经过快速浏览器测试后,这在 IE11 上不起作用(但在 Edge 上起作用),因此根据使用的代码,到处都有错误存在。第二个选项,其中一个包装图像,但适用于 IE11。

    堆栈sn-p

    img {
      min-width: 0;
      width: 100%;
    }
    <h1>Window width:</h1>
    
    <div style="display: flex; align-items: flex-start;">
      <img src="https://unsplash.it/400/225?image=10" alt="1">
      <img src="https://unsplash.it/400/225?image=11" alt="2">
      <img src="https://unsplash.it/400/225?image=12" alt="3">
    </div>
    
    <h1>Wrapped in 500px wide div:</h1>
    
    <div style="width: 500px; overflow: auto">
      <div style="display: flex; align-items: flex-start;">
        <img src="https://unsplash.it/400/225?image=10" alt="1">
        <img src="https://unsplash.it/400/225?image=11" alt="2">
        <img src="https://unsplash.it/400/225?image=12" alt="3">
      </div>
    </div>

    另一种选择是包装图像,并将img 设置为width: 100%

    img {
      width: 100%;
    }
    <div style="display: flex;">
      <div>
        <img src="https://unsplash.it/400/225?image=10" alt="1">
      </div>
      <div>
        <img src="https://unsplash.it/400/225?image=11" alt="2">
      </div>
      <div>
        <img src="https://unsplash.it/400/225?image=12" alt="3">
      </div>
    </div>

    这篇 css3-flexbox-maintain-image-aspect-ratio 的帖子包含链接和一个很好的解释,关于浏览器如何/为什么呈现不同

    【讨论】:

    • 很好的答案!虽然,我仍然很困惑为什么 Firefox 的默认行为与 Chrome 不同。 min-width 在 Firefox 中也默认为 autoalign-items 在 Firefox 和 Chrome 中默认为 normal
    • 这是导致它们行为不同的 Flexbox 错误,而这个答案 stackoverflow.com/a/30792956/2827823 与链接一起提供了很好的解释。我将链接到我的那个答案
    • @Michael_B 和 Edge 有 auto... 加 1 供您解释... 我经常有 how's,但不经常解释 为什么 :)
    猜你喜欢
    • 1970-01-01
    • 2017-07-27
    • 2012-04-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-30
    • 2019-03-12
    相关资源
    最近更新 更多