Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keep image ratio using max-width and max-height in IE 11

I'm trying to get an image to fit inside a container while keeping it's size ratio. The image should take full height or width depending on orientation. My solution does work on all browsers I've tested but IE11(works in 10 and 9 surprisingly).

In IE 11 the image is cropped on the right. I'd like a pure css way if possible and I don't care about centering it.

Here is the 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;
}
like image 977
Mosset Jérémie Avatar asked Jan 05 '17 16:01

Mosset Jérémie


People also ask

How do I keep an image aspect ratio?

By setting the width property to 100%, you are telling the image to take up all the horizontal space that is available. With the height property set to auto, your image's height changes proportionally with the width to ensure the aspect ratio is maintained. The end result is an image that scales up or down perfectly.

Is there a max height in CSS?

The max-height property in CSS is used to set the maximum height of a specified element. Authors may use any of the length values as long as they are a positive value. max-height overrides height, but min-height always overrides max-height .

Can we specify the weight and height of an image in a Web page?

The height and width of an image can be set using height and width attribute. The height and width can be set in terms of pixels. The <img> height attribute is used to set the height of the image in pixels. The <img> width attribute is used to set the width of the image in pixels.


1 Answers

This seems to be a bug in IE11: Bug Report. Adding flex: 1 (as in the report)

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

works for me. Flexbox for the parent seems ok, so even centering is easy.

Another option is

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

on the img, instead of flex: 1 , increasing compatibility with Firefox. See comments and bug report for more info.

like image 126
benzkji Avatar answered Oct 05 '22 06:10

benzkji