The above link to a possible duplicate is not a solution for this case, because the height will be a fixed value for several breakpoints.
I have some DIVs with display:inline-block
, so they are floating nicely side by side. These DIVs all have the same height, e.g. height:300px
. Later, I will load an image inside every DIV with Ajax, and I want the DIV to keep the aspect ratio of the image, so they won't wiggle all around, when the image is actually loaded.
So when the DIVs are displayed in the browser, the images are not yet there, so fixing the height for the image with height:auto;
won't work.
Sample code:
<div class="wrapper">
<div class="item">...</div>
<div class="item">...</div>
<!-- More items here -->
</div>
CSS:
.item {
display:inline-block;
vertical-align:top;
height: 300px;
width: /* depending on the image ratio */
}
Now I know how to keep the aspect ratio of an element for a given width (see here or here). But since my DIVs should all have the same height, how can I keep the aspect ratio and change just the width?
One (not really good) solution would be to insert a blank image and to resize this image to the right dimensions.
The problem is: when resizing the window, the height of all the DIVs will change, so just calculating the width is not enough. I could recalculate the width with Javascript, but I prefer a plain CSS version (if possible).
So here is my question: how can I keep the aspect ratio for an element for a given height by CSS only?
Thanks