137

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?

4
  • I'm able to select input and textarea elements: jsfiddle.net/Smy26 Commented May 30, 2012 at 4:33
  • Looks like webkit allows selection on those elements, but moz doesn't. Commented May 30, 2012 at 4:40
  • 5
    possible duplicate of CSS rule to disable text selection highlighting Commented May 14, 2013 at 18:09
  • I know that this is an old question, but it would be nice to share in the question, why there was the need to deny text selection in the whole body as default. That could be useful to share, since most answers are just guessing your intentions. Thanks for sharing Commented Sep 14, 2023 at 19:37

8 Answers 8

225

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;
}
Sign up to request clarification or add additional context in comments.

Comments

43

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;
}

1 Comment

For full browser support you should add following attributes to your <body> or <div> element: unselectable="yes" onselectstart="return false" onmousedown="return false" /*use with care, the last attribute can AFFECT EXISTING FUNCTOIONALITY*/
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;"/>

1 Comment

Old versions of Firefox could require adding one more attribute to HTML: onmousedown="return false"
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

Comments

2
::selection,::moz-selection {color:currentColor;background:transparent}

2 Comments

As stated here 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 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: developer.mozilla.org/en-US/docs/Web/CSS/::selection . Another note, firefox is ready to remove the need for the prefix: bugzilla.mozilla.org/show_bug.cgi?id=509958
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;}

Comments

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">

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.