I'm currently in the process of developing my portfolio website and I am faced with creating a simplistic image browser feature. My goal is to have a container div that adjusts in size according to the browser window, allowing it to house images of various aspect ratios alongside captions of fixed height but width relative to the image's width. Rather than having the div stretch to accommodate the images, I aim to resize the images themselves (as demonstrated in the provided picture).
View illustration of the problem here
My initial attempt involved using JavaScript to calculate the image size, however, I encountered difficulties as I was unable to determine the element's dimensions before it had fully loaded. Below is an outline of how I approached this issue (excluding consideration for the titlebar):
var divAspectRatio = containerDiv.offsetHeight/containerDiv.offsetWidth;
var imageAspectRatio = image.offsetHeight/image.offsetWidth;
if(divAspectRatio>imageAspectRatio){
image.style.height = content_in.offsetHeight;
}else{
image.style.width = content_in.offsetWidth;
}
captionDiv.style.width = image.offsetWidth;
Any suggestions on how I can resolve this?