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

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

CategoryLayouts

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 curate image order by dragging tiles onto each other, using the native HTML5 drag-and-drop API without any sorting library.
🗂️
CMS media managers
Reorder a product's image gallery, with a numbered position badge on every tile that updates automatically after each reorder.
📚
Grid drag reordering reference
Show how to combine native drag events with a CSS grid, re-rendering the whole grid from one images array after each drop.
🎓
HTML5 drag teaching
Teach dragstart, dragover, dragleave and drop together, with a visual highlight border on the tile currently being hovered.

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.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Hand this snippet's HTML, CSS, and JavaScript to an AI coding assistant like Claude and ask it to adapt "Drag & Drop Image Sort Gallery" to your project — restyle it to match your design system, wire it up to real data instead of the static example, or port it to the framework you're building in.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a layouts component called "Drag & Drop Image Sort Gallery" using plain HTML, CSS, and vanilla JavaScript — no framework, no build step.

Requirements:
- Match the structure and behavior of the "Drag & Drop Image Sort Gallery" snippet from the UI Snippets Library.
- Keep the markup semantic and the styling self-contained (no external dependencies beyond what the original snippet uses).
- Keep the JavaScript vanilla, with no framework runtime required.
- Make it easy to restyle via CSS custom properties or class overrides so it can be dropped into a real project.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Source Code

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