Skip to content Skip to sidebar Skip to footer

A Better Way Of Resizing A Child Image While Keeping Its Aspect Ratio

I'm wondering whether there's a better way of maintaining the image aspect ratio of the images (e.g. the rollover thumbs). It currently uses transparent images that are 3*2 big; wi

Solution 1:

You can use padding-bottom on image-thumb and set it to the ratio of the image in %.

I got 73.375% from 587 / 800, for that first tiger image, I have removed all the transparent placeholders. Use 66.66666% if you just want 2/3

#images-wrap {
  width: 100%;
  height: auto;
  margin-top: 25px;
  float: left;
  display: flex;
}

#details-wrap {
  width: 100%;
  height: 325px;
  float: left;
  text-align: right;
  position: relative;
}
#main-image {
  width: 80.5%;
  float: left;
  background-size: cover !important;
  background-position: center center !important;
  height: auto;
}
#test {
  width: 100%;
  height: auto;
}
#main-image>img {
  display: block;
  width: 100%;
  height: auto;
  margin: 0;
}

#image-thumbs {
  width: 17.5%;
  height: auto;
  float: left;
  margin-left: 2%;
  overflow-y: scroll !important;
  /* make it only scroll when exceeds height of main image *//* max-height: 400px;  make this the height of #main-image */
}

.image-thumb {
  margin-bottom: 6px;
  background-position: center;
  background-size: cover;
  padding-bottom: 73.375%;
}
canvas {
  display: block;
  width: 100%;
  background-position: center;
  background-size: cover;
  margin-bottom: 6px;
}

.image-thumb:last-of-type {
  margin-bottom: 0;
}

.image-thumb>img {
  height: auto;
  width: 100%;
}
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><divid="images-wrap"><divid="main-image"style="background-image: url('http://elephant-family.org/wp-content/uploads/2015/06/shutterstock_77217466.jpg')"><imgsrc="data:image/svg+xml;charset=utf-8,%3Csvg xmlns%3D'http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg' viewBox%3D'0 0 3 2'%2F%3E"width="3"height="2"id="main-image-sizer" /></div><divid="image-thumbs"><divclass="image-thumb"onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));"style="background-image: url('https://imagesvc.timeincapp.com/v3/mm/image?url=https%3A%2F%2Ffortunedotcom.files.wordpress.com%2F2014%2F09%2F174187214.jpg&w=800&q=85')"></div><divclass="image-thumb"onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));"style="background-image: url('http://streamafrica.com/wp-content/uploads/2014/01/african-lion-wallpapers-hd-648x372.jpg')"></div><canvasonmouseover="$('#main-image').css('background-image', $(this).css('background-image'));"style="background-image: url('https://imagesvc.timeincapp.com/v3/mm/image?url=https%3A%2F%2Ffortunedotcom.files.wordpress.com%2F2014%2F09%2F174187214.jpg&w=800&q=85')"width=3height=2></canvas><divclass="image-thumb"onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));"style="background-image: url('http://streamafrica.com/wp-content/uploads/2014/01/african-lion-wallpapers-hd-648x372.jpg')"></div><divclass="image-thumb"onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));"style="background-image: url('https://imagesvc.timeincapp.com/v3/mm/image?url=https%3A%2F%2Ffortunedotcom.files.wordpress.com%2F2014%2F09%2F174187214.jpg&w=800&q=85')"></div><divclass="image-thumb"onmouseover="$('#main-image').css('background-image', $(this).css('background-image'));"style="background-image: url('http://streamafrica.com/wp-content/uploads/2014/01/african-lion-wallpapers-hd-648x372.jpg')"></div><script>// hides overflow scroll if less than 5 thumbsvar thumbs = document.getElementsByClassName('image-thumb');
      var thumbsWrap = document.getElementById('image-thumbs');
      if (thumbs.length < 5) {
        thumbsWrap.style.overflow = 'hidden';
      }
    </script><script>// makes '#image-thumbs' not exceed the height of '#main-image'var mainImgHeight = document.getElementById('main-image-sizer').style.height;
      var imageThumbsInitialHeight = document.getElementById('image-thumbs').style.height;
      if (imageThumbsInitialHeight > mainImgHeight) {
        document.getElementById('image-thumbs').style.height = mainImgHeight;
      }
    </script></div></div>

Post a Comment for "A Better Way Of Resizing A Child Image While Keeping Its Aspect Ratio"