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.
On this page
Using 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.
In Google Chrome:
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; }
In mozilla firefox:
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; }
In Safari:
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; }
In IOS Safari:
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; }
In Internet Explorer/Edge using:
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 value | description |
---|---|
none | user cannot select the text |
text | user can select the text |
all | user can select the text with one click |
auto | user-select value depend upon its parent user-select option |
contain | selection will be bound to particular element |
element | IE 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.
- 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
- Otherwise the default behavior is text. that is user can select the text.
- On pseudo elements ::before and ::after the behavior is none
- 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.