0

I have an html page like this :

<html>
<head>

<style>
.main {
max-width:500px;
min-height:500px;
margin:auto;
}
.image-container {
width:100%;
background-color:black;
min-height: 200px;
height:300px;
}
img {
  object-fit: contain;
  height:200px;
}

</style>

</head>
<body>
<div class="main">
<div class="image-container">
<img src="https://i.gyazo.com/3a4b9b23d8d1a179b75472d58bc55fe3.jpg" style="width:100%"/>
</div>
</div>
</body>
</html>

this object-fit perfectly fine on google chrome and firefox , but unfortunately not in IE . I don't want to use as background image as I will use this image in a carousel in future ? is there any alternative so that I fit an image in a fixed height div without stretching and overflowing ? Noted : Object-fit : contain perfectly serve my requirements but failed on IE .

Titli4fly
  • 11
  • 2

2 Answers2

-1

Use Modernizr.

if ( ! Modernizr.objectfit ) {
    $('.post__image-container').each(function () {
        var $container = $(this),
        imgUrl = $container.find('img').prop('src');
        if (imgUrl) {
          $container
            .css('backgroundImage', 'url(' + imgUrl + ')')
            .addClass('compat-object-fit');
        }  
    });
}
Gündoğdu Yakıcı
  • 677
  • 2
  • 10
  • 31
  • Yes I know it will probably work , but , I am not sure , What will be the property of that compat-object-fit to make the design > could you suggest any ? – Titli4fly Sep 06 '18 at 08:42
-1

So you use the object-fit like this:

.post__featured-image {
    width: 120px;
    height: 120px;
    object-fit: cover;
 }

This will copy the src of our image to the background image source of the container. Further we change our SCSS a little bit:

.post {
 &__image-container {
  width: 120px; // the same width and height as for the <img>
  height: 120px;
  &.compat-object-fit {
   background-size: cover;
   background-position: center center;
   .post__featured-image { // hide image if object fit is not supported - opacity to 0 for the link area
    opacity: 0;
   }
  }
 }
 &__featured-image {
  width: 120px;
  height: 120px;
  object-fit: cover;
 }
}
Gündoğdu Yakıcı
  • 677
  • 2
  • 10
  • 31
  • could you give me an example doing this ? I tried that solution but it seems to me that it will work for object-fit:cover not contain – Titli4fly Sep 06 '18 at 10:27