cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-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>Packery demo - show spaces</h1>
<div class="packery">
  <canvas></canvas>
  <div class="item w3"></div>
  <div class="item h4"></div>
  <div class="item w3"></div>
  <div class="item w2 h3"></div>
</div>

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

body { font-family: sans-serif; }

.packery {
  border: 1px solid;
  max-width: 520px;
}

/* clearfix */
.packery:after {
  content: ' ';
  display: block;
  clear: both;
}

.item {
  width: 40px;
  height: 40px;
  float: left;
  background: #C09;
  border: 4px solid #333;
  border-color: hsla(0, 0%, 0%, 0.3);
  opacity: 0.7;
}

.item:hover {
  border-color: white;
  cursor: move;
}

.item.w2 { width:   80px;  }
.item.h2 { height:  80px;  }
.item.w3 { width:  120px; }
.item.h3 { height:  120px; }
.item.h4 { height: 160px;  }
.item.w4 { width:  160px;  }

.item.is-dragging,
.item.is-positioning-post-drag {
  border-color: white;
  background: #09F;
  z-index: 2;
}

.packery canvas {
  position: absolute;
}

            
          
!
            
              // https://packery.metafizzy.co/packery.pkgd.js and 
// https://draggabilly.desandro.com/draggabilly.pkgd.js added as external resource


var container = document.querySelector('.packery');
var pckry = new Packery( container, {
  itemSelector: '.item',
  columnWidth: 40,
  rowHeight: 40
});
var itemElems = pckry.getItemElements();
// for each item element
for ( var i=0, len = itemElems.length; i < len; i++ ) {
  var elem = itemElems[i];
  // make element draggable with Draggabilly
  var draggie = new Draggabilly( elem );
  // bind Draggabilly events to Packery
  pckry.bindDraggabillyEvents( draggie );
}


// draw spaces on canvas
var canvas = container.querySelector('canvas');
var ctx = canvas.getContext('2d');

function drawSpaces() {
  // reset canvas size
  canvas.width = pckry.size.width;
  canvas.height = pckry.size.height;
  ctx.clearRect(0,0,pckry.size.width,pckry.size.height);
  // all Packery's empty spaces
  var spaces = pckry.packer.spaces;
  ctx.fillStyle = 'hsla(150, 100%, 50%, 0.4)';
  ctx.strokeStyle = 'hsla(150, 100%, 30%, 0.8)';
  for ( var i=0, len = spaces.length; i<len; i++ ) {
    var space = spaces[i];
    // each space is a Rect object, just geometry
    // width, height, x, y
    var height = Math.min( space.height, canvas.height - space.y );
    // draw rectangle for space
    ctx.fillRect( space.x, space.y, space.width, height );
    ctx.strokeRect( space.x + 0.5, space.y + 0.5, space.width - 0.5, height - 0.5 );
  }
}

pckry.on( 'layoutComplete', drawSpaces );
pckry.on( 'dragItemPositioned', drawSpaces );
//hack, trigger another layout for first rendering
pckry.layout();

            
          
!
999px
Loading ..................

Console