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

              
                <div><img class="draggable" src="https://source.unsplash.com/daily" /></div>
<div><img class="draggable" src="https://source.unsplash.com/daily?architecture" /></div>
<div><img class="draggable" src="https://source.unsplash.com/daily?dancing" /></div>
<div><img class="draggable" src="https://source.unsplash.com/daily?music" /></div>
<div><img class="draggable" src="https://source.unsplash.com/daily?nature" /></div>
<div><img class="draggable" src="https://source.unsplash.com/daily?fashion" /></div>
<div><img class="draggable" src="https://source.unsplash.com/daily?business" /></div>
<div><img class="draggable" src="https://source.unsplash.com/daily?cat" /></div>
<div><img class="draggable" src="https://source.unsplash.com/daily?portrait" /></div>
<div><img class="draggable" src="https://source.unsplash.com/daily?model" /></div>
<div><img class="draggable" src="https://source.unsplash.com/daily?music" /></div>
<div><img class="draggable" src="https://source.unsplash.com/daily?nature" /></div>
<div><img class="draggable" src="https://source.unsplash.com/daily?fashion" /></div>
<div><img class="draggable" src="https://source.unsplash.com/daily?flower" /></div>
<div><img class="draggable" src="https://source.unsplash.com/daily?dancing" /></div>
<div><img class="draggable" src="https://source.unsplash.com/daily?dog" /></div>
<div><img class="draggable" src="https://source.unsplash.com/daily?boat" /></div>
<div><img class="draggable" src="https://source.unsplash.com/daily?people" /></div>
              
            
!

CSS

              
                html, body {
  height: 100%;
}
body {
  margin: 0px;
  touch-action: none;
  overflow: hidden;
}

div {
  margin: 2px;
  float: left;
  position: relative;
  
  &.dragging {
    z-index: 1;
    img {
      object-fit: contain;
    }
  }
}

img {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: cover;
  will-change: transform;
}
              
            
!

JS

              
                const MAX_ROW_NUM = 5; // the maximum amount of images per row
const MIN_ROW_NUM = 2; // the minimum amount of images per row
const GAP = 2; // the gap inbetween images in pixels. Has to match the CSS.
const ROW_HEIGHTS = [33, 66, 100, 133, 150]; // want even rows? np! remove all but one here.

// these are all of our images
const imgs = Array.from(document.querySelectorAll('img'));

// main masonry function
function masonry() {
  console.time('recalc masonry..');
  
  // in production code you'd use loops instead of all these
  // array functions... but they explain the code a little better.
  const images = Array.from(imgs);
  while(images.length) {
    
    // Array that stores our currently best candidate.
    // 0: offset from viewport width
    // 1: array of images for that candidate
    // 2: the row height of that candidate
    const bestCandidate = [1000, null, null];
     
    // test 3-5 cols
    for(let i=MIN_ROW_NUM; i <= MAX_ROW_NUM; i++) {
      if(images.length >= i) {
        let candidates = images.slice(0, i);
        
        // test ~10 different row heights
        ROW_HEIGHTS.forEach(rowHeight => {
          let width = candidates.reduce((a, c) => a + (rowHeight * (c.naturalWidth / c.naturalHeight)), 0);
          let delta = (window.innerWidth - GAP * i * 2) - width;
          if(Math.abs(delta) < bestCandidate[0]) {
            bestCandidate[0] = delta;
            bestCandidate[1] = candidates;
            bestCandidate[2] = rowHeight;
          }          
        });

      }
    }

    if(bestCandidate[1]) {
      bestCandidate[1].forEach(img => {
        let crop = bestCandidate[0] / bestCandidate[1].length;
        img.parentNode.style.height = bestCandidate[2] + 'px';
        img.parentNode.style.width = Math.floor((bestCandidate[2] * (img.naturalWidth / img.naturalHeight) + crop)) + 'px';
      });
    } else {
      // means there's not even enough left for one row, so might as well
      // remove previous styles to make sure the image displays fully
      images[0].parentNode.style.width = '';
      images[0].parentNode.style.height = '';
    }
 
    images.splice(0, bestCandidate[1] ? bestCandidate[1].length : 1);
  }
  
  console.timeEnd('recalc masonry..');
  
}

// run masonry on load
// (when we have all image dimensions...luckily we'll have them cached usually)
window.onload = () => {
  masonry();
};

// run masonry debounced on resize
let resizeTimer;
window.onresize = () => {
  clearTimeout(resizeTimer);
  resizeTimer = setTimeout(function() {
    console.log('run debounce');
    masonry();  
  }, 32);
}

// draggy drag..
for(let img of imgs) {
  img.addEventListener('customdragstart', function(e) {
    this.parentNode.classList.add('dragging');
  }, false);
  img.addEventListener('customdragstop', function(e) {
    this.parentNode.classList.remove('dragging');
  }, false);
}

              
            
!
999px

Console