Source Code

<div class="ig-wrap">
  <h2 class="ig-heading">Gallery — drag to reorder</h2>
  <div class="ig-grid" id="igGrid"></div>
</div>

Drag & Drop Image Sort Gallery — Free HTML CSS JS Snippet

Drag & Drop Image Sort Gallery · Layouts · Plain HTML, CSS & JS · Live preview

What's included

Features

Native HTML5 drag-and-drop API — draggable, dragstart, dragover, dragleave, drop, dragend
Single images array as the source of truth; the grid fully re-renders from data after every reorder
Numbered position badge on each tile that updates automatically after a reorder
Visual drag-over highlight border on the tile currently being hovered during a drag
Faded opacity on the tile actively being dragged for clear feedback
Responsive grid that collapses from 4 to 2 columns on narrow viewports
Uses picsum.photos seeded URLs so every tile shows a distinct real image

About this UI Snippet

Drag & Drop Image Sort Gallery — Reorderable Grid With HTML5 Drag-and-Drop

Screenshot of the Drag & Drop Image Sort Gallery snippet rendered live

This snippet is an image gallery grid whose tiles can be reordered by dragging one onto another, implemented with the native HTML5 drag-and-drop API rather than any sorting library.

Data-driven grid

An images array (each entry an id plus a picsum.photos URL) is the single source of truth. renderGrid() clears the grid container and rebuilds every tile from that array on each render, including a numbered badge in the corner showing the tile's current position.

Drag events per tile

Every tile is draggable="true". dragstart records the dragged tile's id both in a module-level dragId variable and via dataTransfer.setData, and applies a faded .ig-dragging class. Each tile also listens for dragover (calling preventDefault() to allow dropping, and highlighting itself with .ig-drag-over unless it's the tile currently being dragged) and dragleave to remove that highlight when the cursor moves off.

Swapping positions on drop

On drop, the handler reads the source tile's id back out of dataTransfer, looks up both the source and target index in the images array, removes the source entry with splice and reinserts it at the target's index — effectively moving the dragged image to land exactly where it was dropped, shifting everything in between. renderGrid() then rebuilds the grid so the numbered badges and DOM order both reflect the new sequence.

Step by step

How to Use

  1. 1
    Load the snippetClick "Drag & Drop Image Sort Gallery" in the sidebar to load its HTML, CSS, and JS into the editor panels. The preview updates instantly.
  2. 2
    Edit the codeModify any panel — HTML, CSS, or JS. The preview refreshes as you type. Use Reset in each panel header to restore the original.
  3. 3
    Preview on devicesClick the Mobile (375px), Tablet (768px), or Desktop buttons in the preview header to check responsiveness.
  4. 4
    Export in your formatClick "HTML" to download a standalone file, "JSX" for a React component, "Tailwind" for a React + Tailwind CSS component, "Tailwind HTML" for a standalone HTML file with Tailwind CDN, "Vue" for a Vue 3 SFC with <template>/<script setup>/<style scoped>, or "Angular" for a standalone Angular .component.ts file. "Copy all" copies the full code to clipboard.
  5. 5
    Save your versionClick "Save as", type a name, and press Enter. Your snippet saves to IndexedDB and appears in the Saved tab.

Real-world uses

Common Use Cases

Photo and portfolio galleries
Let users manually curate image order in a portfolio, album, or media library.
CMS media managers
Reorder a product's image gallery or a blog post's attached images by drag.
Reference for grid-based drag reordering
Shows how to compute and apply array splice-based reordering from drag events.
Teaching HTML5 drag-and-drop with grids
A clear example of tracking source and target positions across a 2D grid layout.

Got questions?

Frequently Asked Questions

The drop handler reads the dragged tile's id via dataTransfer, finds both its current index and the target tile's index in the images array, then uses Array.splice to remove it from the old position and reinsert it at the new one before re-rendering the grid.

It prevents the tile currently being dragged from highlighting itself, since dragover events can still fire on the source element as the cursor passes over it.

Yes, replace the url values in the images array (built from the SEEDS array) with any image URLs — the drag-and-drop and reordering logic works independently of where the image comes from.

The native HTML5 drag-and-drop API used here has inconsistent touch support across mobile browsers. For a touch-first version, pointer events with manual position tracking would be needed instead.