Disable Text Selection Highlighting In HTML Using CSS


We can use user-select property in CSS to disable text selection highlighting in HTML pages.It is not a standard feature, but available in all modern browsers except IE 9 & before.

Table of Contents

Disable text selection css

Disable text selection css

Disable text selection in HTML using CSS user-select:none:

To disable the text selection in HTML we need to give user-select property value as none. Go through the below example to understand if further.

<div>
You can select this text.
</div>
<div class="disable-select">
You cannot select this text,text selection is disabled
</div>

I have added disable-select class to the second div now we will add user-select css property

.disable-select {
    user-select: none; /* supported by Chrome and Opera */
   -webkit-user-select: none; /* Safari */
   -khtml-user-select: none; /* Konqueror HTML */
   -moz-user-select: none; /* Firefox */
   -ms-user-select: none; /* Internet Explorer/Edge */
}

But we have to add browser specific prefix before the user-select option for safari,firefox and internet explorer or edge.

Chrome and opera supports non prefixed versions.

Disable text selection highlighting in Google Chrome using CSS:

To disable text selection highlighting in Google Chrome browser using CSS just set -user-select CSS property to none.

And no prefix is required for Google Chrome and Opera Browsers.

 .disable-select{
  user-select:none;
}

Disable text selection highlighting in mozilla firefox using CSS:

To disable text selection highlighting in mozilla firefox browser using CSS just set -moz-user-select CSS property to none.

And we need add -moz prefix before user-select property for mozilla firefox Browser.

 .disable-select{
   -moz-user-select:none;
}

Disable text selection highlighting in Safari using CSS:

To disable text selection highlighting in Safari browser using CSS just set -webkit-user-select CSS property to none.

And we need add -webkit prefix before user-select property for Safari Browser.

 .disable-select{
  -webkit-user-select:none;
}

Disable text selection highlighting in IOS Safari using CSS:

To disable text selection highlighting in IOS Safari browser using CSS just set -webkit-touch-callout CSS property to none.

 .disable-select{
  -webkit-touch-callout:none;
}

Disable text selection highlighting in Internet Explorer/Edge using CSS:

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

And we need add -ms prefix before user-select property for Safari Browser.

 .disable-select{
   -ms-user-select:none;
}

What does user-select property will do?

user-select css property controls whether a text in a HTML element can be selected or not. It is not a standard feature.

You can find more details about draft specification here

user-select property values:

user-select valuedescription
noneuser cannot select the text
textuser can select the text
alluser can select the text with one click
autouser-select value depend upon its parent user-select option
containselection will be bound to particular element
elementIE version of user-select contain.

user-select none:

As explained above, when we give user-select property value as none to an HTML element we cannot select the text inside the element including it’s children element.

<div style="user-select:none;border:1px solid">
text selection is disabled 
<div>Text selection is disabled in children element also</div>
</div>

user-select text:

When you give user-select property as text, user can select the text.

<div style="user-select: none; border: 1px solid;">
text selection is disabled
<div style="user-select: text;">You can select me</div>
<div> text selection is disabled</div>
</div>

user-select all:

When we give user-select property as all. Text inside the element is automatically selected on context click.

<div style="user-select:all">
On click we can select the text
</div>

user-select auto:

user-select auto behavior depends upon its parent element’s computed value of user-select.

  1. If the parent element’s computed value is none then it’s value is none. or if the computed value is all then it’s value is all. or if the value is text it’s value is text
  2. Otherwise the default behavior is text. that is user can select the text.
  3. On pseudo elements ::before and ::after the behavior is none
  4. And if the element is an editable element i.e., text or textarea the computed value is contain or element (In IE)

user-select contain:

user-select contain is not supported in other browsers except internet explorer. In IE we have to give user-select option as element instead of contain.

So what exactly this user-select contain will do?

When you give user-select as contain or element selection will be bound to that element and cannot be extended.

Go through the below demo to understand it further.

user-select CSS example:

We will see all user-select options in one place.

<h3>user-select:none</h3>
<div class="text-selection-none">
text selection is disabled 
<div>Text selection is disabled in children element also</div>
</div>

<h3>user-select:text</h3>
<div class="text-selection-none">
text selection is disabled
<div class="text-selection-text">You can select me</div>
<div> text selection is disabled</div>
</div>

<h3>user-select:all</h3>
<div class="text-selection-all">
  On click we can select the text
</div>

<h3>user-select:auto</h3>
<div class="text-selection-none">
text selection is disabled
<div class="text-selection-auto">as parent element is none cannot select text</div>
<div> text selection is disabled</div>
<br/>

<div class="text-selection-text">
text selection is enabled
<div class="text-selection-auto">as parent element is text,can select text</div>
<div> text selection is enabled</div>

<br/>

<div class="before text-selection-auto">as parent element is text,can select text<br/></div>

<h3>
user-select:contain
</h3>

<div class="text-selection-contain">text selection is contain<br/></div>


<div>This is not selected</div>

And the corresponding CSS values are

.text-selection-none{
user-select: none; /* supported by Chrome and Opera */
-webkit-user-select: none; /* Safari */
-khtml-user-select: none; /* Konqueror HTML */
-moz-user-select: none; /* Firefox */
-ms-user-select: none;
}
.text-selection-text{
user-select: text; /* supported by Chrome and Opera */
-webkit-user-select: text; /* Safari */
-khtml-user-select: text; /* Konqueror HTML */
-moz-user-select: text; /* Firefox */
-ms-user-select: text;
}
.text-selection-all{
user-select: all; /* supported by Chrome and Opera */
-webkit-user-select: all; /* Safari */
-khtml-user-select: all; /* Konqueror HTML */
-moz-user-select: all; /* Firefox */
-ms-user-select: all;
}
.text-selection-auto{
user-select: auto; /* supported by Chrome and Opera */
-webkit-user-select: auto; /* Safari */
-khtml-user-select: auto; /* Konqueror HTML */
-moz-user-select: auto; /* Firefox */
-ms-user-select: auto;
}
.text-selection-contain{
user-select: contain; 
-webkit-user-select: contain; 
-khtml-user-select: contain; 
-moz-user-select: contain; 
-ms-user-select: element; /*Only IE supports user-select contain option*/
}
div.before::after {
content: "hi";
}

Here is the jsFiddle demo

As explained above user-select : contain option is only supported in IE, if you open the fiddle in IE, You can observe its behaviour the element selection cannot be extended beyond the element with class .text-selection-contain.

Wait before leaving. why can’t you follow me on twitter or be a friend on Facebook or linkedn to get in touch with me.

Spread the word

Get free link to download 900+ Material Icons
Avatar
Arunkumar Gudelli

I am One among a million Software engineers of India. I write beautiful markup.I make the Web useful.

Related

Next
Previous