I am currently working on making side-by-side images dynamic so they can stay side-by-side no matter what screen they are on. However, these images also have different ratios; so, I am using the flex display type to keep them the same height.
Here is my current HTML:
<div class="row">
<div class="column">
<img src=(thefirstimage) alt="A picture of me."/>
</div>
<div class="column">
<img src=(thesecondimage) alt="Another picture of me."/>
</div>
</div>
And here is my CSS:
* {box-sizing: border-box;}
.column {
display: flex;
flex-direction: row;
justify-content: center;
float: left;
height: 40vh;
width: auto;
padding: 5px;
}
.row::after {
content: "";
clear: both;
display: table;
}
At this point on my computer screen, the images are properly side-by-side at the same height despite the different aspect ratios. However, when I view the same page on my phone one image moves on top of the other.
So to sum up, the two problems I'm trying to get to work together are:
- Resizing side-by-side images to be the same height
- Keeping that no matter what the screen size is
I saw two other posts on here that cover those both separately, I just can't get it to work together.
Thanks ahead of time!
You’ve asked for a solution which allows you to fill the width of the viewport with two images side-by-side with the same height but differing aspect ratios. I’m going to give you a solution to put any number of images of varying aspect-ratios side-by-side.
The first part of the solution is a flexbox with a fixed height, with the flex items (images) each styled with 100% height. This gives you an image strip with all images having the same height, and with their width determined by their aspect ratio.
Let’s try with five images with aspect ratios as follows:
If the flexbox is short (has a small height) it will render like this:
Here is a working snippet to demonstrate the solution so far. Make sure after running it you use the full page link to get the full effect.
Did you use the full page link?
Now the only remaining challenge is to dynamically adjust the height of the flexbox so that the images exactly fill the width of the screen. If we can determine the aspect ratio that the flexbox needs to have, we can use that together with the viewport width to calculate our desired height.
Fortunately, if we know the aspect ratios of each of the images in the row, we simply need to sum these ratios to obtain the aspect ratio when they are arranged beside each other. Let's do this for the five images in our example, which are:
Add these up and the result is 6.17. So we simply divide our viewport width (minus the sum of the gaps, if we want gaps) by 6.17 to obtain the desired height of our flexbox.
Here is a snippet which implements this algorithm. It does not depend on knowing the aspect ratios of the images ahead of time, rather it waits for the images to load and then checks their
naturalWidthandnaturalHeight. Note that because it listens for resize events, which are fired rapidly when a browser window is stretched with a mouse, we need to debounce for performance reasons.Again, after running the snippet please use the full page link and adjust the size of your browser window to test the responsiveness.
Did you use the full page link?
For further reading, see this thread.