Calculate Auto Aspect Ratio Automatically

Asked

Viewed 153 times

1

With the example below we calculate the Aspect ratio of an image by setting Width/Height manually, with this the value of the Aspect ratio is generated to be put in the padding-bottom:

What I would like is something similar: I want to take the widht/height value of an image, calculate the ratio value, and put it in its container, all automatically. There will be several images with different dimensions.

<div class="image-container">
    <img src="minhaimagen.jpg" width="300" height="200">
</div>

The generated value would be in a padding-bottom in the div image-container

Example: https://codepen.io/EightArmsHQ/full/OMGOyE/

For some reason you can’t put the example here in Stack.

  • Link you might be interested in https://css-tricks.com/aspect-ratio-boxes/

  • Aspect Ratio in this case would it be to make the image proportional (relative to height)? For example, an image with 320wx240h and resize it to 640 height automatically the width will be 480?

  • @Rpgboss I can’t say. This Aspect Ratio Generator solves the problem I was having with lazysizes. The ratio is calculated based on Width/Height, generating a value to be placed in the padding-bottom, and it works really well. I’ll just have several images with different dimensions and I don’t want to have to manually set their ratio. But do automatically as described in the question. That’s all.

1 answer

1


You can browse all images on <div class="image-container">, calculate the ratio and convert to %. Simply divide the smallest by the largest (the width or the height) and multiply by 100. If they are the same size, the result will be 100%.

window.onload = function(){
   
   var imgs = document.body.querySelectorAll(".image-container a img");
   
   for(var x=0; x<imgs.length; x++){
      
      var img_w = parseInt(imgs[x].getAttribute("width")),
          img_h = parseInt(imgs[x].getAttribute("height"));

          if(img_w == img_h){
             var img_p = 100;
          }else if(img_w > img_h){
             var img_p = (img_h/img_w) * 100;
          }else if(img_w < img_h){
             var img_p = (img_w/img_h) * 100;
          }
          
          imgs[x].parentNode.parentNode.style.paddingBottom = img_p.toFixed(2)+"%";
          console.log(img_p.toFixed(2)+"%");
   }
   
}
<div class="image-container">
    <a href="#">
       <img src="http://lorempixel.com/400/200" width="400" height="200">
    </a>
</div>

<div class="image-container">
    <a href="#">
       <img src="http://lorempixel.com/output/nature-q-c-300-200-1.jpg" width="300" height="200">
    </a>
</div>

<div class="image-container">
    <a href="#">
       <img src="http://lorempixel.com/output/technics-q-c-150-150-8.jpg" width="150" height="150">
    </a>
</div>

<div class="image-container">
    <a href="#">
       <img src="http://lorempixel.com/200/200/sports/" width="100" height="200">
    </a>
</div>

<div class="image-container">
    <a href="#">
       <img src="http://lorempixel.com/300/200/people/" width="300" height="200">
    </a>
</div>

  • One thing I forgot: before the image has a link: <div class="image-container"><a href="#">&#xA; <img src="minhaimagen.jpg" width="100" height="50">&#xA;</a>&#xA;</div>

  • I removed the link just to test, and it’s working fine. But the value with padding-bottom is being placed in the to and not in the image-container.

  • I updated the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.