StealThis .dev

Drag & Drop List

A drag-and-drop sortable list using the native HTML5 Drag and Drop API. Items animate to their new positions with smooth CSS transitions. Zero dependencies.

Open in Lab
vanilla-js css drag-api html5
Targets: JS HTML

Code

Drag & Drop List

A smooth drag-and-drop sortable list using the native HTML5 Drag and Drop API — no libraries, no dependencies. Items reorder in real-time as you drag over them.

How it works

  1. Each <li> has draggable="true" and a drag handle icon
  2. dragstart stores a reference to the dragged element
  3. dragover on sibling items determines insertion point (before or after) using getBoundingClientRect midpoint
  4. The list re-inserts the dragged node in real-time via insertBefore / appendChild
  5. Visual feedback: dragged item gets .dragging opacity; hovered items get a top/bottom border hint

Accessibility note

The native drag API has limited keyboard/screen-reader support. For production, pair with a keyboard-accessible fallback (arrow keys to move items).

When to use it

  • Todo / task list ordering
  • Kanban column sorting
  • Dashboard widget rearrangement