Equal Height Images with Flexbox
Images on the Web have a mind of their own. Want to place two fluid images side by side so that their heights are always equal? Flexbox to the rescue!
You can see the final demo here: equal height images with flexbox demo
This would be easy in the old days — set equal height
s and appropriate width
on each img
element and you’re done! But being being responsive citizens we use responsive design and fluid images! So we have the following HTML markup for our images:
<div class="img-group">
<div class="img-container1">
<img src="image1.jpg" >
</div>
<div class="img-container2">
<img src="image2.jpg" >
</div>
</div>
The div
with class img-group
contains all the images we want to display at the same height. Each img
is in its own container so that we can make it fluid using the CSS:
img{
width:100%;
height:auto;
}
This gives us the fluid images we want — the images scale with the size of their containers (see the first demo) and they are stacked vertically. Now we want to place these images horizontally next to each other and then, force them to have the same heights.
We can do this by either using a combination of We can place the image containers side by side using We have to do some math to find the correct widths for each of the image container elements. Say the first image has an aspect ratio aspect1 = 0.5656 and the second image has an aspect ratio aspect2 = 1.7679. I have taken these numbers from the images used in the demo, you should pick your own! To get each image to display at the same height, we use the widths calculated as: So we use these widths as the We have our equal height fluid images! If we want some padding or border on the left/right side of either image container we can use Clearly, this float and width method is hacky; there has to be a better way! Flexbox allows us to get our equal height fluid images very easily. Firstly, we want all images in the Just that simple line makes the images sit side by side (see Flex on Image Group demo); no need for any To make the images the same height we set the This tells each image container to fill up the space inside the image group according to the image’s aspect ratio. The browser does all the math needed to decide how to fit the images correctly. We have equal height flexible images (see Flex with aspect ratios demo). We can easily add padding, border and margins to the left/right of either image container while keeping the equal heights(see the padding and margin demos). Now if we add our third image, we simply add a line of CSS for its container: Flexbox automatically calculates all the correct widths for the previous images (see More Images demo) and we don’t have to change the previous code! If we have a responsive site (as we should) the images will get very small on small viewports and we might want them to stack vertically instead of sitting side by side. This can be done quite easily by putting the The flexbox method still requires us to know the image aspect ratios. Further, we have to insert a separate CSS float
and width
or a much cleaner way using Flexbox.
Float & Width
float:left
1 and setting the correct width
on the image containers in CSS using percentage units. But what are the correct width
s so that both images display at the same height?width
property of the image containers
.img-container1{
width:24.238%;
}
.img-container2{
width:75.762%;
}
box-sizing:border-box;
on the image containers to maintain the equal heights of the images. But since margins sit outside the box, there is no way to put margins on the image containers while having the heights of the images be the same. Worse yet, if we add a third image with aspect ratio aspect3 = 1.0135 we have to go back and recalculate the widths of all the image containers!2Enter Flexbox
img-group
to sit side by side. This is done by the CSS:
.img-group{
display:flex;
}
float
witchcraft.flex
property 3 of each image container to match the aspect ratio of the image with the CSS
.img-container1{
flex:0.5656;
}
.img-container2{
flex:1.7679;
}
.img-container3{
flex:1.0135;
}
Asides
display:flex
on the img-group
inside a min-width
media query. So on smaller screens we get vertically stacked images but on larger ones they sit next to each other with equal heights.flex
rule for each image. I don’t know a good way around this. If anyone does let me know.