You want to create a dynamic grid where the images are fixed width but the height is flexible, creating Pinterest panel style affect.
UIKit uses the CSS3 Flex component with its own Grid components to create seamless panels that are responsive and automatically close the gaps.
Give it a spin. Reduce your browser width and sort by asc/desc. I think the results are pretty impressive for very little code.