JSFiddle - React, Tailwind, and code Playground

by thirtydot

HTML

<ul id="my_boxwrapper" class="column">
    <li class="box"><img />One</li>
    <li class="box"><img />Two Lorem ipsum this is very long text, much longer than the rest and should take more space.Two Lorem ipsum this is very long text, much longer than the rest and should take more space. Two Lorem ipsum this is very long text, much longer than the rest and should take more space</li>
    <li class="box"><img />Three</li>
    <li class="box"><img />Four</li>
    <li class="box"><img />Five</li>
    <li class="box"><img />Six</li>
    <li class="box"><img />Seven</li>
    <li class="box"><img />Eight</li>
    <li class="box"><img />Nine</li>
    <li class="box"><img />Ten</li>
    <li class="box"><img />Eleven</li>
    <li class="box"><img />Twelve</li>
    <li class="box"><img />Thirteen</li>
    <li class="box"><img />Fourteen</li>
</ul>

CSS

body {
    background: blue;
}
body { margin: 0; padding: 0; }
.column {
    width: 960px;
    padding: 0;
    display: block;
    margin: 0;
    list-style: none;
}
.box {
    width: 200px;
    display: inline-block;
    vertical-align: top;
    background: green;
}
.box > * {
    margin: 1%;
}
.box img {
    display: block;
    height: 100px;
    width: 98%;
    background: red;
}

JavaScript

var smartColumns = {
    config: {
        width: 'auto',
        column_selector: "ul.column",
        box_selector: "ul.column > .box",
        distribute_remainder: true,
    },
    init: function(config) {        
        var me = this;
        
        $(this.config.column_selector).contents().filter(function() { return this.nodeType === 3; }).remove();
        
        if (config) {
            for (prop in config) {
                this.config[prop] = config[prop];
            }
        }
        this.config.width = this.config.width == 'auto' ? $(this.config.box_selector).width() : this.config.width;

        // Execute the function when page loads
        me.run();

        //Each time the viewport is adjusted/resized, execute the function
        $(window).resize(function() { me.run() });
    },
    run: function(config) {
        var column_selector = this.config.column_selector;
        var box_selector = this.config.box_selector;
        
        //Reset column size to a 100% once view port has been adjusted
        $(column_selector).css({ 'width' : "100%"});

        var colWrap = $(column_selector).width(); //Get the width of row
        var colNum = Math.floor(colWrap / this.config.width); //Find how many columns of 200px can fit per row / then round it down to a whole number
        var colFixed = Math.floor(colWrap / colNum); //Get the width of the row and divide it by the number of columns it can fit / then round it down to a whole number. This value will be the exact width of the re-adjusted column

        $(column_selector).css({ 'width' : colWrap}); //Set exact width of row in pixels instead of using % - Prevents cross-browser bugs that appear in certain view port resolutions.
        $(box_selector).css({ 'width' : (100 / colNum) + "%"}); //Set exact width of the re-adjusted column    

    }
};
$(document).ready(function() {
    smartColumns.init();
});