

- 1HTML Forms
- 2HTML form Tag
- 3HTML Input Types
- 4HTML Labels and Placeholders
- 5HTML Select Dropdown
- 6HTML Checkbox
- 7HTML Radio Buttons
- 8HTML Textarea
- 9HTML Submit and Reset Buttons
- 10HTML Form Validation
- 11HTML Required Fields
- 12HTML Input Pattern Attribute
- 13HTML min and max Attribute
- 14HTML Form Action and Method
- 15HTML Fieldset and Legend
- 16HTML Form Advanced Controls
- 17HTML Date Picker
- 18HTML Range Slider
- 19HTML Color Picker
- 20HTML File Upload
- 21HTML Datalist
- 22HTML Autofocus and Autocomplete




- 1Accessibility in HTML
- 2Alt Text for Images
- 3ARIA Roles in HTML
- 4Semantic HTML for Accessibility
- 5Keyboard Navigation in HTML
- 6Screen Reader Accessibility in HTML
- 7HTML Best Practices for SEO
- 8HTML Meta Tags
- 9HTML Headings Best Practices
- 10Title and Meta Description Tags in HTML
- 11HTML Linking Structure
- 12HTML Clean Code Standards

Drag and Drop in HTML
Enable Draggable Elements and Drop Targets
Drag and Drop in HTML: Make Elements Move with Purpose
Want to let users drag a banana image into a basket? Reorder a to-do list? Rearrange photos? The HTML Drag and Drop API gives you full control over interactive movement between elements using simple JavaScript events.
How Drag and Drop Works
At its core, drag and drop involves three pieces:
- A draggable source element
- A target (drop zone)
- JavaScript to handle the transfer
Step 1: Enable Dragging
Use the draggable="true"
attribute on any HTML element to make it draggable.
<img src="banana.png" id="banana" width="100" draggable="true">
Step 2: Set Up the Drop Zone
You need a container that allows drops. You do this by listening for the dragover
and drop
events in JavaScript.
<div id="basket" style="width:200px;height:120px;border:2px dashed #aaa;text-align:center;padding-top:40px;">
Drop banana here
</div>
Step 3: Write JavaScript to Handle Drag and Drop Events
const banana = document.getElementById('banana');
const basket = document.getElementById('basket');
// Start drag
banana.addEventListener('dragstart', (e) => {
e.dataTransfer.setData('text/plain', 'banana');
});
// Allow drop
basket.addEventListener('dragover', (e) => {
e.preventDefault(); // Required to allow dropping
});
// Handle drop
basket.addEventListener('drop', (e) => {
e.preventDefault();
basket.innerHTML = '🍌 Banana dropped!';
});
[ Drag the banana → Drop it → Message updates in drop zone ]
Customizing Drag Feedback
Use dataTransfer.setDragImage()
to change the ghost image shown during drag:
banana.addEventListener('dragstart', (e) => {
e.dataTransfer.setData('text/plain', 'banana');
const dragIcon = document.createElement('img');
dragIcon.src = 'banana.png';
dragIcon.width = 80;
e.dataTransfer.setDragImage(dragIcon, 10, 10);
});
Multiple Drop Zones
<div id="basket1">Drop into Basket 1</div>
<div id="basket2">Drop into Basket 2</div>
<script>
function enableDrop(id) {
const basket = document.getElementById(id);
basket.addEventListener('dragover', e => e.preventDefault());
basket.addEventListener('drop', e => {
e.preventDefault();
basket.textContent = '🍒 Dropped!';
});
}
enableDrop('basket1');
enableDrop('basket2');
</script>
Best Practices
- Always call
preventDefault()
ondragover
to allow dropping - Use
setData()
andgetData()
to transfer useful info - Give visual cues for active drop targets using CSS
Styling Drag Feedback
You can style your drop zone dynamically using dragenter
and dragleave
events:
basket.addEventListener('dragenter', () => {
basket.style.background = '#eef';
});
basket.addEventListener('dragleave', () => {
basket.style.background = '';
});
Summary
Drag and Drop in HTML allows for intuitive, click-free movement of items across the UI. You now know how to:
- Enable elements to be dragged
- Design drop zones and capture drop events
- Customize drag visuals with
setDragImage()
- Style feedback to enhance UX
What’s Next?
Coming up: Take interactivity to the next level with the Geolocation API — learn how to find a user’s position and use it to personalize their experience.