简体   繁体   English

在 IE11 上的嵌套 flexbox 容器中保持图像纵横比

[英]Maintain image aspect ratio in nested flexbox container on IE11

I'm using flexbox to display a row of blocks.我正在使用 flexbox 来显示一行块。 Each block is using flexbox to display a column of elements.每个块都使用 flexbox 来显示一列元素。 One of the elements is an image that needs to maintain its aspect ratio.其中一个元素是需要保持纵横比的图像。 The code below is a simplified version of my code that illustrates what I'm trying to do.下面的代码是我的代码的简化版本,它说明了我正在尝试做的事情。

In Chrome the images scale to 186px x 186px.在 Chrome 中,图像缩放为 186px x 186px。 In IE11 they display 186px x 500px.在 IE11 中,它们显示 186px x 500px。 I've tried wrapping the images in another div and setting its height and/or width to 100%, but nothing works for both Chrome and IE11.我尝试将图像包装在另一个 div 中并将其高度和/或宽度设置为 100%,但对 Chrome 和 IE11 都不起作用。

 section { max-width: 600px; margin: 0 auto; } .grid { display: flex; flex-wrap: wrap; margin-left: -20px; margin-top: 10px; } .block { margin: 0; flex: 1 0 150px; margin-left: 20px; display: flex; flex-direction: column; align-items: center; } .block img { width: 100%; }
 <section> <div class="grid"> <div class="block"> <img src="http://placehold.it/500" alt=""> <h2>Block title</h2> </div> <div class="block"> <img src="http://placehold.it/500" alt=""> <h2>Block title</h2> </div> <div class="block"> <img src="http://placehold.it/500" alt=""> <h2>Block title</h2> </div> </div> </section>

It looks like adding min-height: 1px;看起来像添加min-height: 1px; to the img is the solution.img是解决方案。 I wish I had a good explanation for why this works.我希望我有一个很好的解释为什么这行得通。 Here's the best I could find : 这是我能找到的最好的

... My best guess is that the min-height forces IE to recalculate the height of the rendered content after all of the resizing, and that makes it realize that the height is different.... ...我最好的猜测是 min-height 强制 IE 在所有调整大小后重新计算渲染内容的高度,这使得它意识到高度是不同的....

Fellow stranger looking for fix, but featured on top answer didn't help.寻找修复的陌生人,但在最佳答案中出现并没有帮助。 Setting min-width: 1px;设置min-width: 1px; to the img may help you as well.img也可以帮助你。

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

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