Bootstrap 5 Draggable Image Card Grid
A responsive image-card grid you can reorder by dragging a card, with keyboard arrow-key support and a shuffle button.
<section class="wdw-stage">
<div class="container" style="max-width: 1040px">
<div class="d-flex flex-wrap align-items-end justify-content-between gap-3 mb-4">
<div>
<span class="wdw-eyebrow">Moodboard</span>
<h2 class="wdw-heading h2 mb-1">Drag to arrange your board</h2>
<p class="wdw-muted mb-0" id="sortHint">Drag a card by its handle, with mouse or touch. With the handle focused, use the arrow keys to move it.</p>
</div>
<button type="button" class="btn btn-outline-primary rounded-pill" id="shuffleBtn">Shuffle</button>
</div>
<ul class="list-unstyled mb-0 wdw-board" id="sortable" aria-label="Reorderable gallery">
<li data-title="Forest light">
<article class="wdw-tile">
<img src="https://images.unsplash.com/photo-1441974231531-c6227db76b6e?w=900&h=700&fit=crop&q=80" alt="Sunlight through a forest" draggable="false">
<div class="wdw-tile-cap"><h3 class="h6 mb-1">Forest light</h3><p class="small mb-0">Soft greens and warm shafts of sun.</p></div>
<button type="button" class="wdw-handle" aria-label="Reorder Forest light" aria-describedby="sortHint"></button>
</article>
</li>
<li data-title="Table spread">
<article class="wdw-tile">
<img src="https://images.unsplash.com/photo-1414235077428-338989a2e8c0?w=900&h=700&fit=crop&q=80" alt="A plated restaurant dish" draggable="false">
<div class="wdw-tile-cap"><h3 class="h6 mb-1">Table spread</h3><p class="small mb-0">Plating ideas for a seasonal menu.</p></div>
<button type="button" class="wdw-handle" aria-label="Reorder Table spread" aria-describedby="sortHint"></button>
</article>
</li>
<li data-title="Live night">
<article class="wdw-tile">
<img src="https://images.unsplash.com/photo-1429962714451-bb934ecdc4ec?w=900&h=700&fit=crop&q=80" alt="A crowd at a concert making a heart with their hands" draggable="false">
<div class="wdw-tile-cap"><h3 class="h6 mb-1">Live night</h3><p class="small mb-0">Crowd energy for an events page.</p></div>
<button type="button" class="wdw-handle" aria-label="Reorder Live night" aria-describedby="sortHint"></button>
</article>
</li>
<li data-title="Quiet water">
<article class="wdw-tile">
<img src="https://images.unsplash.com/photo-1439066615861-d1af74d74000?w=900&h=700&fit=crop&q=80" alt="A calm lake at dusk" draggable="false">
<div class="wdw-tile-cap"><h3 class="h6 mb-1">Quiet water</h3><p class="small mb-0">Calm tones for the footer area.</p></div>
<button type="button" class="wdw-handle" aria-label="Reorder Quiet water" aria-describedby="sortHint"></button>
</article>
</li>
<li data-title="Desk setup">
<article class="wdw-tile">
<img src="https://images.unsplash.com/photo-1423666639041-f56000c27a9a?w=900&h=700&fit=crop&q=80" alt="Hands holding a phone above a laptop" draggable="false">
<div class="wdw-tile-cap"><h3 class="h6 mb-1">Desk setup</h3><p class="small mb-0">Workspace shots for the about page.</p></div>
<button type="button" class="wdw-handle" aria-label="Reorder Desk setup" aria-describedby="sortHint"></button>
</article>
</li>
</ul>
<p class="visually-hidden" id="sortStatus" role="status" aria-live="polite"></p>
</div>
</section>
/* Web Designer Wall library style — shared base for every Bootstrap 5 snippet.
Indigo accent, Plus Jakarta Sans, soft cards, pill buttons. Works in light and dark. */
@import url("https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap");
:root,
[data-bs-theme="light"] {
--wdw-font: "Plus Jakarta Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
--bs-body-font-family: var(--wdw-font);
--bs-primary: #4f46e5;
--bs-primary-rgb: 79, 70, 229;
--bs-primary-text-emphasis: #3730a3;
--bs-primary-bg-subtle: #eef2ff;
--bs-primary-border-subtle: #c7d2fe;
--bs-link-color: #4f46e5;
--bs-link-color-rgb: 79, 70, 229;
--bs-link-hover-color: #4338ca;
--bs-link-hover-color-rgb: 67, 56, 202;
--bs-focus-ring-color: rgba(79, 70, 229, 0.25);
--bs-border-radius: 0.75rem;
--bs-border-radius-lg: 1rem;
--bs-border-radius-xl: 1.25rem;
--wdw-radius-card: 1.25rem;
--wdw-shadow: 0 1px 2px rgba(16, 24, 40, 0.05), 0 16px 40px -16px rgba(16, 24, 40, 0.22);
--wdw-shadow-sm: 0 1px 2px rgba(16, 24, 40, 0.06), 0 4px 12px -4px rgba(16, 24, 40, 0.12);
--wdw-stage: #f5f6fb;
}
[data-bs-theme="dark"] {
--bs-primary: #818cf8;
--bs-primary-rgb: 129, 140, 248;
--bs-primary-text-emphasis: #c7d2fe;
--bs-primary-bg-subtle: rgba(129, 140, 248, 0.14);
--bs-primary-border-subtle: rgba(129, 140, 248, 0.35);
--bs-link-color: #a5b4fc;
--bs-link-color-rgb: 165, 180, 252;
--bs-link-hover-color: #c7d2fe;
--bs-link-hover-color-rgb: 199, 210, 254;
--bs-focus-ring-color: rgba(129, 140, 248, 0.35);
--wdw-shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 16px 40px -16px rgba(0, 0, 0, 0.7);
--wdw-shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.4), 0 4px 12px -4px rgba(0, 0, 0, 0.5);
--wdw-stage: #0f1117;
}
body { font-family: var(--wdw-font); -webkit-font-smoothing: antialiased; }
/* Bootstrap compiles its blue into these components; point them at the accent. */
.btn-primary {
--bs-btn-bg: var(--bs-primary); --bs-btn-border-color: var(--bs-primary);
--bs-btn-hover-bg: #4338ca; --bs-btn-hover-border-color: #4338ca;
--bs-btn-active-bg: #3730a3; --bs-btn-active-border-color: #3730a3;
--bs-btn-disabled-bg: var(--bs-primary); --bs-btn-disabled-border-color: var(--bs-primary);
--bs-btn-focus-shadow-rgb: var(--bs-primary-rgb);
}
[data-bs-theme="dark"] .btn-primary { --bs-btn-color: #0f1117; --bs-btn-hover-bg: #a5b4fc; --bs-btn-hover-border-color: #a5b4fc; --bs-btn-hover-color: #0f1117; --bs-btn-active-color: #0f1117; }
.btn-outline-primary {
--bs-btn-color: var(--bs-primary); --bs-btn-border-color: var(--bs-primary);
--bs-btn-hover-bg: var(--bs-primary); --bs-btn-hover-border-color: var(--bs-primary);
--bs-btn-active-bg: var(--bs-primary); --bs-btn-active-border-color: var(--bs-primary);
--bs-btn-focus-shadow-rgb: var(--bs-primary-rgb);
}
.btn { --bs-btn-font-weight: 600; min-height: 44px; display: inline-flex; align-items: center; justify-content: center; gap: .5rem; }
.btn-sm { min-height: 36px; }
.form-control, .form-select { min-height: 44px; border-radius: .75rem; }
.form-control:focus, .form-select:focus, .form-check-input:focus { border-color: rgba(var(--bs-primary-rgb), .6); box-shadow: 0 0 0 .25rem var(--bs-focus-ring-color); }
.form-check-input:checked { background-color: var(--bs-primary); border-color: var(--bs-primary); }
.nav-pills { --bs-nav-pills-link-active-bg: var(--bs-primary); }
.nav { --bs-nav-link-color: var(--bs-secondary-color); --bs-nav-link-hover-color: var(--bs-emphasis-color); }
.pagination { --bs-pagination-active-bg: var(--bs-primary); --bs-pagination-active-border-color: var(--bs-primary); --bs-pagination-color: var(--bs-body-color); --bs-pagination-focus-box-shadow: 0 0 0 .25rem var(--bs-focus-ring-color); }
.progress, .progress-stacked { --bs-progress-bar-bg: var(--bs-primary); }
.list-group { --bs-list-group-active-bg: var(--bs-primary); --bs-list-group-active-border-color: var(--bs-primary); }
.dropdown-menu { --bs-dropdown-link-active-bg: var(--bs-primary); border-radius: .875rem; box-shadow: var(--wdw-shadow-sm); }
.accordion { --bs-accordion-active-bg: var(--bs-primary-bg-subtle); --bs-accordion-active-color: var(--bs-primary-text-emphasis); --bs-accordion-btn-focus-box-shadow: 0 0 0 .25rem var(--bs-focus-ring-color); }
.badge { font-weight: 600; }
/* Library building blocks — use these instead of re-inventing them per snippet. */
.wdw-stage { min-height: 100vh; display: flex; align-items: center; background: var(--wdw-stage); padding-block: 3rem; }
.wdw-card { background: var(--bs-body-bg); border: 1px solid var(--bs-border-color-translucent); border-radius: var(--wdw-radius-card); box-shadow: var(--wdw-shadow); }
.wdw-card-sm { background: var(--bs-body-bg); border: 1px solid var(--bs-border-color-translucent); border-radius: 1rem; box-shadow: var(--wdw-shadow-sm); }
.wdw-aurora { position: relative; color: #fff; background: radial-gradient(60% 55% at 100% 0, #818cf8 0, transparent 70%), radial-gradient(65% 60% at 0 100%, #ec4899 0, transparent 65%), #1e1b4b; }
.wdw-eyebrow { display: inline-block; font-size: .75rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase; color: var(--bs-primary); }
.wdw-heading { font-weight: 800; letter-spacing: -.02em; color: var(--bs-emphasis-color); }
.wdw-muted { color: var(--bs-secondary-color); }
.wdw-icon { width: 2.75rem; height: 2.75rem; flex: none; display: inline-flex; align-items: center; justify-content: center; border-radius: .875rem; background: var(--bs-primary-bg-subtle); color: var(--bs-primary); }
/* Component */
.wdw-board { display: grid; grid-template-columns: repeat(2, 1fr); grid-auto-rows: 11rem; gap: 1rem; }
.wdw-board > li:first-child { grid-column: span 2; grid-row: span 2; }
@media (min-width: 992px) { .wdw-board { grid-template-columns: repeat(4, 1fr); grid-auto-rows: 13rem; gap: 1.25rem; } }
.wdw-tile { position: relative; height: 100%; overflow: hidden; border-radius: 1rem; box-shadow: var(--wdw-shadow-sm); background: var(--bs-tertiary-bg); transition: transform .2s, box-shadow .2s; }
.wdw-tile img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.wdw-tile-cap { position: absolute; inset: auto 0 0 0; padding: 2.5rem 1rem .9rem; color: #fff; background: linear-gradient(transparent, rgba(15,17,23,.78)); }
.wdw-tile-cap p { color: rgba(255,255,255,.85); }
.wdw-board > li:not(:first-child) .wdw-tile-cap p { display: none; }
.wdw-board > li:first-child .wdw-tile-cap h3 { font-size: 1.25rem; }
.wdw-board > li:hover .wdw-tile { transform: translateY(-3px); box-shadow: var(--wdw-shadow); }
.wdw-board > li.dragging { opacity: .55; }
.wdw-board > li.dragging .wdw-tile { transform: scale(.97); }
.wdw-handle { position: absolute; top: .6rem; right: .6rem; width: 2.75rem; height: 2.75rem; border: 0; border-radius: 50%; cursor: grab; touch-action: none; background: rgba(15,17,23,.6) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='%23fff'%3E%3Ccircle cx='9' cy='6' r='1.7'/%3E%3Ccircle cx='15' cy='6' r='1.7'/%3E%3Ccircle cx='9' cy='12' r='1.7'/%3E%3Ccircle cx='15' cy='12' r='1.7'/%3E%3Ccircle cx='9' cy='18' r='1.7'/%3E%3Ccircle cx='15' cy='18' r='1.7'/%3E%3C/svg%3E") center/1.25rem no-repeat; backdrop-filter: blur(4px); }
.wdw-handle:active { cursor: grabbing; }
.wdw-handle:focus-visible { outline: 3px solid var(--bs-primary); outline-offset: 2px; box-shadow: 0 0 0 5px #fff; }
function initSortableGrid() {
var list = document.getElementById('sortable');
if (!list) return;
var status = document.getElementById('sortStatus');
var dragged = null;
var items = function () { return Array.prototype.slice.call(list.children); };
var announce = function (el) { if (status) status.textContent = el.dataset.title + ' moved to position ' + (items().indexOf(el) + 1) + ' of ' + items().length; };
list.addEventListener('pointerdown', function (e) {
var handle = e.target.closest('.wdw-handle');
if (!handle || (e.pointerType === 'mouse' && e.button !== 0)) return;
dragged = handle.closest('li');
dragged.classList.add('dragging');
e.preventDefault();
});
document.addEventListener('pointermove', function (e) {
if (!dragged) return;
var el = document.elementFromPoint(e.clientX, e.clientY);
var target = el && el.closest('#sortable > li');
if (!target || target === dragged) return;
var r = target.getBoundingClientRect();
// swap only when the pointer is near the target centre on both axes
var all = items();
var from = all.indexOf(dragged), to = all.indexOf(target);
var dx = Math.abs(e.clientX - (r.left + r.width / 2)) / r.width;
var dy = Math.abs(e.clientY - (r.top + r.height / 2)) / r.height;
if (Math.max(dx, dy) > 0.35) return;
list.insertBefore(dragged, to > from ? target.nextSibling : target);
});
function end() {
if (!dragged) return;
dragged.classList.remove('dragging');
announce(dragged);
dragged = null;
}
document.addEventListener('pointerup', end);
document.addEventListener('pointercancel', end);
list.addEventListener('keydown', function (e) {
var handle = e.target.closest('.wdw-handle');
if (!handle) return;
var item = handle.closest('li');
var prev = item.previousElementSibling, next = item.nextElementSibling;
if ((e.key === 'ArrowLeft' || e.key === 'ArrowUp') && prev) { list.insertBefore(item, prev); }
else if ((e.key === 'ArrowRight' || e.key === 'ArrowDown') && next) { list.insertBefore(next, item); }
else return;
e.preventDefault();
handle.focus();
announce(item);
});
}
function initShuffle() {
var btn = document.getElementById('shuffleBtn');
var list = document.getElementById('sortable');
if (!btn || !list) return;
btn.addEventListener('click', function () {
var arr = Array.prototype.slice.call(list.children);
for (var i = arr.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); var t = arr[i]; arr[i] = arr[j]; arr[j] = t; }
arr.forEach(function (el) { list.appendChild(el); });
});
}
initSortableGrid();
initShuffle();
Built on Bootstrap 5.3.8 with plain JavaScript — no jQuery. Supports light and dark mode via data-bs-theme.