简体   繁体   English

Flexbox和图像 - Chrome和Firefox中的结果不同

[英]Flexbox & Images — different results in Chrome and Firefox

I am trying to display images on a flexbox grid, and it works as intended in Chrome but not in Firefox (not sure which one is wrong). 我正在尝试在flexbox网格上显示图像,它在Chrome中按预期工作,但在Firefox中不能工作(不确定哪一个是错误的)。

Link to CodePen : https://codepen.io/anon/pen/QJNajw?editors=1100 链接到CodePenhttps//codepen.io/anon/pen/QJNajw editors = 1100

Chrome: 铬:

如预期

Firefox: 火狐:

不是故意的

The idea behind the CSS is to first make the cells of the grid occupy all available space, and then let the images fill the cells without stretching. CSS背后的想法是首先使网格的单元格占据所有可用空间,然后让图像填充单元格而不拉伸。

Looks like in Firefox the images just consume all the available width, which results in an overflow. 在Firefox中看起来,图像只消耗所有可用宽度,这会导致溢出。 (Notice that in the second grid, the layout is more wide than tall, so it does not overlap vertically when the images consume the available width.) (请注意,在第二个网格中,布局比高大更宽,因此当图像消耗可用宽度时,它不会垂直重叠。)

Is there a way to make it work as intended in Firefox? 有没有办法让它在Firefox中按预期工作?

 .grids { display: flex; } .grid { display: flex; flex-direction: column; background: #ddd; box-sizing: border-box; border-radius: 10px; margin: 5px; padding: 2px; width: 120px; height: 120px; } .row { display: flex; justify-content: center; flex-grow: 1; flex-basis: 0; } .cell { display: flex; flex-grow: 1; flex-basis: 0; justify-content: center; align-items: center; box-sizing: border-box; height: 100%; padding: 2px; } .cell img { display: block; max-width: 100%; max-height: 100%; object-fit: contain; } 
 <div class="grids"> <div class="grid"> <div class="row" style="padding: 0px 16.6667%;"> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> </div> <div class="row" style="padding: 0px 0%;"> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> </div> <div class="row" style="padding: 0px 16.6667%;"> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> </div> <div class="row" style="padding: 0px 0%;"> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> </div> </div> <div class="grid"> <div class="row" style="padding: 0px 37.5%;"> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> </div> <div class="row" style="padding: 0px 25%;"> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> </div> <div class="row" style="padding: 0px 12.5%;"> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> </div> <div class="row" style="padding: 0px 0%;"> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> <div class="cell"><img src="https://img.icons8.com/wired/80/000000/circled-user.png"></div> </div> </div> </div> 

Interesting case! 有趣的案例! I don't know why this happens with flexbox. 我不知道为什么这会发生在flexbox上。

But, after some experimenting I figured out that it works by positioning the img relative to its cell: 但是,经过一些实验,我发现它通过将img相对于其单元格定位来工作:

.cell {
     position: relative;
}
.cell img {
    position: absolute;
}

This works in both Firefox and Chrome. 这适用于Firefox和Chrome。

I have no great insight as to the difference between browsers, but you could just set a max-width on the images so that they don't grow larger than you'd like. 我对浏览器之间的差异没有很好的了解,但你可以在图像上设置最大宽度,这样它们就不会比你想要的那样大。 The triangle grid images default to 25px so you could match that, like this: 三角形网格图像默认为25px,因此您可以匹配,如下所示:

.cell img {
  display: block;
  width: 100%;
  max-width: 25px;
  max-height: 100%;
  object-fit: contain;
}

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

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