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.

UIKit Dynamic Grid Example

Start by just adding the UIKit core js and the grid component js to your project:

    <script src="/js/uikit.min.js"></script>
    <script src="/js/grid.min.js"></script>

Sample Code for 2 elements in a dynamic grid:

        <div data-uk-grid> <!--dynamic-grid parent element-->
            <div class="uk-width-small-1-2 uk-width-medium-1-4 uk-width-large-1-4"> <!--child element-->
                <img src="http://www.ucl.ac.uk/news/news-articles/1213/muscle-fibres-heart.jpg" class="uk-responsive-height">
            </div>
            <div class="uk-width-small-1-2 uk-width-medium-1-4 uk-width-large-1-4"> <!--child element-->
                <img src="http://climate.nasa.gov/system/feature_items/images/9_images_of_change.jpg" class="uk-responsive-height">
            </div>
        </div>

Keep on Learning!