I'm trying to arrange images into a neatly packed grid like in the screenshot. The images come from a CMS so they can be any size.
How can I make this work either using CSS or JS?
There are various layout js
libraries already present like
You can check how to replicate pinterest.com's absolute div stacking layout
Try this, hopefully this will help. Just you have to add the margin in between the images of your choice.
<div id="image-container">
<img src="http://fakeimg.pl/300/">
<img src="http://fakeimg.pl/250x100/">
...
</div>
#image-container {
line-height: 0;
-webkit-column-count: 5;
-webkit-column-gap: 0px;
-moz-column-count: 5;
-moz-column-gap: 0px;
column-count: 5;
column-gap: 0px;
}