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

Auto Save

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 class="card positioned js-is-draggable" aria-grabbed="false">Drag Me 🎉</div>
              
            
!

CSS

              
                :root {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, "Open Sans", "Helvetica Neue", sans-serif;
  font-size: 16px;
}

.card {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 20rem;
  height: 10rem;
  background-color: #efefef;
  border-radius: .625rem;
  box-shadow: 0 .0625rem .1875rem rgba(51, 51, 51, .12),
    0 .0625rem .125rem rgba(51, 51, 51, .12);
  transition: box-shadow 75ms linear;
}

.positioned {
  position: relative;
  top: 6.25rem;
  left: 50%;
  transform: translateX(-50%);
}

.card:hover { cursor: move; }

.card.js-is-dragging {
  box-shadow: 0 .4375rem .875rem rgba(51, 51, 51, .25),
    0 .3125rem .3125rem rgba(51, 51, 51, .22);
}

.js-is-draggable { user-select: none; }
              
            
!

JS

              
                let mover;
let offsets;
let position;

function addEvent(type, callback, target = document) { target.addEventListener(type, callback, false); }
function removeEvent(type, callback, target = document) { target.removeEventListener(type, callback); }

function offsetPosition(target) {
  let node = target;
  let X = 0;
  let Y = 0;

  do {
    X += node.offsetLeft;
    Y += node.offsetTop;
    node = node.offsetParent;
  }
  while (node);

  return { X, Y };
}
function parentsUntilMatch(target, filter) {
  if (!target || target.nodeType === 9) return null;
  switch (true) {
    case ('matchesSelector' in target):
        if (target.matchesSelector(filter)) return target;
        break;
    case ('mozMatchesSelector' in target):
        if (target.mozMatchesSelector(filter)) return target;
        break;
    case ('msMatchesSelector' in target):
        if (target.msMatchesSelector(filter)) return target;
        break;
    case ('oMatchesSelector' in target):
        if (target.oMatchesSelector(filter)) return target;
        break;
    case ('webkitMatchesSelector' in target):
        if (target.webkitMatchesSelector(filter)) return target;
        break;
    default:
        if (target.matches(filter)) return target;
        break;
  }
  return parentsUntilMatch(target.parentNode, filter);
}
function styleStringFromObj(obj) {
  let str = '';

  for (const prop in obj) {
    str += `${prop}:${obj[prop]};`;
  }

  return str;
}

function mousedown(isTouch = false) {
  const MOVE = isTouch ? 'touchmove' : 'mousemove';
  const END = isTouch ? 'touchend' : 'mouseup';

  return function caller (evt) {
    const target = parentsUntilMatch(evt.target, '.js-is-draggable');

    if (!target) return;

    mover = target;
    offsets = { X: evt.pageX, Y: evt.pageY, };
    position = offsetPosition(mover);
    mover.setAttribute('style', styleStringFromObj({
      position: 'relative',
      top: `${position.Y}px`,
      left: `${position.X}px`
    }));

    mover.classList.add('js-is-dragging');
    mover.setAttribute('aria-grabbed', true);

    addEvent(MOVE, mousemove);
    addEvent(END, mouseup(isTouch));
  }
}
function mousemove(evt) {
  // evt.preventDefault();
  // new offsets based on mouse position
  const newOffsets = {
    X: evt.pageX,
    Y: evt.pageY,
  };

  const newY = position.Y + (newOffsets.Y - offsets.Y);
  const newX = position.X + (newOffsets.X - offsets.X);

  // update styles
  mover.style.top = newY + 'px';
  mover.style.left = newX + 'px';

  // update object
  offsets = newOffsets;

  // update position
  position = { X: newX, Y: newY };
}
function mouseup(isTouch = false) {
  const MOVE = isTouch ? 'touchmove' : 'mousemove';
  const END = isTouch ? 'touchend' : 'mouseup';
  
  return function caller(evt) {
    removeEvent(MOVE, mousemove);
    removeEvent(END, caller);

    mover.classList.remove('js-is-dragging');
    mover.setAttribute('aria-grabbed', false);

    mover = offsets = position = undefined;
  }
}

addEvent('mousedown', mousedown());
addEvent('touchstart', mousedown(true));

              
            
!
999px

Console