29

I'm trying to disable highlight on image, when i move with my mouse on image and drag, Take a look : enter image description here

Thanks alot!

marto
  • 4,402
  • 25
  • 15
  • 2
    This is the top result on google for some searches. I think [this](http://stackoverflow.com/a/5225928/645270) is what many of you are looking for! – keyser Jan 23 '13 at 00:05
  • 1
    Possible duplicate of [CSS rule to disable text selection highlighting](http://stackoverflow.com/questions/826782/css-rule-to-disable-text-selection-highlighting) – showdev Feb 16 '16 at 17:27

9 Answers9

57

Use user-select property:

img{
    -khtml-user-select: none;
    -o-user-select: none;
    -moz-user-select: none;
    -webkit-user-select: none;
    user-select: none;
}
Victor
  • 5,493
  • 1
  • 27
  • 28
  • 1
    I think this is the cleanest method as of today. Also, for anyone looking for a way to disable odd effects in images like I was, take a look at the `draggable` attribute. Setting it to `false` in the img tag will avoid the ghost image should the user accidentally (or not) drag it. – Herick Nov 13 '14 at 11:45
14

You can try this (this won't work in all browsers):

img::-moz-selection {
    background-color: transparent;
    color: #000;
}

img::selection {
    background-color: transparent;
    color: #000;
}

Or you can use a <div> with the appropriate width and height set and use a CSS background image on it. For example I use this on my site:

<div id="header"></div>

#header {
    height: 79px;
    width: 401px;
    background: url(http://nclabs.org/images/header.png) no-repeat;
}

And finally you can use Javascript to programatically disable it.

orlp
  • 112,504
  • 36
  • 218
  • 315
10
img {
    -khtml-user-select: none;
    -o-user-select: none;
    -moz-user-select: none;
    -webkit-user-select: none;
    user-select: none;
}
Phil
  • 10,948
  • 17
  • 69
  • 101
  • @Phil You could edit your answer, so it's no longer incorrect. There are heavy discussions about deleting accepted answers and why it shouldn't be deleted. https://meta.stackoverflow.com/questions/272849/please-allow-me-to-delete-my-own-accepted-answer – tom_mai78101 Jun 29 '18 at 12:44
6

This disabled highlighting on a DOM element:

function disableSelection(target){
    if (typeof target.onselectstart!="undefined") // if IE
        target.onselectstart=function(){return false}
    else if (typeof target.style.MozUserSelect!="undefined") // if Firefox
        target.style.MozUserSelect="none";
    else // others
        target.onmousedown=function(){return false;}

    target.style.cursor = "default";
}

Use it like this:

disableSelection(document.getElementById("my_image"));
tskuzzy
  • 35,812
  • 14
  • 73
  • 140
4

Try to put it as a css background instead of an img element.

nakhli
  • 4,009
  • 5
  • 38
  • 61
3
img{
   -ms-user-select: none;      /* IE 10+ */
   -moz-user-select: none;     /* Firefox all */
   -webkit-user-select: none;  /* Chrome all / Safari all */
   user-select: none;          /* Likely future */      
}
1

To remove the selection of text and images from entire website use body selector

body {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
Aamer Shahzad
  • 2,617
  • 1
  • 27
  • 25
1

If you are facing a problem when you click on the image, here is the solution to that.

img {
  -webkit-tap-highlight-color: transparent;
}

if not working then please try that in your parent of the image that contains the whole width of the image.

Nisharg Shah
  • 16,638
  • 10
  • 62
  • 73
0

In case some people here are interested about the sass mixin:

// Prevent users to select an element
@mixin no-select {
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -khtml-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}
Julien Le Coupanec
  • 7,742
  • 9
  • 53
  • 67