Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
<head>
<title>Gallery columns</title>
</head>
<body>
</body>
</html>
              
            
!

CSS

              
                * {
    padding: 0;
    margin: 0;
}

body {
    font: 12pt sans-serif;
    color: #7f7f7f;
}

.gallery{
    margin: 0 auto;
    display: flex;
    border-left: #7f7f7f solid 1px;
    border-right: #7f7f7f solid 1px;  
}

.column {
    display: block;
}

.card {
    padding: 10px;
    background: #eee;
    box-sizing: border-box;  
}
              
            
!

JS

              
                const COLUMN_WIDTH = 300;
const CARD_WIDTH   = 290;
const CARD_MARGIN  = 10;

function createDomElement(parent, elementName, className=null) {
    var element  = document.createElement(elementName);
    if (className !== null) {
        element.className = className;
    }
    parent.appendChild(element);
    return element;
}

function getMinIndex(array) {
    let index = 0;
    for (let i = 1; i < array.length; i++) {
        if (array[i] < array[index])
            index = i;
        }
    return index;
}

function getCountColumns() {
    let count = Math.floor(document.documentElement.clientWidth / COLUMN_WIDTH);
    if (count < 1) {
        count = 1;
    }
    return count;
}

function getHeights(columns) {
    let heights = [];
    for (let i = 0; i < columns; i++) {
        heights[i] = 0;
    }
    return heights;
}

function getSize() {
    let maximum = 700;
    let minimum = 100;
    return Math.floor(Math.random() * (maximum - minimum) + minimum);
}

function createCard(column, i, index, heights) {
    let height = getSize();
    let card   = createDomElement(column, 'div', 'card');
    card.style.width  = CARD_WIDTH + 'px';
    card.style.height = height + 'px';
    card.style.margin = '0px 0px ' + CARD_MARGIN + 'px ' + CARD_MARGIN + 'px';

    card.innerHTML = 'i:' + i + '<br/>';
    card.innerHTML += 'column: ' + index + '<br/>';

    heights[index] = heights[index] + height + CARD_MARGIN;
    
    return card;
}

function createCards(columns) {
    let cards   = [];
    let count   = 30;
    let height  = 0;
    let card    = null;
    let index   = 0;
    let length  = columns.length - 1;
    let heights = getHeights(columns.length);
    for (let i  = 0; i <= count; i++) {
        index   = getMinIndex(heights);
        card    = createCard(columns[index], i, index, heights);
        cards.push(card);
    }
    return cards;
}

function createColumns(gallery) {
    let count   = getCountColumns();
    let columns = [];

    gallery.style.width = ((COLUMN_WIDTH * count) + CARD_MARGIN) + 'px';

    for (let i = 0; i < count; i++) {
        let column = createDomElement(gallery, 'div', 'column');
        column.style.width  = COLUMN_WIDTH + 'px';
        columns.push(column);
    };

    return columns;
}

function createGallery() {
    let gallery = createDomElement(document.body, 'div', 'gallery');
    let columns = createColumns(gallery);
    let cards   = createCards(columns);
}

createGallery();
              
            
!
999px

Console