How come the font size and div elements combine when I drag and drop the items?

Recently, I decided to create my own drag and drop game.

The game is almost complete, but there's one issue. When I try to drop the items into the designated "Drop Items Here" area, their style changes abruptly to mimic the text. For example:

https://i.sstatic.net/lanFm.png

Additionally, if I drop multiple items at once, they end up merging into each other. This can be seen in the following image:

https://i.sstatic.net/Yq4PU.png

Being a beginner in this field, my code might seem unconventional. Is there any way to prevent the items from merging together and maintaining their original styles after being dropped? Thank you for your help.

function allowDrop(event) {
  event.preventDefault();
}
function drag(event) {
  event.dataTransfer.setData("text", event.target.id);
}
function drop(event) {
  event.preventDefault();
  var data = event.dataTransfer.getData("text");
  if (data == "1"|"2"|3|4){
    event.target.appendChild(document.getElementById(data));
  }
}
.Drag{
    display: flex;
    flex-flow: column;
}
.lists {
    display: flex;
    flex: 1;
    width: 100%;

}
.lists .list{
    display: flex;
    flex-flow: column;
    flex: 1;        
    margin: 0 15px;
}
.lists .list .item {
    background-color: #0099ff;
    border-radius: 8px;
    border: 3px solid #0085de;
    padding: 5px 5px;
    text-align: left;
    margin:4px 0px;
    color: white;
    font-weight: 900;
}
.DropHere{
  text-align: center;
  color:#d9d9d9;
  font-size: 28px;
}
<div class = "Drag">
    <div class = "lists">       
        <div class = "list">
            <div>Drag and drop</div>            
                <div ondrop="drop(event)" ondragover="allowDrop(event)" 
                     style="border-style: dashed; border-color:grey; border-radius: 18px; height: 600px;">
                    <p class = "DropHere">Drop items here</p>
                </div>              
            </div>
        <div class = "list">
            <div class = "item" draggable="true" ondragstart="drag(event)" id = "1">This is a</div>
            <div class = "item" draggable="true" ondragstart="drag(event)" id = "2">This is b</div>
            <div class = "item" draggable="true" ondragstart="drag(event)">This is c</div>  
            <div class = "item" draggable="true" ondragstart="drag(event)">This is d</div>  
            <div class = "item" draggable="true" ondragstart="drag(event)" id = "3">This is f</div> 
            <div class = "item" draggable="true" ondragstart="drag(event)" id = "4">This is g</div>
        </div>
    </div>
</div>

Answer №1

There's a P page inside the Drop Items Here container. Extract this P tag and style it using CSS. Review the following code snippet for details.

function allowDrop(event) {
  event.preventDefault();
}
function drag(event) {
  event.dataTransfer.setData("text", event.target.id);
}
function drop(event) {
  event.preventDefault();
  var data = event.dataTransfer.getData("text");
  if (data == "1"|"2"|3|4){
  event.target.closest(".drag-box-output").appendChild(document.getElementById(data));}
}
.Drag{
    display: flex;
    flex-flow: column;
}

.lists{
    display: flex;
    flex: 1;
    width: 100%;

}

.lists .list{
    display: flex;
    flex-flow: column;
    flex: 1;
    
    margin: 0 15px;
}

.lists .list .item {
    background-color: #0099ff;
    border-radius: 8px;
    border: 3px solid #0085de;
    padding: 5px 5px;
    text-align: left;
    margin:4px 0px;
    color: white;
    font-weight: 900;
}

.DropHere{
  text-align: center;
  color:#d9d9d9;
  font-size: 28px;
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    z-index: -1;
}
/* custom CSS*/
.drag-box{
    position: relative;
    z-index: 1;
}
.drag-box-output{
padding-top: 80px;
}
<div class = "Drag">
        <div class = "lists">       
            <div class = "list">
                <div>Drag and drop</div>   
                <div class="drag-box">
                <div class="drag-box-output" ondrop="drop(event)" ondragover="allowDrop(event)" style = "border-style: dashed; border-color:grey; border-radius: 18px; height: 600px;"></div><p class = "DropHere">Drop items here</p></div>
            </div>
        <div class = "list">
            <div class = "item" draggable = "true" ondragstart="drag(event)" id = "1">This is a</div>
            <div class = "item" draggable = "true" ondragstart="drag(event)" id = "2">This is b</div>
            <div class = "item" draggable = "true" ondragstart="drag(event)">This is c</div>    
            <div class = "item" draggable = "true" ondragstart="drag(event)">This is d</div>    
            <div class = "item" draggable = "true" ondragstart="drag(event)" id = "3">This is f</div>   
            <div class = "item" draggable = "true" ondragstart="drag(event)" id = "4">This is g</div>   

        </div>
        </div>
     </div>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Encountering a 404 error when trying to navigate to the next route using the Link component

Having issues with my login route. I've added a Link to the login route inside a button tag in my Navbar component, but when I try to access the route, I'm getting a 404 error page. --app ----pages ------component --------Navbar.js ----- ...

Include the name of the uploaded attachment in the textarea before hitting the submit button

Is there a way to automatically insert the filename into a textarea before the user submits the form? Can this be achieved using PHP or JavaScript? Thank you. <form id="Apply" name="Apply" method="post" enctype="multipart/form-data" action="applyLea ...

Adjust the height to match the shortest sibling child div height

In my layout, I have multiple rows with each row containing multiple columns. Within each column, there is a div and a paragraph - the div contains an image. My goal is to set the div with the class cover-bg to the lowest height of cover-bg in the same row ...

Utilizing an Immediate-Invoked Function Expression (IIFE) for jQuery in JavaScript

I'm looking at this code snippet that I believe is an Immediately Invoked Function Expression (IIFE). But, I'm confused about the role of (jQuery) and ($). I understand that it involves passing a reference to jQuery into the IIFE, but can someone ...

Utilizing Typescript to implement an interface's properties

After declaring an interface as shown below interface Base { required: string; } I proceeded to implement the interface in a class like this class MyClass implements Base{ method(): void { console.log(this.required); } } However, I ...

How to Retrieve Grandparent Component Attributes in Angular Using Grandchild Components

I am constructing an Angular application and facing the challenge of accessing a property of Component 1 within Component 3. In this scenario, the relationship is described as grandparent-grandchild. Successfully establishing communication between parent/ ...

Improving Material UI Responsiveness: A Comprehensive Guide

Could someone please help me resolve the overflow issue in my UI? You can view the overflow here. The second image explains the reason behind it, which you can see here. I am currently working on fixing my UI using React and Material UI. When checking the ...

What is the reason for Range.getBoundingClientRect() returning 0 for a range that is inside a textarea

When working with a <textarea> element and creating a range inside it, the following steps are taken: A new range is created using document.createRange() The only child node of the <textarea> is retrieved using textarea.childNodes[0] Range st ...

Utilizing headless Chrome to automatically capture AJAX requests

Chrome officially supports running the browser in headless mode, allowing for programmatic control through the Puppeteer API and/or the CRI library. I've thoroughly explored the documentation but have not discovered a method to programmatically captu ...

Encounter a 401 error code while attempting to fetch data from CouchDB using an AJAX request

I attempted to make an AJAX call in a JavaScript file to fetch data from CouchDB. Unfortunately, I encountered a 401 error message: Failed to load resource: the server responded with a status of 401 (Unauthorized) This is an extract of my JavaScript cod ...

AngularJS object array function parameter is not defined

Recently, I've been honing my AngularJS1x skills by following tutorials on Lynda and Udemy. One tutorial involved creating a multiple choice quiz. To test my understanding, I decided to modify the code and transform it into a fill-in-the-blank quiz. ...

Is there a way to invoke a high-order function multiple times within the code?

const x = { a:1 }; (function q({a}){ console.log(a); return a; })(x); // unable to execute console.log(q(x)) I'm encountering an issue trying to run line 6. Why is this happening? The console works in line 3 when I remove line 6: Error: q is n ...

The controller function is not triggered if the user does not have administrator privileges

I have a code snippet in which my controller function is not being triggered for users who do not have the role of "Admin". Can someone help me identify where I went wrong? Just to clarify, the controller function works fine for users with the role of "Adm ...

Understanding the process of globally evaluating classes from files in node.js using the Eval function

Parent script with childscript integration const fs = require('fs'); function includeFile(f) { eval.apply(global,[fs.readFileSync(f).toString()]) } includeFile(__dirname+'/fileToRead.js'); hi(); ...

Codeigniter dropdown sending incorrect value to controller

When I choose an option from the dropdown list and click the submit button in the modal, the selected value is not being posted to the controller as expected. Below is my view code: <div class="form-group"> <div class="col-md-4"> ...

Similar to session_start() and $_SESSION in Node.js/Express

I'm on a quest to discover the equivalent of session_start() and $_SESSION in Node.js/Express so I can store the current user's id in the session. Most tutorials and videos recommend using express-session, but I've come across a warning: ...

Iterating Through Multiple Dimensions

I am facing a challenge with creating rules from three arrays that I have. The task is to generate every possible combination of entries from each array, but I am struggling with the logic required to write a function for this purpose. For example: var ar ...

React: An error has occurred - Properties cannot be read from an undefined value

THIS PROBLEM HAS BEEN RESOLVED. To see the solutions, scroll down or click here I've been working on a React project where I need to fetch JSON data from my server and render it using two functions. However, I'm encountering an issue where the v ...

Modify the transparency of a form's backdrop without affecting the input fields

I'm attempting to achieve a similar effect as shown in this example on my website form. I've tried using opacity to make the form field background only 20% visible, but it ends up affecting the text inside and when typing. Is there a way to apply ...

Failure to trigger the callback for mongoose.connection.once('open') event

Currently, I am in the process of setting up a custom Node server using Next.js. Although I'm utilizing Next.js this time around, it should not affect the outcome. In my previous applications, I always relied on mongoose.connection.once('open&ap ...