158

Here is an interesting CSS questions for you!

I have a textarea with a transparent background overlaying some TEXT that I'd like to use as a sort of watermark. The text is large and takes up a majority of the textarea. It looks nice, the problem is when the user clicks in the textarea it sometimes selects the watermark text instead. I want the watermark text to never be selectable. I was expecting if something was lower in the z-index it would not be selectable but browsers don't seem to care about z-index layers when selecting items. Is there a trick or way to make it so this DIV is never selectable?

Whozumommy
  • 3,123
  • 7
  • 28
  • 22

12 Answers12

231

I wrote a simple jQuery extension to disable selection some time back: Disabling Selection in jQuery. You can invoke it through $('.button').disableSelection();

Alternately, using CSS (cross-browser):

.button {
        user-select: none;
        -moz-user-select: none;
        -khtml-user-select: none;
        -webkit-user-select: none;
        -o-user-select: none;
} 
aleemb
  • 29,883
  • 18
  • 95
  • 112
  • @kasperTaeymans thanks for pointing that out. updated the snippet. – aleemb Dec 19 '11 at 09:50
  • @kasperTaeymans But it is in a [W3C working draft](http://www.w3.org/TR/2000/WD-css3-userint-20000216#user-select)... I'd include it just in case. – Camilo Martin Sep 01 '12 at 01:20
  • 2
    Not to take anything away from the original answer, which I upvoted, but it is 3+ years old. So I added an answer below (http://stackoverflow.com/questions/924916/is-there-a-way-to-make-a-div-unselectable/24831222#24831222) with an added setting for touch interface. – Anne Gunn Jul 18 '14 at 18:07
  • That's what I wanted for movable bootstrap modal header! Thanks! – NoWar Sep 19 '18 at 03:24
69

The following CSS code works almost modern browser:

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

For IE, you must use JS or insert attribute in html tag.

<div id="foo" unselectable="on" class="unselectable">...</div>
KimKha
  • 4,230
  • 1
  • 36
  • 44
50

Just updating aleemb's original, much-upvoted answer with a couple of additions to the css.

We've been using the following combo:

.unselectable {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    -o-user-select: none;
    user-select: none;
}

We got the suggestion for adding the webkit-touch entry from:
http://phonegap-tips.com/articles/essential-phonegap-css-webkit-touch-callout.html

2015 Apr: Just updating my own answer with a variation that may come in handy. If you need to make the DIV selectable/unselectable on the fly and are willing to use Modernizr, the following works neatly in javascript:

    var userSelectProp = Modernizr.prefixed('userSelect');
    var specialDiv = document.querySelector('#specialDiv');
    specialDiv.style[userSelectProp] = 'none';
Anne Gunn
  • 2,279
  • 1
  • 28
  • 39
  • applying `user-select: none` to a `div` has no effect whatsoever. i'm pretty sure `user-select` is for text only. are there any other ways to make a `div` unselectable? – oldboy May 18 '17 at 06:04
22

As Johannes has already suggested, a background-image is probally the best way to achieve this in CSS alone.

A JavaScript solution would also have to affect "dragstart" to be effective across all popular browsers.

JavaScript:

<div onselectstart="return false;" ondragstart="return false;">your text</div>

jQuery:

var _preventDefault = function(evt) { evt.preventDefault(); };
$("div").bind("dragstart", _preventDefault).bind("selectstart", _preventDefault);

Rich

kim3er
  • 6,170
  • 4
  • 39
  • 66
20

You can use pointer-events: none; in your CSS

div {
  pointer-events: none;
}
Linh Dam
  • 1,743
  • 1
  • 16
  • 17
  • 1
    This is exactly what I needed to avoid my
    grabbing the selection from a hidden div. Thanks!
    – Jason Oct 30 '20 at 21:30
6

Wouldn't a simple background image for the textarea suffice?

Joey
  • 330,812
  • 81
  • 665
  • 668
5

you can try this:

<div onselectstart="return false">your text</div>
Fortega
  • 19,153
  • 14
  • 74
  • 112
4

WebKit browsers (ie Google Chrome and Safari) have a CSS solution similar to Mozilla's -moz-user-select:none

.no-select{    
    -webkit-user-select: none;
    cursor:not-allowed; /*makes it even more obvious*/
}
James
  • 4,017
  • 2
  • 17
  • 13
2

Also in IOS if you want to get rid of gray semi-transparent overlays appearing ontouch, add css:

-webkit-tap-highlight-color: rgba(0,0,0,0);
-webkit-touch-callout: none;
Artjom Kurapov
  • 6,023
  • 3
  • 30
  • 42
-1

Use

onselectstart="return false"

it prevents copying your content.

Anuraj
  • 2,383
  • 20
  • 26
-1

Make sure that you set position explicitly as absolute or relative for z-index to work for selection. I had a similar issue and this solved it for me.

Eric Grotke
  • 3,903
  • 2
  • 20
  • 19
-4

Not sure of your use case, but you could make it draggable.

Costa Michailidis
  • 7,107
  • 14
  • 64
  • 114