Here is my situation:
I have a wrapper div, a main div inside that and then an <img />
in the main div, whose aspect ratio I have to keep when the window resizes. Width and height of the <img />
are not fixed and may change from one image to another.
To keep the aspect ratio of each image preserved, I have used a :after
pseudo-element for my wrapper class like this:
wrapper:after{
padding-top: **%;
display: block;
content: '';
}
But since I don't know the width and height of the image, I have to calculate its aspect ratio dynamically using jQuery when the page loads, like this:
$("#ad_image_main").load(function(){
var h = $(this).height();
var hh = (h/660)*100;
/**/
});
#ad_image_main is the id of my <img />
tag
And I have to insert in the /**/
the padding-top
value (which is hh
) for the wrapper:after
-element to get the aspect ratio I want.
How can I do this?