在IE 11中使用max-width和max-height保持图像比例

Mos*_*mie 14 css cross-browser css3 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;
}
Run Code Online (Sandbox Code Playgroud)

ben*_*kji 33

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

.img-flux img {
    flex: 1;
    max-width: 100%;
    max-height: 100%;
}
Run Code Online (Sandbox Code Playgroud)

适合我.父母的Flexbox似乎没问题,所以即使是居中也很容易.

另一种选择是

flex: 0 0 auto;  /* IE */
object-fit: scale-down; /* FF */
Run Code Online (Sandbox Code Playgroud)

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

  • 这有效!你只需要使用css hack来定位IE 11,因为它会破坏至少FF上的东西. (2认同)
  • 为了防止宽高比下降,我需要在图像上使用 `flex: 0 0 auto`,如错误报告中所述。谢谢你的链接。 (2认同)
  • Bug Report链接不再有效.微软已退休"微软连接".我猜他们仍然将文章隐藏在其他网站中,但我不知道哪一个 - 他们认为没有必要为引用的项目提供更新的URL. (2认同)