Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource


Babel includes JSX processing.

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


Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.


Auto Save

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.


<div id="masonry-effect">
    <div class="item" style="height: 400px;"><a href="" target="_blank">View Article</a></div>
    <div class="item" style="height: 200px;">Item 2</div>
    <div class="item" style="height: 350px;">Item 3</div>
    <div class="item" style="height: 210px;">Item 4</div>
    <div class="item" style="height: 600px;">Item 5</div>
    <div class="item" style="height: 320px;">Item 6</div>
    <div class="item" style="height: 80px;">Item 7</div>
    <div class="item" style="height: 450px;">Item 8</div>
    <div class="item" style="height: 600px;">Item 9</div>
    <div class="item" style="height: 320px;">Item 10</div>
    <div class="item" style="height: 80px;">Item 11</div>
    <div class="item" style="height: 450px;">Item 12</div>


    #masonry-effect {
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
    a {
      color: white;
      font-size: 2rem;
      font-weight: 800;
    .item {
        flex-direction: column;
        margin-right: 1.5rem;
        margin-bottom: 1.5rem;
      color: white;
        position: relative;
        width: calc(33.3% - 1.5rem);
        background: linear-gradient(45deg, #281dd4, #a42bff);
        border-radius: 12px;
        padding: 1rem;
        font-size: 1.25rem;
        box-sizing: border-box;
        font-variation-settings: 'wght' 600;
    .item:nth-of-type(5) {
        background: linear-gradient(45deg, #d4581d, #ffbe2b);
    .item:nth-of-type(3) {
        background: linear-gradient(45deg, #d41d71, #a42bff);
    .item:nth-of-type(1) {
        background: linear-gradient(45deg, #1d5ed4, #2bbbff);
    .item:nth-of-type(9), .item:nth-of-type(7) {
        background: linear-gradient(45deg, #85d41d, #2bd8ff);

body {
  background: black;
  padding: 3rem;


let mainId = 'masonry-effect';
let itemIdentifier = '#masonry-effect .item';

document.addEventListener('DOMContentLoaded', function(e) {

    // Programmatically get the column width
    let item = document.querySelector(itemIdentifier);
    let parentWidth = item.parentNode.getBoundingClientRect().width;
    let itemWidth = item.getBoundingClientRect().width + parseFloat(getComputedStyle(item).marginLeft) + parseFloat(getComputedStyle(item).marginRight);
    let columnWidth = Math.round((1 / (itemWidth / parentWidth)));

    // We need this line since JS nodes are dumb
    let arrayOfItems = document.querySelectorAll(itemIdentifier) );
    let trackHeights = {};
    arrayOfItems.forEach(function(item) {
        // Get index of item
        let thisIndex = arrayOfItems.indexOf(item);
        // Get column this and set width
        let thisColumn = thisIndex % columnWidth;
        if(typeof trackHeights[thisColumn] == "undefined") {
            trackHeights[thisColumn] = 0;
        trackHeights[thisColumn] += item.getBoundingClientRect().height + parseFloat(getComputedStyle(item).marginBottom);
        // If the item has an item above it, then move it to fill the gap
        if(thisIndex - columnWidth >= 0) {
            let getItemAbove = document.querySelector(`${itemIdentifier}:nth-of-type(${thisIndex - columnWidth + 1})`);
            let previousBottom = getItemAbove.getBoundingClientRect().bottom;
            let currentTop = item.getBoundingClientRect().top - parseFloat(getComputedStyle(item).marginBottom);
   = `-${currentTop - previousBottom}px`;
    let max = Math.max(...Object.values(trackHeights));
    document.getElementById(mainId).style.height = `${max}px`;