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.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

HTML

            
              <div class="grid-container">
  <div class="grid-item grid-first" style="order: 1">
    <div draggable="true">1</div>
  </div>
  <div class="grid-item" style="order: 2">
    <div draggable="true">2</div>
  </div>
  <div class="grid-item" style="order: 3">
    <div draggable="true">3</div>
  </div>
  <div class="grid-item" style="order: 4">
    <div draggable="true">4</div>
  </div>
  <div class="grid-item" style="order: 5">
    <div draggable="true">5</div>
  </div>
</div>
            
          
!

CSS

            
              body {
  margin: 0;
  background: rgb(240,240,240);
}

.grid-container {
  display: grid;
  grid-template-columns: 400px 400px repeat(auto-fill, 400px);
  grid-template-rows: 400px 400px repeat(auto-fill, 400px);
  grid-gap: 10px;
  margin: 10px;
  padding: 0;
}

.grid-item {  
  min-height: 400px;
/*   border-radius: 2px; */
}

.grid-first {
  grid-column: 1 / 3;
  grid-row:  1 / 3; 
}

.grid-item.hovering div {
  background: #b6d6fb;
  border: 5px dashed #276cbc;
}

.grid-item div {
  background: white;
  height: 100%;
  min-height: 400px;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 6em;
  box-shadow: 0px 3px 1px -2px rgba(0,0,0,0.2), 0px 2px 2px 0px rgba(0,0,0,0.14), 0px 1px 5px 0px rgba(0,0,0,0.12);
  border-radius: 2px;
}

@media ( max-width: 900px ) {  
  .grid-container         { 
    grid-template-columns: 1fr;  
  }
  .grid-first { 
    grid-column: 1 / 1;
    grid-row: inherit;
  }
}
            
          
!

JS

            
              const draggers = document.querySelectorAll('.grid-item div')
const catchers = document.querySelectorAll( '.grid-item' )

draggers.forEach((item) => {
  item.ondragstart = function(ev) {
    ev.dataTransfer.setData("text/plain", ev.target.parentNode.style.order);
  }
})

catchers.forEach((item) => {
    item.ondragenter = item.ondragover = function(ev) {
      ev.preventDefault();
      item.classList.add('hovering');
    };

    item.ondragleave = function() {
      item.classList.remove('hovering');
    };

    item.ondrop = function(ev) {
      
      // get dragging parent div for getting/setting order
      const selectNumber = Number(ev.dataTransfer.getData("text/plain"))   
      const currentlyDragging = document.querySelector(`.grid-item[style*="order: ${selectNumber}"]`)

      // logic to switch order   
      const catcherOrder = item.style.order // catcher
      const draggerOrder = currentlyDragging.style.order // mover


      // bump stuff around
      catchers.forEach((catcher) => {
        catcher.classList.remove("grid-first")
        if (catcher.style.order < draggerOrder && catcher.style.order >= catcherOrder ) {
          catcher.style.order = Number(catcher.style.order) + 1
        }
        if (catcher.style.order > draggerOrder && catcher.style.order <= catcherOrder ) {
          catcher.style.order = Number(catcher.style.order) - 1
        }
      })

      // assign dragged item
      currentlyDragging.style.order = catcherOrder

      // reassign grid-first
      document.querySelector('.grid-item[style*="order: 1"]').classList.add("grid-first")

      item.classList.remove( 'hovering' );

    };
  
});
            
          
!
999px

Console