HTML Drag and Drop Get Certified
What is HTML5 Drag and Drop?
The HTML5 Drag and Drop API lets users click and drag elements around a page (or drop files from their computer). You make an element draggable with the draggable="true" attribute and then listen for events such as dragstart, dragover, and drop.
Common Use Cases
- Reordering list items
- Moving items between containers
- File upload drop zones
- Kanban-style boards
HTML Drag and Drop Code Examples
1. Basic Drag and Drop
A simple example where you can drag a box into a drop zone.
What this code does: Makes a box draggable and allows it to be dropped into a dashed container.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Basic Drag and Drop</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }
#dropZone {
width: 300px;
height: 150px;
border: 3px dashed #999;
display: flex;
align-items: center;
justify-content: center;
margin-bottom: 20px;
background: white;
}
#dragItem {
width: 120px;
padding: 20px;
background: #3498db;
color: white;
text-align: center;
cursor: grab;
border-radius: 6px;
user-select: none;
}
</style>
</head>
<body>
<h2>Basic Drag and Drop</h2>
<div id="dropZone">Drop here</div>
<div id="dragItem" draggable="true">Drag Me</div>
<script>
const dragItem = document.getElementById("dragItem");
const dropZone = document.getElementById("dropZone");
dragItem.addEventListener("dragstart", function(e) {
e.dataTransfer.setData("text/plain", e.target.id);
});
dropZone.addEventListener("dragover", function(e) {
e.preventDefault(); // Necessary to allow dropping
});
dropZone.addEventListener("drop", function(e) {
e.preventDefault();
const id = e.dataTransfer.getData("text/plain");
const element = document.getElementById(id);
dropZone.appendChild(element);
dropZone.textContent = ""; // clear the "Drop here" text
dropZone.appendChild(element);
});
</script>
</body>
</html>
2. File Drop Zone
Create a drop area that accepts files from the user’s computer.
What this code does: Shows the names of the files that the user drops onto the zone.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>File Drop Zone</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }
#dropZone {
width: 100%;
max-width: 400px;
height: 180px;
border: 3px dashed #666;
display: flex;
align-items: center;
justify-content: center;
background: white;
font-size: 18px;
color: #555;
transition: background 0.2s;
}
#dropZone.dragover {
background: #e8f4fd;
border-color: #3498db;
}
#fileList {
margin-top: 15px;
font-size: 16px;
}
</style>
</head>
<body>
<h2>File Drop Zone</h2>
<div id="dropZone">Drop files here</div>
<div id="fileList"></div>
<script>
const dropZone = document.getElementById("dropZone");
const fileList = document.getElementById("fileList");
dropZone.addEventListener("dragover", function(e) {
e.preventDefault();
dropZone.classList.add("dragover");
});
dropZone.addEventListener("dragleave", function() {
dropZone.classList.remove("dragover");
});
dropZone.addEventListener("drop", function(e) {
e.preventDefault();
dropZone.classList.remove("dragover");
const files = e.dataTransfer.files;
if (files.length === 0) return;
let names = [];
for (let i = 0; i < files.length; i++) {
names.push(files[i].name);
}
fileList.innerHTML = "<strong>Dropped files:</strong><br>" + names.join("<br>");
});
</script>
</body>
</html>
3. Sortable List
Drag list items to reorder them.
What this code does: Lets you rearrange the items in a list by dragging them up or down.
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Sortable List</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }
ul {
list-style: none;
padding: 0;
max-width: 300px;
}
li {
padding: 14px 18px;
margin-bottom: 8px;
background: white;
border: 1px solid #ddd;
border-radius: 6px;
cursor: grab;
user-select: none;
}
li.dragging {
opacity: 0.5;
background: #e3f2fd;
}
</style>
</head>
<body>
<h2>Sortable List</h2>
<ul id="sortable">
<li draggable="true">Item 1</li>
<li draggable="true">Item 2</li>
<li draggable="true">Item 3</li>
<li draggable="true">Item 4</li>
</ul>
<script>
const list = document.getElementById("sortable");
let draggedItem = null;
list.querySelectorAll("li").forEach(item => {
item.addEventListener("dragstart", function(e) {
draggedItem = this;
this.classList.add("dragging");
e.dataTransfer.effectAllowed = "move";
});
item.addEventListener("dragend", function() {
this.classList.remove("dragging");
draggedItem = null;
});
item.addEventListener("dragover", function(e) {
e.preventDefault();
const afterElement = getDragAfterElement(list, e.clientY);
if (afterElement == null) {
list.appendChild(draggedItem);
} else {
list.insertBefore(draggedItem, afterElement);
}
});
});
function getDragAfterElement(container, y) {
const draggableElements = [...container.querySelectorAll("li:not(.dragging)")];
return draggableElements.reduce((closest, child) => {
const box = child.getBoundingClientRect();
const offset = y - box.top - box.height / 2;
if (offset < 0 && offset > closest.offset) {
return { offset: offset, element: child };
} else {
return closest;
}
}, { offset: Number.NEGATIVE_INFINITY }).element;
}
</script>
</body>
</html>
Important Events
- dragstart – Fires when the user starts dragging
- dragover – Fires continuously while dragging over a valid drop target (must call
preventDefault()) - drop – Fires when the user releases the mouse over a drop target
- dragend – Fires when the drag operation ends
Web Resources on HTML Drag and Drop
1. MDN : HTML Drag and Drop API
2. MDN : Drag Operations Guide
3. MDN : File Drag and Drop Tutorial
Questions and Answers related to HTML Drag and Drop
Add the attribute draggable="true" to the element. Then listen for the dragstart event to set the data being transferred.
By default the browser does not allow dropping. Calling e.preventDefault() in the dragover handler tells the browser that the element is a valid drop target.
Listen for dragover and drop on a container. In the drop handler, read the files from e.dataTransfer.files and process them (upload, preview, etc.).
Yes. Make each list item draggable, track the currently dragged item, and on dragover insert it before or after the element under the cursor.
Native HTML5 Drag and Drop has limited support on touch devices. For full mobile support many developers use libraries that also handle touch events, or implement their own pointer-based solution.
🧪 Test Your HTML Code
Edit the HTML code on the left and click “Run Code” to see the result on the right.
Click “Run Code” to see the result here.