187

Does anyone know of a way to make an image not draggable and not selectable -- at the same time -- in Firefox, without resorting to Javascript? Seems trivial, but here's the issue:

  1. Can be dragged and highlighted in Firefox:

  2. So we add this, but image can still be highlighted while dragging:

  3. So we add this, to fix the highlighting issue, but then counterintuitively, the image become draggable again. Weird, I know! Using FF 16.0.1

So, does anyone know why adding -moz-user-select: none, would somehow trump and disable draggable=false? Of course, webkit works as expected. Nothing is out there on the Interwebs about this...It would be great if we could shine some light on this together.

Edit: This is about keeping UI elements from being inadvertently dragged and improving usability - not some lame attempt at a copy protection scheme.

tmkly3
  • 3,131
  • 2
  • 18
  • 13
  • 2
    @JimGarrison of course not, sorry I should have been more specific, I'm trying to keep some of my UI elements from getting moved about, in the right context it's harming usability. – tmkly3 Oct 16 '12 at 04:08
  • 2
    Very useful instead if you have background elements that will be dragged on desktop (or worse as href code lines into some file open in background behind the browser window while working on it!) when they are close to the very small modern scroll bars and you do not hit the bar exactly each time testing the scrolls. – Garavani Oct 11 '16 at 15:16
  • 2
    This has been a known issue in Firefox for [a really long time](https://bugzilla.mozilla.org/show_bug.cgi?id=1376369) as it turns out ([duplicate?](https://bugzilla.mozilla.org/show_bug.cgi?id=1326143)) – Coderer Mar 21 '19 at 14:12

10 Answers10

296

Set the following CSS properties to the image:

.selector {
    user-drag: none;
    -webkit-user-drag: none;
    user-select: none;
    -moz-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
}
IAfanasov
  • 4,775
  • 3
  • 27
  • 42
Jeff Wooden
  • 5,339
  • 2
  • 19
  • 24
  • 4
    Thanks, I have those in-place as well, but the issue is starting to look like a bug in Firefox. I guess the only way to solve this for now is through Javascript. – tmkly3 Oct 16 '12 at 04:15
  • Worked on all browsers. – Milad Abooali Aug 21 '15 at 03:16
  • Alright so how do I make it show no effect but still fire the drag event? – Ty_ Sep 14 '15 at 17:49
  • @EW-CodeMonkey - Is this what you're looking for? http://stackoverflow.com/questions/17055044/controlling-the-appearance-of-the-html5-drag-and-drop-effect – Jeff Wooden Sep 15 '15 at 18:18
  • 13
    I found I could still drag on Firefox 47 but adding Masadow's ondragstart="return false;" property to the image tag fixed it. – andrew pate Jul 06 '16 at 13:24
  • @andrewpate, if you are going to use something other than CSS, then use the [`draggable` attribute](https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes#attr-draggable)/[property](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement). It is, after all, what the [HTML Spec](https://html.spec.whatwg.org/multipage/interaction.html#the-draggable-attribute) defines as what controls if an element is draggable. (e.g. HTML: `draggable="false"` JS: `element.setAttribute('draggable',false);`, or `element.draggable = false;`) – Makyen Aug 12 '16 at 03:29
  • 5
    I can still drag in chrome – lonewarrior556 Jan 26 '17 at 22:02
  • 17
    `user-drag` only works in Safari. if you add `draggable="false"` to the image tag, it will stop being draggable. – WrksOnMyMachine Mar 22 '18 at 19:08
  • I left a comment on the question, but in it probably bears repeating here: [known issue](https://bugzilla.mozilla.org/show_bug.cgi?id=1326143), no apparent movement. – Coderer Mar 21 '19 at 14:12
  • 5
    When I set `user-select: none` the `user-drag: none` properties do not work... Chrome 80 – Elijah Mock Mar 20 '20 at 14:14
  • You should add a sample where this actually works. I tried in FF and it doesn't. Maybe it does work in some browsers? – sprajagopal Feb 17 '21 at 11:55
  • By 2023, according to [caniuse.com](https://caniuse.com/webkit-user-drag), this method has only 37% browsers coverage. – maxime schoeni Jul 27 '23 at 09:32
72

You can use the pointer-events property in your CSS, and set it equal to 'none'

img {
    pointer-events: none;
}

Edited

this will block (click) event. So better solution would be

<img draggable="false" (dragstart)="false;" class="unselectable">

.unselectable {
  user-drag: none; 
  user-select: none;
  -moz-user-select: none;
  -webkit-user-drag: none;
  -webkit-user-select: none;
  -ms-user-select: none;
}
Pratap A.K
  • 4,337
  • 11
  • 42
  • 79
Linh Dam
  • 2,033
  • 1
  • 19
  • 18
60

I've been forgetting to share my solution, I couldn't find a way to do this without using JS. There are some corner cases where @Jeffery A Wooden's suggested CSS just wont cover.

This is what I apply to all of my UI containers, no need to apply to each element since it recuses on all the child elements.

CSS:

.unselectable {
    /* For Opera and <= IE9, we need to add unselectable="on" attribute onto each element */
    /* Check this site for more details: http://help.dottoro.com/lhwdpnva.php */
    -moz-user-select: none; /* These user-select properties are inheritable, used to prevent text selection */
    -webkit-user-select: none;
    -ms-user-select: none; /* From IE10 only */
    user-select: none; /* Not valid CSS yet, as of July 2012 */

    -webkit-user-drag: none; /* Prevents dragging of images/divs etc */
    user-drag: none;
}

JS:

var makeUnselectable = function( $target ) {
    $target
        .addClass( 'unselectable' ) // All these attributes are inheritable
        .attr( 'unselectable', 'on' ) // For IE9 - This property is not inherited, needs to be placed onto everything
        .attr( 'draggable', 'false' ) // For moz and webkit, although Firefox 16 ignores this when -moz-user-select: none; is set, it's like these properties are mutually exclusive, seems to be a bug.
        .on( 'dragstart', function() { return false; } );  // Needed since Firefox 16 seems to ingore the 'draggable' attribute we just applied above when '-moz-user-select: none' is applied to the CSS 

    $target // Apply non-inheritable properties to the child elements
        .find( '*' )
        .attr( 'draggable', 'false' )
        .attr( 'unselectable', 'on' ); 
};

This was way more complicated than it needed to be.

Ali Sheikhpour
  • 10,475
  • 5
  • 41
  • 82
tmkly3
  • 3,131
  • 2
  • 18
  • 13
14

Depending on the situation, it is often helpful to make the image a background image of a div with CSS.

<div id='my-image'></div>

Then in CSS:

#my-image {
    background-image: url('/img/foo.png');
    width: ???px;
    height: ???px;
}

See this JSFiddle for a live example with a button and a different sizing option.

Nick Merrill
  • 1,242
  • 1
  • 14
  • 20
14

You could probably just resort to

<img src="..." style="pointer-events: none;">
Thomas Bachem
  • 1,545
  • 1
  • 16
  • 10
  • This should be the accepted answer, user drag is not supported by firefox. Thank you for this! – Merritt6616 Feb 16 '18 at 09:38
  • For me too, "pointer-events" was the solution because it worked cross-browser ("user-drag" has no effect on Firefox, at least in 11.2018) – David Dal Busco Nov 07 '18 at 07:57
  • 2
    Beware that setting `pointer-events` to `none` also turns off ALL POINTER EVENTS, such as onclick, onmouseenter, etc. for JavaScript. See this answer: https://stackoverflow.com/a/27086394/12101554 – Samathingamajig Jan 16 '21 at 07:49
10

A generic solution especially for Windows Edge browser (as the -ms-user-select: none; CSS rule doesn't work):

window.ondragstart = function() {return false}

Note: This can save you having to add draggable="false" to every img tag when you still need the click event (i.e. you can't use pointer-events: none), but don't want the drag icon image to appear.

Guillermo Gutiérrez
  • 17,273
  • 17
  • 89
  • 116
Philip Murphy
  • 870
  • 3
  • 10
  • 24
3

Here's a four-part solution that should work in nearly every modern browser:

<img src="foobar.jpg" draggable="false" ondragstart="return false;">
img
{
    user-drag: none;
    -webkit-user-drag: none;
    
    user-select: none;
    -moz-user-select: none;
    -webkit-user-select: none;
    -ms-user-select: none;
}

You can remove the bottom four lines of CSS if you want to allow selecting/highlighting.

Pikamander2
  • 7,332
  • 3
  • 48
  • 69
  • This answer is a consolidated version of several other answers, all of which were helpful but none of which had been combined into a single convenient answer yet. – Pikamander2 Feb 16 '22 at 18:33
  • This will not currently work in Firefox, as `-moz-user-select: none` prevents `draggable="false"` from working. See https://bugzilla.mozilla.org/show_bug.cgi?id=1376369 – Marcio Duarte May 05 '22 at 18:18
2

I created a div element which has the same size as the image and is positioned on top of the image. Then, the mouse events do not go to the image element.

User
  • 14,131
  • 2
  • 40
  • 59
1

You could set the image as a background image. Since it resides in a div, and the div is undraggable, the image will be undraggable:

<div style="background-image: url("image.jpg");">
</div>
Lea Cohen
  • 7,990
  • 18
  • 73
  • 99
joshua pogi 28
  • 521
  • 1
  • 6
  • 15
0

You should simply add the draggable attribute to your img tag.

<img src="myimage.png" draggable="false">
Halo
  • 1,730
  • 1
  • 8
  • 31