242

Is there a way to prevent the user from seeing a ghost of the image they are trying to drag (not concern about security of the images, but the experience).

I've tried this which fixes the problem with the blue selection on text and images but not the ghost image:

img {
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -o-user-select: none;
  user-select: none;
}

(I also tried nesting the image inside a div with the same rules applied to the div). Thanks

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
user885355
  • 3,369
  • 4
  • 19
  • 10

23 Answers23

308

You can set the draggable attribute to false in either the markup or JavaScript code.

// As a jQuery method: $('#myImage').attr('draggable', false);
document.getElementById('myImage').setAttribute('draggable', false);
<img id="myImage" src="https://cdn.sstatic.net/Sites/stackoverflow/Img/apple-touch-icon.png">

Or direclty with HTML:

<img id="myImage" src="https://link-to-your.image.com/image.png" draggable="false">

Note that draggable="false" can also used on other HTML elements than img.

Avatar
  • 14,622
  • 9
  • 119
  • 198
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
  • 125
    Why add it with JavaScript??? `` – Greg Aug 08 '13 at 11:45
  • 45
    @Greg: Why are you asking me? It's the OP who wanted a JavaScript solution. Plus I did say "in either the markup or JavaScript code" anyway, so it's not like you *couldn't* do it in the markup if you had the option of editing your markup. – BoltClock Aug 08 '13 at 11:49
  • 21
    Doesn't work in FF in combination with `-moz-user-select: none`. Possible solution: Add `pointer-events: none`. – Cedric Reichenbach Apr 05 '14 at 14:10
  • 9
    Maybe I have misinterpreted the question but isn't the OP asking how they can retain drag & drop but just hide the ghosted image? Setting `draggable` to `false` will completely disable drag & drop. – James Jun 24 '14 at 10:21
  • 2
    @James: It's not very clear from the question, to be honest. I'm mostly answering under the assumption that the ghost image is visual indicator for drag-and-drop (which it is), and that most people who want to hide the ghost image generally don't care if drag-and-drop is disabled altogether. – BoltClock Jun 26 '14 at 01:28
  • @BoltClock the reason for my comment was because I wanted to do exactly that; retain drag & drop but avoid generating the image. I consequently had to eventually give up on the native drag & drop but I still feel this type of functionality would be useful. – James Jun 26 '14 at 10:13
  • 1
    Note that `pointer-events: none` will disable all pointer events, including any desired clicks. Which wouldn't be good for something like the `map` tag, or anything else you'd want to click but not drag. – Beejor Feb 02 '15 at 20:49
  • Note that the `draggable` attribute requires HTML5 and at least IE 9, Safari 6, or Opera 12. – Beejor Dec 03 '16 at 02:45
  • this doesn't work if the parent has a `dragover` or `drop` event associated with it. is there another way? – oldboy May 24 '17 at 05:23
  • @Anthony: Not that I know of - I wrote this answer with the assumption that the author does not intend for the image to be dragged at all. – BoltClock May 24 '17 at 05:41
  • @BoltClock yes, that's exactly what i'm talking about--not allowing something to be dragged at all. none of these ways work for me. [here is the question i just posted.](https://stackoverflow.com/questions/44149403/prevent-ghost-image-on-dragging-non-draggable-elements?noredirect=1#comment75316600_44149403) – oldboy May 24 '17 at 05:54
  • Hi everyone, I'm facing this issue in firefox 59.0.2 (64-bit) but not in any other browsers. The sample gets dragged in firefox. Any help? – kingshuk basak Apr 26 '18 at 08:22
  • 1
    You actually need to do `element.setAttribute("draggable","false");`. Tested in Firefox 70 and directly setting `element.draggable = "false"` will not always work. E.g., when DOM element has not yet been appended to page – Azmisov Oct 25 '19 at 16:23
  • @Azmisov: Thanks for pointing this out. This limitation does not appear to be mentioned in the spec (alternatively, it could be an issue with Firefox). In hindsight, I don't know why I didn't just go with that as a counterpart to jQuery's .attr()... – BoltClock Oct 27 '19 at 13:27
  • @James You could set the drag image to a minimum or transparent image. `ondragstart` set `event.dataTransfer.setDragImage(img, 0, 0);` The image could for example be https://stackoverflow.com/a/13139830/3342816 – user3342816 Nov 04 '21 at 19:34
143

I think you can change your

img {
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -o-user-select: none;
  user-select: none;
}

into a

img {
  -webkit-user-drag: none;
  -khtml-user-drag: none;
  -moz-user-drag: none;
  -o-user-drag: none;
  user-drag: none;
}
chpio
  • 896
  • 6
  • 16
Max
  • 1,463
  • 1
  • 9
  • 2
57

Try it:

img {
  pointer-events: none;
}

and try to avoid

* {
  pointer-events: none;
}
chpio
  • 896
  • 6
  • 16
Erik Rybalkin
  • 1,143
  • 12
  • 21
  • 39
    This is a poor solution, as it removes all ability to interact with the element. OP is only asking to disable the "drag" functionality of an element, not to entirely disable any and all pointer-based interaction with the element. – Chad Dec 28 '18 at 20:42
  • 9
    @Chad that's true but it's possible to wrap the image and add the event listener to wrapper while the image has no ghost image anymore. – Vincent Hoch-Drei Feb 21 '19 at 10:32
26

This will disable dragging for an image in all browsers, while preserving other events such as click and hover. Works as long as any of HTML5, JS, or CSS are available.

<img draggable="false" onmousedown="return false" style="user-drag: none" />

If you're confident the user will have JS, you only need to use the JS attribute, etc. For more flexibility, look into ondragstart, onselectstart, and some WebKit tap/touch CSS.

Beejor
  • 8,606
  • 1
  • 41
  • 31
  • This doesn't seem to work in FireFox because, strictly speaking, do not even have onclick or click events. Anyways, I just used the onmouseup portion and let the parent DIV handle the onclick and it works. – Nelson Nov 28 '16 at 09:00
  • @Nelson Thanks for letting me know. It seems the onmouseup wasn't needed anyway since onclick fires regardless of the return value from any onmousedown/up handler. I decided to revise my answer for simplicity. The new one-liner should work fine; just tested in FF, Safari, Chrome, IE8/10. – Beejor Dec 03 '16 at 02:39
  • I'm on Firefox 62 and `onmousedown="return false"` is sufficient. It is also needed to avoid the user selecting the image while dragging on it, which then would allow them to drag the selection. – loxaxs Oct 06 '18 at 19:38
24

You can use a CSS property to disable images in webkit browsers.

img{-webkit-user-drag: none;}
mins
  • 6,478
  • 12
  • 56
  • 75
Ashwani
  • 293
  • 2
  • 5
  • 6
    This also works with `-ms-user-drag`, `-moz-user-drag`, and `user-drag`. A great CSS-only solution! – Beejor Dec 03 '16 at 02:41
20

Very simple don't make it complicated with lots of logic use simple attribute draggable and make it false

<img draggable="false" src="img/magician.jpg" alt="" />
MD SHAYON
  • 7,001
  • 45
  • 38
  • This appears to exist only in the very latest browsers, e.g. https://caniuse.com/?search=draggable says the feature arrived in Chrome 113 and https://chromereleases.googleblog.com/2023/05/stable-channel-update-for-desktop.html says Chrome 113 released 2 May 2023. You wrote your comment in late 2020, did it exist then in stable releases? – Neek May 19 '23 at 06:05
12

The be-all-end-all, for no selecting or dragging, with all browser prefixes:

-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-o-user-select: none;
-ms-user-select: none;
user-select: none;

-webkit-user-drag: none;
-khtml-user-drag: none;
-moz-user-drag: none;
-o-user-drag: none;
-ms-user-drag: none;
user-drag: none;

You can also set the draggable attribute to false. You can do this with inline HTML: draggable="false", with Javascript: elm.draggable = false, or with jQuery: elm.attr('draggable', false).

You can also handle the onmousedown function to return false. You can do this with inline HTML: onmousedown="return false", with Javascript: elm.onmousedown=()=>return false;, or with jQuery: elm.mousedown(()=>return false)

Justin
  • 945
  • 12
  • 26
11
<img src="myimage.jpg" ondragstart="return false;" />
lubosdz
  • 4,210
  • 2
  • 29
  • 43
10

Handle the dragstart event and return false.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
4

You can assign an alternate ghost image if you really need to use drag events and can't set draggable=false. So just assign a blank png like so:

    $('#img').bind({
        dragstart: function(e) {
            var dragIcon = document.createElement('img');
            dragIcon.src = 'blank.png';
            dragIcon.width = 100;
            e.dataTransfer.setDragImage(dragIcon, -10, -10);
        }
    });
louisinhongkong
  • 551
  • 1
  • 6
  • 13
4

HTML

<img draggable="false" src="img/example.jpg">

JavaScript

document.querySelector('img').addEventListener('dragstart', (event) => {
  event.preventDefault();
});

CSS

img {
  -webkit-user-drag: none;
}

There is currently no unprefixed user-drag or other version of the property implemented in browsers or on a standards track as the HTML draggable attribute/property is the preferred solution.

It's a Webkit-specific property. Here's what the WebKit documentation says about it:

Heading

WebKit provides automatic support to let users drag common items, such as images, links, and selected text. You can extend this support to include specific elements on an HTML page. For example, you could mark a particular div or span tag as draggable.

To mark an arbitrary element as draggable, add the -webkit-user-drag attribute to the style definition of the element. Because -webkit-user-drag is a cascading style sheet (CSS) attribute, you can include it as part of a style definition, or as an inline style attribute on the element tag. The values for this attribute are listed in Table 4-1.

Values for -webkit-user-drag attribute:

  • none: Do not allow this element to be dragged.

  • element: Allow this element to be dragged.

  • auto: Use the default logic for determining whether the element should be dragged. (Images, links, and text selections are the only elements that can be dragged.) This is the default value.

It's supported by all browsers using the WebKit rendering engine, so Chrome, newer versions of Opera, Safari, etc. Support in mobile browsers using WebKit may vary depending on the mobile OS.

Emin Temiz
  • 51
  • 1
  • 2
3

For Firefox you need to go a little deeper with this:

var imgs = document.getElementsByTagName('img');

    // loop through fetched images
    for (i = 0; i < imgs.length; i++) {
        // and define onmousedown event handler
        imgs[i].onmousedown = disableDragging;
    }

function disableDragging(e) {
        e.preventDefault();
    }

Enjoy.

dijipiji
  • 3,063
  • 1
  • 26
  • 21
3

Place the image as a background of an empty div, or under a transparent element. When the user clicks on the image to drag, they are clicking on a div.

See http://www.flickr.com/photos/thefella/5878724253/?f=hp

<div id="photo-drag-proxy"></div>
Michael Jasper
  • 7,962
  • 4
  • 40
  • 60
2

You can wrap the img inside another div and apply pointer-events:none; to that div. So that the img will not be able to drag. But this should be utilized only if you are not intending to add any other behaviour to the image you are targeting.

.wrapper {
  pointer-events: none;
}
<div>
  <div class="wrapper">
    <img class="image" src="https://images.unsplash.com/photo-1575936123452-b67c3203c357?ixlib=rb-4.0.3&ixid=MnwxMjA3fDB8MHxzZWFyY2h8Mnx8aW1hZ2V8ZW58MHx8MHx8&auto=format&fit=crop&w=500&q=60"/>
  </div>
</div>
XPD
  • 1,121
  • 1
  • 13
  • 26
1

I found that for IE, you must add the draggable="false" attribute to images and anchors to prevent dragging. the CSS options work for all other browsers. I did this in jQuery:

$("a").attr('draggable', false); 
$("img").attr('draggable', false);
Derek Wade
  • 697
  • 8
  • 11
1

There is a much easier solution here than adding empty event listeners. Just set pointer-events: noneto your image. If you still need it to be clickable, add a container around it which triggers the event.

Micros
  • 5,966
  • 2
  • 28
  • 34
1

When Firefox does not appreciate your draggable attribute (when set to false) or none of your user-drag CSS rules in your link/anchor or image element:

Firefox user-drag CSS rules rejected

And you want to keep pointer-events as they are, you may use the big guns for that ghostly "translucent image generated from the drag target (the element the dragstart event is fired at)" as described in MDN setDragImage. Simply use:

if (/(firefox)/i.test(navigator.userAgent)) {
  document.querySelector('.my-non-draggable').addEventListener('dragstart',
    e => e.preventDefault()
  );
  // or jQuery: $('.my-non-draggable').on('dragstart', e => e.preventDefault());
}
CPHPython
  • 12,379
  • 5
  • 59
  • 71
1
document.querySelectorAll("*").forEach((elem) => {
    elem.setAttribute('draggable', false)
    elem.addEventListener('dragstart', (event) => {
        event.preventDefault()
    })
})

This disables drag on all elements.

PowerKuu
  • 63
  • 7
0

Tested on Firefox: removing and putting back the image works! And it's transparent at the execution, too. For instance,

$('.imageContainerClass').mousedown(function() {
    var id = $(this).attr('id');
    $('#'+id).remove();
    $('#'+id).append('Image tag code');
});

EDIT: This works only on IE and on Firefox, strangely. I also added draggable = false on each image. Still a ghost with Chrome and Safari.

EDIT 2: The background-image solution is genuinely the best one. The only subtlety is that the background-size property has to be redefined every time the background-image is changed! Or so, that's what it looked like from my side. Better still, I had an issue with normal img tags under IE, where IE failed to resize the images. Now, the images have the correct dimensions. Simple:

$(id).css( 'background-image', url('blah.png') );
$(id).css( 'background-size', '40px');

Also, perhaps consider those:

background-Repeat:no-repeat;
background-Position: center center;
Tama Yoshi
  • 303
  • 2
  • 15
0

You can set the image that is shown when an item is dragged. Tested with Chrome.

setDragImage

use

onclick = myFunction();
myFunction(e) {
    e.dataTransfer.setDragImage(someImage, xOffset, yOffset);
}

Alternatively, as already mentioned in the answers, you can set draggable="false" on the HTML element, if not being able to drag the element at all is no issue.

Colin Cline
  • 1,291
  • 1
  • 12
  • 25
0

You can use "Empty Img Element".
Empty Img Element - document.createElement("img")

[HTML Code]
<div id="hello" draggable="true">Drag!!!</div>
[JavaScript Code]
var block = document.querySelector('#hello');
block.addEventListener('dragstart', function(e){
    var img = document.createElement("img");
    e.dataTransfer.setDragImage(img, 0, 0);
})
0

//disable image dragging

$(document).on("dragstart", function (e) {
  e.preventDefault();
});

This requires jQuery. This prevent dragging of anything text or img. Replace document with your any element you want to prevented from dragging.

Pankaj
  • 61
  • 4
-3

This work for me, i use some lightbox scripts

.nodragglement {
    transform: translate(0px, 0px)!important;
}