Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why HTML draggable element drags with parent background?

Tags:

html

css

I have a draggable DIV without background inside DIV container with background:

<div style="width: 200px; height: 200px; background: red">
  <div style="width: 100px; height: 100px; border: 1px solid black" draggable="true">
    div without background
  </div>
</div>

When I drag the inner DIV, for some reason it moves with the parent background. Is it possible to drag the inner DIV with transparent background?

like image 308
Ildar Avatar asked Jun 08 '26 00:06

Ildar


2 Answers

It seems for some reason draggable="true" forces the div to inherit the parent background attribute. One fix is to use position on the <div> with z-index. Not sure if there is another way to fix, but here is a working snippet:

[draggable="true"] {
  width: 100px;
  height: 100px;
  border: 1px solid black;
  position: relative;
  z-index: 1;
}
<div style="width: 200px; height: 200px; background: red;">
  <div style="width: 100px; height: 100px; border: 1px solid black;" draggable="true">
    div without background
  </div>
</div>
like image 92
ROOT Avatar answered Jun 10 '26 18:06

ROOT


you can add css style to your draggable element it will remove parent background:

transform: translate(0, 0);

like image 28
saumya nigam Avatar answered Jun 10 '26 17:06

saumya nigam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!