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="anchor-element js-anchor-element">
      <svg
        xmlns="http://www.w3.org/2000/svg"
        viewBox="0 -960 960 960"
        width="24"
      >
        <path
          d="M480-80q-61 0-125-22t-116-60q-52-38-85.5-89T120-360v-120l160 120-62 62q29 51 92 88t130 47v-357H320v-80h120v-47q-35-13-57.5-43.5T360-760q0-50 35-85t85-35q50 0 85 35t35 85q0 39-22.5 69.5T520-647v47h120v80H520v357q67-10 130-47t92-88l-62-62 160-120v120q0 58-33.5 109T721-162q-52 38-116 60T480-80Zm0-640q17 0 28.5-11.5T520-760q0-17-11.5-28.5T480-800q-17 0-28.5 11.5T440-760q0 17 11.5 28.5T480-720Z"
        />
      </svg>
    </div>

    <div class="message js-position-element" contenteditable>
      Positioned Element
    </div>

    <h2>Drag the anchor towards edges</h2>
              
            
!

CSS

              
                @layer tokens, reset, animations, base, components, theme, demo;

@layer demo {
  .message {
    position: absolute;
    position-anchor: --anchor-element;
    position-area: top left;
    position-try-fallbacks: flip-block, flip-inline, flip-block flip-inline;
  }

  .anchor-element {
    position: absolute;
    top: var(--y, 30%);
    left: var(--x, 70%);
    z-index: 1;
    cursor: grab;

    &:active {
      cursor: grabbing;
    }
  }

  h2 {
    color: var(--gray-7);
    &::selection {
      background-color: transparent;
    }
  }
}

@layer tokens {
  :where(html) {
    --gray-0: rgb(248, 249, 250);
    --gray-1: rgb(241, 243, 245);
    --gray-2: rgb(233, 236, 239);
    --gray-3: rgb(222, 226, 230);
    --gray-4: rgb(206, 212, 218);
    --gray-5: rgb(173, 181, 189);
    --gray-6: rgb(134, 142, 150);
    --gray-7: rgb(73, 80, 87);
    --gray-8: rgb(52, 58, 64);
    --gray-9: rgb(33, 37, 41);

    --blue-0: rgb(231, 245, 255);
    --blue-1: rgb(208, 235, 255);
    --blue-2: rgb(165, 216, 255);
    --blue-3: rgb(116, 192, 252);
    --blue-4: rgb(77, 171, 247);
    --blue-5: rgb(51, 154, 240);
    --blue-6: rgb(34, 139, 230);
    --blue-7: rgb(28, 126, 214);
    --blue-8: rgb(25, 113, 194);
    --blue-9: rgb(24, 100, 171);

    --indigo-0: #edf2ff;
    --indigo-1: #dbe4ff;
    --indigo-2: #bac8ff;
    --indigo-3: #91a7ff;
    --indigo-4: #748ffc;
    --indigo-5: #5c7cfa;
    --indigo-6: #4c6ef5;
    --indigo-7: #4263eb;
    --indigo-8: #3b5bdb;
    --indigo-9: #364fc7;

    --ease-in-out: cubic-bezier(0.4, 0, 0.2, 1);
    --ease-out: cubic-bezier(0, 0, 0.2, 1);
    --ease-in: cubic-bezier(0.4, 0, 1, 1);
    --sharp: cubic-bezier(0.4, 0, 0.6, 1);
    --ease-spring: linear(
      0,
      0.006,
      0.025 2.8%,
      0.101 6.1%,
      0.539 18.9%,
      0.721 25.3%,
      0.849 31.5%,
      0.937 38.1%,
      0.968 41.8%,
      0.991 45.7%,
      1.006 50.1%,
      1.015 55%,
      1.017 63.9%,
      1.001
    );

    --font-family-system: system-ui, sans-serif;
    --font-family-sans-serif: Inter, Roboto, Helvetica Neue, Arial Nova,
      Nimbus Sans, Arial, sans-serif;

    --font-line-height-1: 1;
    --font-line-height-2: 1.25;
    --font-line-height-3: 1.375;
    --font-line-height-4: 1.5;

    --font-size-1: 1rem;
    --font-size-2: 1.1rem;
    --font-size-3: 1.25rem;
    --font-size-4: 1.5rem;
    --font-size-5: 2rem;
    --font-size-6: 2.5rem;
    --font-size-7: 3rem;
    --font-size-8: 3.5rem;

    --font-weight-1: 100;
    --font-weight-2: 200;
    --font-weight-3: 300;
    --font-weight-4: 400;
    --font-weight-5: 500;
    --font-weight-6: 600;
    --font-weight-7: 700;
    --font-weight-8: 800;
    --font-weight-9: 900;

    --size-1: 0.5rem;
    --size-2: calc(2 * var(--size-1));
    --size-3: calc(3 * var(--size-1));
    --size-4: calc(4 * var(--size-1));
    --size-5: calc(5 * var(--size-1));
    --size-6: calc(6 * var(--size-1));
    --size-7: calc(7 * var(--size-1));
    --size-8: calc(8 * var(--size-1));
    --size-9: calc(9 * var(--size-1));
    --size-10: calc(10 * var(--size-1));

    --radius-1: 1rem;

    --duration-1: 100ms;
    --duration-2: 150ms;
    --duration-3: 200ms;
    --duration-4: 400ms;
  }
}

@layer reset {
  *,
  ::before,
  ::after {
    box-sizing: border-box;
  }

  :where(*) {
    margin: 0;
    padding: 0;
  }

  :where(h1, h2, h3, h4, h5, h6) {
    font-weight: var(--font-weight-9);
    line-height: var(--font-line-height-1);
  }

  :where(h1) {
    font-size: var(--font-size-8);
  }

  :where(h2) {
    font-size: var(--font-size-6);
  }

  :where(h3) {
    font-size: var(--font-size-5);
  }

  :where(h4) {
    font-size: var(--font-size-4);
  }

  :where(h5) {
    font-size: var(--font-size-3);
  }

  :where(a[href]) {
    color: var(--link);
  }

  :where(a[href]):where(:visited) {
    color: var(--link-visited);
  }

  :focus-visible {
    outline-color: var(--brand, var(--link));
  }

  :where(input, button, textarea, select),
  :where(input[type="file"])::-webkit-file-upload-button {
    font: inherit;
    font-size: inherit;
    color: inherit;
    letter-spacing: inherit;
  }

  ::placeholder {
    color: var(--gray-3);
    opacity: 0.75;
  }

  :where(input:not([type="range"]), textarea, select) {
    padding: 0.5rem 0.25rem;
  }

  :where(textarea, select, input:not(button)) {
    background-color: var(--surface-2);
  }

  :where(ul[class]) {
    list-style: none;
  }

  fieldset {
    border: 0;
    margin: 0;
    padding: 0;
  }

  legend {
    font-weight: 600;
    font-size: 1.25rem;
    margin-bottom: var(--size-2);
    padding-inline: var(--size-1);
  }
}

@layer animations {
  @keyframes scale-out {
    to {
      scale: 0;
    }
  }

  @keyframes fade-in {
    to {
      opacity: 1;
    }
  }
}

@layer base {
  :where(html) {
    height: 100%;
    display: grid;

    font-family: var(--font-family-sans-serif);
    line-height: var(--font-line-height-4);

    color: var(--text-1);
    background-color: var(--surface-1);
  }

  :where(body) {
    place-items: center;

    display: grid;
    overflow: hidden;

    &:before {
      content: "";

      top: 0;
      left: 0;
      z-index: -1;
      position: fixed;

      width: 100vw;
      height: 100vh;

      display: block;
      pointer-events: none;

      mask: linear-gradient(-30deg, transparent 50%, white);
      background: linear-gradient(
            90deg,
            var(--dot-bg) calc(var(--dot-space) - var(--dot-size)),
            transparent 1%
          )
          center / var(--dot-space) var(--dot-space),
        linear-gradient(
            var(--dot-bg) calc(var(--dot-space) - var(--dot-size)),
            transparent 1%
          )
          center / var(--dot-space) var(--dot-space),
        var(--dot-color);
    }
  }

  ::-webkit-scrollbar-track {
    background: transparent;
    cursor: pointer;
  }

  ::-webkit-scrollbar-thumb {
    background-color: var(--indigo-3);
    cursor: pointer;
    border-radius: var(--radius-1);
  }

  ::-webkit-scrollbar {
    height: var(--size-1);
    width: var(--size-1);
    background: transparent;
    cursor: pointer;
  }

  @media (prefers-reduced-motion: no-preference) {
    :where(*) {
      scroll-behavior: smooth;
    }
  }
}

@layer components {
  .container {
    width: 55ch;
    display: grid;

    gap: var(--size-2);
  }

  .chip {
    position: relative;

    cursor: pointer;

    border: 1px solid var(--gray-7);
    padding: var(--size-1) var(--size-2);

    border-radius: var(--radius-1);
    background-color: var(--surface-1);

    line-height: 1;
    transition: color var(--duration-4) var(--ease-spring),
      background-color var(--duration-4) var(--ease-spring);

    ::selection {
      background: transparent;
    }

    &:after {
      content: "";

      position: absolute;
      inset: -1px;

      color: inherit;
      opacity: 0;
      border-radius: inherit;
      pointer-events: none;

      border: 1px solid var(--pop);

      transition: opacity 400ms var(--ease-spring);
    }

    &:has(:checked) {
      color: var(--pop);
      background-color: var(--pop-dark);

      &:after {
        opacity: 1;
      }
    }

    &:hover:has(:not(:checked)) {
      background-color: var(--pop-dark);

      &:after {
        opacity: 0.2;
      }
    }

    [type="radio"] {
      position: absolute;
      width: 1px;
      height: 1px;
      padding: 0;
      margin: -1px;
      overflow: hidden;
      clip: rect(0, 0, 0, 0);
      white-space: nowrap;
      border-width: 0;
    }
  }

  .chip-list {
    flex-wrap: wrap;

    display: flex;
    list-style-type: none;

    gap: var(--size-1);
    margin: 0;
    padding: 0;
  }

  .message {
    --message-tip-size: var(--size-1);
    z-index: 100;

    padding: var(--size-1) var(--size-2);

    line-height: 1;
    white-space: nowrap;
    display: grid;
    place-items: center;

    border-radius: var(--radius-1);
    background-color: var(--surface-4);

    &.pointer {
      margin: var(--message-tip-size);

      &:after {
        content: " ";
        position: absolute;
        top: 100%;
        left: 50%;
        margin-left: calc(-1 * var(--message-tip-size));
        border-width: var(--message-tip-size);
        border-style: solid;
        border-color: var(--surface-4) transparent transparent transparent;
      }
    }
  }

  .anchor-element {
    anchor-name: --anchor-element;

    place-items: center;

    display: grid;

    width: var(--size-8);
    aspect-ratio: 1;
    border: 1px solid var(--divider);

    border-radius: 22%;
    background-color: var(--surface-2);

    > svg {
      width: 62%;

      display: block;
      opacity: 0.35;

      fill: var(--gray-1);
    }
  }
}

@layer theme {
  :where(html) {
    --pop: var(--indigo-3);
    --pop-dark: color-mix(in srgb, var(--pop) 10%, var(--surface-1));

    --surface-1: var(--gray-9);
    --surface-2: var(--gray-8);
    --surface-3: var(--gray-7);
    --surface-4: var(--gray-6);

    --divider: var(--gray-7);

    --text-1: var(--gray-1);
    --text-2: var(--gray-2);

    --link: var(--blue-6);
    --link-visited: var(--blue-4);

    --dot-bg: var(--surface-1);
    --dot-color: var(--gray-7);
    --dot-size: 0.125rem;
    --dot-space: 1.5rem;
  }
}

              
            
!

JS

              
                /**
 * Makes an element draggable by updating its position with custom CSS
 * properties --x and --y.
 *
 * @param {HTMLElement} element - The element to make draggable.
 */
function makeElementDraggable(element) {
  let xOffset = 0;
  let yOffset = 0;

  element.addEventListener("mousedown", onMouseDown);

  /**
   * Handles the mousedown event to initiate dragging.
   *
   * @param {MouseEvent} event - The mousedown event.
   */
  function onMouseDown(event) {
    xOffset = event.clientX;
    yOffset = event.clientY;

    document.addEventListener("mousemove", onMouseMove);
    document.addEventListener("mouseup", onMouseUp);
  }

  /**
   * Handles the mousemove event to update element position.
   *
   * @param {MouseEvent} event - The mousemove event.
   */
  function onMouseMove(event) {
    const deltaX = event.clientX - xOffset;
    const deltaY = event.clientY - yOffset;

    // Update offsets to current mouse position
    xOffset = event.clientX;
    yOffset = event.clientY;

    element.style.setProperty("--y", `${element.offsetTop + deltaY}px`);
    element.style.setProperty("--x", `${element.offsetLeft + deltaX}px`);
  }

  /**
   * Handles the mouseup event to stop dragging.
   */
  function onMouseUp() {
    document.removeEventListener("mouseup", onMouseUp);
    document.removeEventListener("mousemove", onMouseMove);
  }
}

// Bind the drag setup to all anchor elements
document.querySelectorAll(".js-anchor-element").forEach(makeElementDraggable);

              
            
!
999px

Console