Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
574 views
in Technique[技术] by (71.8m points)

html - 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.

.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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

@TheRuler, The best solution is to wrap the image tag in a div or span, to render the images as per Firefox. Firefox will also work fine if you wrap the images in a div or span.

Now you can ask me why this is correct.

Basically display flex has defaults value attached to it, which are:

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. But still the rendering is different in the both browser specially in case of image as a 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. So in this case Firefox is shrinking the size of images to fit it properly in 600px parent.

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. 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. 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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...