简体   繁体   English

Firefox 和 Chrome 中 Flexbox 的不同实现

[英]Different implementation of Flexbox in Firefox and Chrome

The following code works as expected in Firefox, but in Chrome, the images become much bigger than the flex container.以下代码在 Firefox 中按预期工作,但在 Chrome 中,图像变得比 flex 容器大得多。

 .r { width: 100%; height: auto; min-width: unset; max-width: 100%; } .container { display: flex; width: 600px; }
 <div class="container"> <img src="https://via.placeholder.com/1000x500" class="r" > <img src="https://via.placeholder.com/1000x500" class="r" > <img src="https://via.placeholder.com/1000x500" class="r" > <img src="https://via.placeholder.com/1000x500" class="r" > </div>

Firefox :火狐 在此处输入图像描述

Chrome : 在此处输入图像描述

The following code works as expected in Firefox以下代码在 Firefox 中按预期工作

I disagree with this because for me Chrome is behaving as expected for 2 reasons:我不同意这一点,因为对我来说,Chrome 的行为符合预期有两个原因:

  1. You set the width of the image to be 100% which means 100% of their containing block (container) that is defined by 600px .您将图像的宽度设置为100% ,这意味着600px定义的包含块(容器)的 100%。 So each image will be 600px所以每张图片都是600px

  2. The image cannot shrink past its content size due to the default min-width configuration (note that using unset is equivalent to initial in this case so it's somehow useless).由于默认的min-width配置,图像不能缩小到其内容大小(请注意,在这种情况下使用unset等效于initial所以它在某种程度上是无用的)。 So the image will be kept at 600px所以图像将保持在600px

If you add min-width:0 the image will shrink only in width:如果添加min-width:0图像只会缩小宽度:

 .r { width: 100%; /*height: auto; useless */ min-width: 0; max-width: 100%; } .container { display: flex; width: 600px; }
 <div class="container"> <img src="https://via.placeholder.com/1000x500" class="r" > <img src="https://via.placeholder.com/1000x500" class="r" > <img src="https://via.placeholder.com/1000x500" class="r" > <img src="https://via.placeholder.com/1000x500" class="r" > </div>

the behavior of chrome seems to have changed so the below is no more needed in the last version chrome 的行为似乎已经改变,因此在上一个版本中不再需要以下内容

Now if we consider the height you are facing the stretch effect that is also not the same in both browser. 现在,如果我们考虑您所面临的 stretch效果的高度,这在两个浏览器中也不相同。 It's a bit trikcy to explain 1 but if you change the default alignment you will get the expected result within chrome: 解释 1有点棘手,但是如果您更改默认对齐方式,您将在 chrome 中获得预期的结果:

 
   
   
    
    .r { width: 100%; /*height: auto; useless */ min-width: 0; max-width: 100%; } .container { display: flex; width: 600px; align-items:flex-start; }
   
   
 
   
   
    
    <div class="container"> <img src="https://via.placeholder.com/1000x500" class="r" > <img src="https://via.placeholder.com/1000x500" class="r" > <img src="https://via.placeholder.com/1000x500" class="r" > <img src="https://via.placeholder.com/1000x500" class="r" > </div>
   
   

Or if you change the height using percentage value you will make it fail and also have what you want (this is a bit hacky because we are triggering another issue to fix the actual one) 或者,如果您使用百分比值更改高度, 您将使其失败并获得您想要的(这有点 hacky,因为我们正在触发另一个问题来修复实际问题)

 
   
   
    
    .r { width: 100%; height:658%; /*any value here with %*/ min-width: 0; max-width: 100%; } .container { display: flex; width: 600px; }
   
   
 
   
   
    
    <div class="container"> <img src="https://via.placeholder.com/1000x500" class="r" > <img src="https://via.placeholder.com/1000x500" class="r" > <img src="https://via.placeholder.com/1000x500" class="r" > <img src="https://via.placeholder.com/1000x500" class="r" > </div>
   
   

Why Firefox is behaving like that?为什么 Firefox 会有这样的行为?

I don't know exactly, but the logical explanation is that Firefox is not considering the default min-width configuration and is giving priority to keeping ratio and not to the stretch effect.我不太清楚,但合乎逻辑的解释是 Firefox 没有考虑默认的min-width配置,而是优先考虑保持比例而不是拉伸效果。


1 Initially your image are defining the height of the container since they are big (around 700px in height), this height is used by the container then we apply the properties to our image so they shrink in width and since the default alignment is stretch they will stretch to the height of the container that was initially defined by their own intial height creating this rendring. 1最初,您的图像定义了容器的高度,因为它们很大(大约 700 像素的高度),这个高度由容器使用,然后我们将属性应用于我们的图像,因此它们的宽度会缩小,并且由于默认对齐方式是拉伸它们将拉伸到最初由它们自己的初始高度定义的容器的高度,从而创建此渲染。

If we remove the stretch effect, the image will try to keep their ratio since we removed the height constraint.如果我们移除拉伸效果,图像 将尝试保持它们的比例,因为我们移除了高度约束。

Same logic if we consider percentage value for height.如果我们考虑高度的百分比值,则相同的逻辑。 This one will fail to auto and we get back to the default behavior (keeping the aspect ratio)这一个将无法auto ,我们回到默认行为(保持纵横比)


Another alternative另一种选择

The issue arised due to the use of image that are replaced element with intrinsic dimension where the calculation of width/height is not as simple as other element.该问题是由于使用图像替换元素而产生的,这些元素具有固有尺寸,其中宽度/高度的计算不像其他元素那么简单。

To avoid such behavior, better wrap the image inside a div and avoid having them as flex items.为避免这种行为,最好将图像包装在div中,并避免将它们作为弹性项目。

 .r { width: 100%; max-width: 100%; } .container { display: flex; width: 600px; } img { max-width: 100%; }
 <div class="container"> <div class="r"><img src="https://via.placeholder.com/1000x500"></div> <div class="r"><img src="https://via.placeholder.com/1000x500"></div> <div class="r"><img src="https://via.placeholder.com/1000x500"></div> <div class="r"><img src="https://via.placeholder.com/1000x500"></div> </div>

@TheRuler, The best solution is to wrap the image tag in a div or span, to render the images as per Firefox. @TheRuler,最好的解决方案是将图像标签包装在 div 或 span 中,以按照 Firefox 呈现图像。 Firefox will also work fine if you wrap the images in a div or span.如果您将图像包装在 div 或 span 中,Firefox 也可以正常工作。

Now you can ask me why this is correct.现在你可以问我为什么这是正确的。

Basically display flex has defaults value attached to it, which are:基本上 display flex 附加了默认值,它们是:

flex-grow: 0
flex-shrink: 1
flex-basis: auto
flex: 0 1 auto /* Shorthand */

Now, for both case Firefox and Chrome the default value is same.现在,对于 Firefox 和 Chrome,默认值是相同的。 But still the rendering is different in the both browser specially in case of image as a flex child.但是两个浏览器中的渲染仍然不同,特别是在图像作为 flex child 的情况下。

Now who is correct is debatable, and who is following the guidelines properly is also debatable.现在谁是正确的值得商榷,谁正确地遵循了指导方针也是值得商榷的。

If you look at the specification of flex, it says, flex rules should prevail over the CSS width and height properties.如果你看一下 flex 的规范,它会说,flex 规则应该优先于 CSS 的宽度和高度属性。 So in this case Firefox is shrinking the size of images to fit it properly in 600px parent.所以在这种情况下,Firefox 正在缩小图像的大小以使其适合 600px 父级。

Now if you see this example,现在如果你看到这个例子,

.container { display: flex; flex-direction: column;}
.container > div { flex: 1; height: 300px;}

<div class="container">
  <div>Single line in Firefox, but 300px tall in Chrome!</div>
</div>

So if you see this code in both browser you will see the difference.因此,如果您在两个浏览器中都看到此代码,您将看到不同之处。 Chrome values the height property of the flex child where as Firefox doesn't value it. Chrome 重视 flex 子项的 height 属性,而 Firefox 不重视它。 Same goes for image height and image width.图像高度和图像宽度也是如此。 Chrome doesn't prevail the height.铬不占高度。

So both browser went with their own implementation, which is in this case doesn't match each other and it is debatable who is correct.所以这两个浏览器都有自己的实现,在这种情况下不匹配,谁是正确的存在争议。

So wrap them in span or div to make it work in both the browser which should be the correct answer with no hack.因此,将它们包装在 span 或 div 中,使其在两个浏览器中都可以正常工作,这应该是正确的答案,没有 hack。 Other solution will solve the problem but they are kind of hack to solve the problem.其他解决方案将解决问题,但它们是解决问题的一种技巧。

Please let me know if you get it Thanks如果你得到它请告诉我谢谢

thanks to @TemaniAfif who guided me in the right direction and I came up with this:感谢@TemaniAfif,他引导我朝着正确的方向前进,我想出了这个:

 .r { width: 100%; height: 100%; min-width: 0px; max-width: 100%; } .container { display: flex; width: 600px; }
 <div class="container"> <img src="https://lundbeckconsulting.no/Temp/illu1.jpg" class="r" /> <img src="https://lundbeckconsulting.no/Temp/illu1.jpg" class="r" /> <img src="https://lundbeckconsulting.no/Temp/illu1.jpg" class="r" /> <img src="https://lundbeckconsulting.no/Temp/illu1.jpg" class="r" /> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM