Pen Settings

HTML

CSS

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

JavaScript

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

Packages

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.

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

              
                <!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
     <title>Auto-repositioning boxes</title>
     <link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700,800|Open+Sans+Condensed:300,700" rel="stylesheet" type="text/css">
       </head>
   <body>
     <div>
       <h3>Content Title</h3>
       <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequa.</p>
       <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatm.</p>
     </div>
     <div>
       <h3>Content Title</h3>
       <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequa.</p>
     </div>
     <div>
       <h3 class="pink">Content Title</h3>
       <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatm.</p>
     </div>
     <div>
       <h3 class="pink">Content Title</h3>
       <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequa. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatm.</p>
     </div>
     
     <div>
       <h3>Content Title</h3>
       <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequa.</p>
       <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatm.</p>
     </div>
     <div>
       <h3 class="pink">Content Title</h3>
       <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatm.</p>
     </div>
     <div>
       <h3>Content Title</h3>
       <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequa.</p>
       <p>Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatm.</p>
     </div>
    
     
     <div>
       <h3>Content Title</h3>
       <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequa. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatm.</p>
     </div>
     
     <div>
       <h3>Content Title</h3>
       <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequa.</p>
     </div>
      <div>
       <h3>Content Title</h3>
       <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequa.</p>
     </div>
   </body>
</html>
              
            
!

CSS

              
                * { margin: 0; padding: 0; }
body {
  padding: 15px;
  background-color: #222;
}
div {
  float: left;
  width: 210px;
  margin: 15px;
  padding: 15px;
  background-color: #333;
  border-radius: 10px;
}
div:hover {
  background-color: #444;
}
div h3 { 
  font-family: "Open Sans Condensed";
  margin-bottom: 10px;
  color: #3498db;
}
div h3.pink {
  color: #e74c3c;
}
div p {
  font-family: "Open Sans";
  font-size: 12px;
  color: #ecf0f1;
  margin-bottom: 15px;
}
              
            
!

JS

              
                //positions each box based on window width and the Y pos of box above it
function layoutBoxes(boxes) {
      var box, boxWidth = 250, boxSpacing = 55, currentPosX = 0, currentPosY = 0, rowItems = 0,
      viewportWidth = document.body.clientWidth;

  for(var i=0, len=boxes.length; i<len; i++) {
      box = boxes[i];
      box.style.top = 0;
      
      //if necessary, start new row
      if(currentPosX + 250 > viewportWidth) {
        rowItems = (rowItems == 0) ? i : rowItems;
        currentPosX = 0;
      }
  
      //calculate top position of current box
      if(rowItems > 0) {
        var boxAbove = boxes[i-rowItems];
            boxAboveHeight = boxAbove.offsetHeight-30;
            boxAboveY = boxAbove.style.top;
        
        boxAboveY = (boxAboveY != "" && boxAboveY != 0) ? +boxAboveY.substr(0,boxAboveY.length-2)-15 : 0;
        currentPosY = boxAboveHeight + boxSpacing + boxAboveY;
      }
  
      //set attributes and increment X position
      box.style.cssText = "position: absolute; left: " + currentPosX + "px; top: " + currentPosY + "px; transition: top .8s, left .8s; -webkit-transition: top .8s, left .8s;";
      currentPosX = currentPosX + boxWidth;
    }
}

//init
var boxes = document.getElementsByTagName("div");
layoutBoxes(boxes);

//only reflow and repaint boxes if number of columns has changed
var currentNumCols = Math.floor(document.body.clientWidth%250);
window.onresize=function() {
  var newNumCols = Math.floor(document.body.clientWidth/250);
  if(newNumCols != currentNumCols) {
    layoutBoxes(boxes);
    currentNumCols = newNumCols;
  }
}
              
            
!
999px

Console