133

Currently, I have put this in the body tag to disable text selections:

body {
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
}

However, my input and textarea boxes are now unselectable. How can I only make these input elements selectable and the rest unselectable?

Toothbrush
  • 2,080
  • 24
  • 33
Neil
  • 2,802
  • 8
  • 34
  • 49
  • I'm able to select `input` and `textarea` elements: http://jsfiddle.net/Smy26/ – Sampson May 30 '12 at 04:33
  • Looks like webkit allows selection on those elements, but moz doesn't. – Christian May 30 '12 at 04:40
  • 5
    possible duplicate of [CSS rule to disable text selection highlighting](http://stackoverflow.com/questions/826782/css-rule-to-disable-text-selection-highlighting) – Mottie May 14 '13 at 18:09

9 Answers9

221

Don't apply these properties to the whole body. Move them to a class and apply that class to the elements you want to disable select:

.disable-select {
  -webkit-user-select: none;  
  -moz-user-select: none;    
  -ms-user-select: none;      
  user-select: none;
}
Someth Victory
  • 4,492
  • 2
  • 23
  • 27
  • 4
    [Not supported](https://developer.mozilla.org/en-US/docs/Web/CSS/user-select#Browser_compatibility) in Chrome, Safari. – knutole May 28 '14 at 19:45
  • 6
    @knutole Read the table again. The _value_ of `element` is not supported in Chrome/Safari. – MForMarlon Sep 05 '14 at 23:59
42

You could also disable user select on all elements:

* {
    -webkit-touch-callout:none;
    -webkit-user-select:none;
    -moz-user-select:none;
    -ms-user-select:none;
    user-select:none;
}

And than enable it on the elements you do want the user to be able to select:

input, textarea /*.contenteditable?*/ {
    -webkit-touch-callout:default;
    -webkit-user-select:text;
    -moz-user-select:text;
    -ms-user-select:text;
    user-select:text;
}
Dan
  • 55,715
  • 40
  • 116
  • 154
Rik
  • 3,328
  • 1
  • 20
  • 23
  • 1
    For full browser support you should add following attributes to your or
    element: unselectable="yes" onselectstart="return false" onmousedown="return false" /*use with care, the last attribute can AFFECT EXISTING FUNCTOIONALITY*/
    – Dan Jul 18 '13 at 12:19
13

Just wanted to summarize everything:

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

<div class="unselectable" unselectable="yes" onselectstart="return false;"/>
Meglio
  • 1,646
  • 2
  • 17
  • 33
  • 1
    Old versions of Firefox could require adding one more attribute to HTML: onmousedown="return false" – Dan Jul 18 '13 at 12:14
2

Use a wild card selector * for this purpose.

#div * { /* Narrowing, to specific elements, like  input, textarea is PREFFERED */
 -webkit-user-select: none;  
  -moz-user-select: none;    
  -ms-user-select: none;      
  user-select: none;
}

Now, every element inside a div with id div will have no selection.

Demo

Community
  • 1
  • 1
Starx
  • 77,474
  • 47
  • 185
  • 261
2
::selection,::moz-selection {color:currentColor;background:transparent}
B T
  • 57,525
  • 34
  • 189
  • 207
  • As stated here http://www.w3schools.com/cssref/sel_selection.asp. This is not part of any CSS specification so far, and Firefox requires -moz-. So the correct should be: ::-moz-selection {color:currentColor;background:transparent} ::selection {color:currentColor;background:transparent} – Vladislav Jul 13 '16 at 06:24
  • 1
    @Vladislav Good point about the moz prefix. Honestly tho, don't trust w3schools. Its such a terrible source. MDN gives the *actual* browser compatibility versions and notes that ::selection was removed from the CSS draft, BUT was added back: https://developer.mozilla.org/en-US/docs/Web/CSS/::selection . Another note, firefox is ready to remove the need for the prefix: https://bugzilla.mozilla.org/show_bug.cgi?id=509958 – B T Jul 13 '16 at 22:19
2

you can disable all selection

.disable-all{-webkit-touch-callout: none;  -webkit-user-select: none;-khtml-user-select: none;-moz-user-select: none;-ms-user-select: none;user-select: none;}

now you can enable input and text-area enable

input, textarea{
-webkit-touch-callout:default;
-webkit-user-select:text;
-khtml-user-select: text;
-moz-user-select:text;
-ms-user-select:text;
user-select:text;}
Shahnawaz
  • 105
  • 1
  • 12
0

instead of body type a list of elements you want.

Eric Fortis
  • 16,372
  • 6
  • 41
  • 62
0

I agree with Someth Victory, you need to add a specific class to some elements you want to be unselectable.

Also, you may add this class in specific cases using javascript. Example here Making content unselectable with help of CSS.

Florke64
  • 5
  • 5
starikovs
  • 3,240
  • 4
  • 28
  • 33
0

Disable selection everywhere

input, textarea ,*[contenteditable=true] {
  -webkit-touch-callout:default;
  -webkit-user-select:text;
  -moz-user-select:text;
  -ms-user-select:text;
  user-select:text;
   }

IE7

 <BODY oncontextmenu="return false" onselectstart="return false" ondragstart="return false">
cristianojeda
  • 339
  • 2
  • 12