cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - Activehtmlicon-personicon-teamoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

Code Indentation

     

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.

            
              <h1>Masonry - droppable</h1>
<div class="drops">
  <div class="drop"></div>
  <div class="drop"></div>
  <div class="drop"></div>
  <div class="drop"></div>
</div>

<div class="masonry">
  <div class="item"></div>
  <div class="item w2 h2"></div>
  <div class="item h3"></div>
  <div class="item h2"></div>
  <div class="item w3"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item h2"></div>
  <div class="item w2 h3"></div>
  <div class="item"></div>
  <div class="item h2"></div>
  <div class="item"></div>
  <div class="item w2 h2"></div>
  <div class="item w2"></div>
  <div class="item"></div>
  <div class="item h2"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item h3"></div>
  <div class="item h2"></div>
  <div class="item"></div>
  <div class="item"></div>
  <div class="item h2"></div>
</div>

            
          
!
            
              * {
  -webkit-box-sizing: border-box;
     -moz-box-sizing: border-box;
          box-sizing: border-box;
}

body { font-family: sans-serif; }

.masonry {
  background: #EEE;
}

.item {
  width:  100px;
  height: 100px;
  float: left;
  margin-bottom: 20px;
  background: #D26;
  border: 2px solid #333;
  border-color: hsla(0, 0%, 0%, 0.5);
  border-radius: 5px;
}

.item.h2 { height: 130px; }
.item.h3 { height: 180px; }
.item.h4 { height: 220px; }

.drops:after {
  content: '';
  clear: both;
  display: block;
}

.drop {
  width: 120px;
  height: 240px;
  background: #BDF;
  float: left;
  margin-right: 20px;
  margin-bottom: 20px;
  box-shadow: inset 0 3px 10px hsla(0, 0%, 0%, 0.2)
}
            
          
!
            
              // masonry.pkgd.js added as external resource

$( function() {
  
  var $container = $('.masonry').masonry({
    columnWidth: 100,
    gutter: 20
  });
  
  $container.find('.item').draggable({
    revert: true
  });

  var $drops = $('.drop').droppable();
  
  $drops.on( 'drop', function( event, ui ) {
    var $draggie = ui.draggable;
    // check if already in drop area
    if ( $draggie.isDropped ) {
      return;
    }
    // set flag
    $draggie.isDropped = true;
    // disable revert
    $draggie.draggable( 'option', {
      revert: 'invalid'
    });
    // ignore from masonry and layout
    $container.masonry( 'ignore', $draggie[0] )
      .masonry();
  })
  
  // masonry container droppable
  $container.droppable().on( 'drop', function( event, ui ) {
    var $draggie = ui.draggable;
    // check if already in container
    if ( !$draggie.isDropped ) {
      return;
    }
    // reset flag
    $draggie.isDropped = false;
    // unignore and layout
    $container.masonry( 'unignore', $draggie[0] )
      .masonry( 'once', 'layoutComplete', function() {
        // re-enable revert, after layout complete
        $draggie.draggable( 'option', {
          revert: true
        });
      });
    $container.masonry();
    
  });

});

            
          
!
999px
Close

Asset uploading is a PRO feature.

As a PRO member, you can drag-and-drop upload files here to use as resources. Images, Libraries, JSON data... anything you want. You can even edit them anytime, like any other code on CodePen.

Go PRO

Loading ..................

Console