I used a different approach. You said that the width will remain unchanged, while the heights will vary. Unlike floating each individual img, I think it's better (and easier) to place imgs inside a column and rather float columns.
Live demo, try resizing your browser and more ...
HTML
<div id="image_box"> <div class="col"> <img><img><img><img><img><img> </div> <div class="col"> <img><img><img><img><img><img> </div> <div class="col"> <img><img><img><img><img><img> </div> <div class="col"> <img><img><img><img><img><img> </div> <div class="col"> <img><img><img><img><img><img> </div> </div>
CSS
#image_box { width:90%; margin:0px auto; } .col { width:18%; float:left; margin:0px 1%; } img{ width:100%; height:auto; margin-top:6%; }
I just threw it together relatively quickly. If you want to improve the layout, you can specify a different width for the .image_box and the column overflow under other columns.
source share