Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS disable text selection

Tags:

css

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?

like image 959
Neil Avatar asked May 30 '12 04:05

Neil


People also ask

How do I stop selecting text in CSS?

To disable text selection highlighting in Internet Explorer/Edge browser using CSS just set -ms-user-select CSS property to none.

How do you block text selection?

For a quick a glimpse of what you can do, just click anywhere in the middle of some text, hold down the [Alt] key, and make a small circle with your mouse. By using the [Alt] key while dragging your mouse, you can customize the shape and size of the selected block of text—it's up to you, not Word!

How do I mute text in CSS?

Use the user-select: none; CSS rule.


6 Answers

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;
}
like image 80
Someth Victory Avatar answered Sep 30 '22 12:09

Someth Victory


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;
}
like image 33
Rik Avatar answered Sep 30 '22 12:09

Rik


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;"/>
like image 23
Meglio Avatar answered Sep 30 '22 12:09

Meglio


::selection,::moz-selection {color:currentColor;background:transparent}
like image 42
B T Avatar answered Sep 30 '22 11:09

B T


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;}
like image 20
Shahnawaz Avatar answered Sep 30 '22 11:09

Shahnawaz


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

like image 33
Starx Avatar answered Sep 30 '22 13:09

Starx