css - 在 IE 11 中使用最大宽度和最大高度保持图像比例

标签 css cross-browser internet-explorer-11 responsive

我正在尝试让图像适合容器,同时保持其大小比例。图像应根据方向占据全高或全宽。我的解决方案适用于我测试过的所有浏览器,但 IE11(令人惊讶地适用于 10 和 9)。

在 IE 11 中,图像在右侧被裁剪。如果可能的话,我想要一种纯 css 方式,我不关心它是否居中。

这是 JSFiddle:https://jsfiddle.net/wagad0u8/

<div class="owl-item" style="width: 465px;">
  <a class="img-flux" href="page1.html">
    <img alt="omg" src="http://placehold.it/1000x100">
  </a>
</div>

<div class="owl-item" style="width: 465px;">
  <a class="img-flux" href="page1.html">
    <img alt="omg" src="http://placehold.it/400x780">
  </a>
</div>

.img-flux img {
    border: 0;
    max-height: 100%;
    max-width: 100%;
    height: auto;
    width: auto;
    position: relative;
    transition: all 0.3s;
    margin: 0 auto;
    float: none;
    display: block;
    vertical-align:middle;
}
#content-block *:last-child {
    margin-bottom: 0px;
}
.owl-item, .owl-item .img-flux {
    height: 100%;
}
.img-flux {
    background-color: #ECECEC;
    position: relative;
    width: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
    overflow: hidden;
}
.owl-item{
  float:left;
  height:300px;
  margin-bottom:50px;
}

最佳答案

这似乎是 IE11 中的错误:Bug Report .添加 flex: 1(如报告中所示)

.img-flux img {
    flex: 1;
    max-width: 100%;
    max-height: 100%;
}

对我有用。父级的 Flexbox 似乎没问题,所以即使居中也很容易。

另一种选择是

flex: 0 0 auto;  /* IE */
object-fit: scale-down; /* FF */

img 上,而不是 flex: 1 ,增加了与 Firefox 的兼容性。有关详细信息,请参阅评论和错误报告。

关于css - 在 IE 11 中使用最大宽度和最大高度保持图像比例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41489798/

相关文章:

javascript - 在新选项卡中打开但未与父选项卡分组

html - 使用 Google Web Toolkit 的 UI 设计师指南

html - 使 HTML5 和 CSS3 网站与旧浏览器兼容?

css - 属性宽度 css 不能正常工作 IE11

javascript - IE FileReader打印出 "\n"

javascript - 是否有任何跨浏览器(IE7-8)支持CSS3属性显示的js文件:box/display:flexbox?

html - 关键帧动画在 IE11 上无法正常工作

css - CSS 中的报纸专栏

CSS 和 z-index : child element under parent element in a list

css - 如果网站在其他浏览器中运行正常,您如何开始解决 IE6 问题?