DEV Community

Anurag Gharat
Anurag Gharat

Posted on • Updated on

HTML Drag and Drop API

Hey there! Today I will show you a simple implementation of HTML Drag and Drop API.

HTML Drag and Drop

HTML allows you to drag and drop every element in the browser. Using a mouse you can grab a draggable element and place it inside a droppable element!

Below is a simple implementation!

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
        height: 100vh;
        width: 100%;
        padding: 0%;
        margin: 0%;
        box-sizing: border-box;
        display: flex;
        flex-direction: row;
        justify-content: space-around;
        align-items: center;
        width: 30%;
        height: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        border: 1px solid #222;
        background-color: #efefef;
        background-color: aqua;
        text-align: center;
        height: 50%;
        width: 50%;
    <div id="draggableitem" ondragstart="drag(event)" draggable="true">
        <h1>Drag me</h1>

    <section id="dropzone" ondrop="drop(event)" ondragover="allowDrop(event)"



    //Fires when user starts dragging
    function drag(e){
        //adding target elements id to datatransfer object 

    //Fired when mouse moves over droppable element
    function allowDrop(e) {
        console.log("over drop zone")

    //fired when the element is dropped
    function drop(e) {
    //getting data and appending it to the target section
    var data = e.dataTransfer.getData("text");;
Enter fullscreen mode Exit fullscreen mode

Lets go through the code, one part at a time!

HTML code:
Here we are having two sections. One drag zone and one drop zone.
Div is the element which we wish to drag and drop. We then set the draggable attribute of the Div tag as "true".

"ondragstart" event runs when you grab the draggable item. Further more, in drop section, we have two events. “ondragover” starts firing when you move the dragged item over the drop zone and “ondrop” fires when you drop the item by releasing the mouse button.
We even have more events if you wish to apply more logic to your component. In our current implementation this 3 events are enough.

HTML code

CSS code:
Basic CSS code where I am centering the sections and div so we can easily understand the drag and drop layout.

CSS code

JavaScript code:
So using JS we add the draggable element using its ID to the dataTransfer object. Then on reaching the drop zone we append the draggable element to the drop zone section.
The explanation of each step in the functions has been added in the comments.

JS code

For further reference you can read this MDN documentation for Drag and Drop API.

Hope this article helped you in knowing Drag and Drop feature in HTML. I regularly post tweets regarding HTML, CSS, JS, React, Cloud and Blockchain on my twitter. Do follow me if you love such topics. Thanks!

Top comments (3)

meatboy profile image
Meat Boy • Edited

Off top but imo better put the code in proper tag. If someone wants to quickly test your code, must rewrite everything. Can you imagine stackoverflow with images instead of code snippets? ;)

anuraggharat profile image
Anurag Gharat

Oh yeah good point!
I removed the image and added a code block.

suhakim profile image
sadiul hakim

Nice content