Reputation: 3253
I have a textarea with a transparent background overlaying some text that I'd like to use as a sort of watermark. The text is large and takes up a majority of the textarea. It looks nice, but the problem is when the user clicks in the textarea it sometimes selects the watermark text instead.
I want the watermark text to never be selectable. I was expecting if something was lower in the z-index it would not be selectable, but browsers don't seem to care about z-index layers when selecting items. Is there a trick or way to make it so this DIV is never selectable?
Upvotes: 170
Views: 216210
Reputation: 2749
Use
onselectstart="return false"
It prevents copying your content.
Upvotes: -2
Reputation: 4152
WebKit browsers (i.e., Google Chrome and Safari) have a CSS solution similar to Mozilla's -moz-user-select: none
:
.no-select{
-webkit-user-select: none;
cursor:not-allowed; /* Makes it even more obvious */
}
Upvotes: 5
Reputation: 2447
We've been using the following combination:
.unselectable {
-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;
}
We got the suggestion for adding the webkit-touch entry from: Essential PhoneGap CSS: WebKit Touch Callout
2015 Apr: Here is a variation that may come in handy. If you need to make the DIV selectable/unselectable on the fly and are willing to use Modernizr, the following works neatly in JavaScript:
var userSelectProp = Modernizr.prefixed('userSelect');
var specialDiv = document.querySelector('#specialDiv');
specialDiv.style[userSelectProp] = 'none';
Upvotes: 56
Reputation: 6155
Also in iOS if you want to get rid of gray semi-transparent overlays appearing ontouch, add this CSS content:
-webkit-tap-highlight-color: rgba(0,0,0,0);
-webkit-touch-callout: none;
Upvotes: 2
Reputation: 4510
The following CSS code works in almost all modern browsers:
.unselectable {
-moz-user-select: -moz-none;
-khtml-user-select: none;
-webkit-user-select: none;
-o-user-select: none;
user-select: none;
}
For Internet Explorer, you must use JavaScript or insert an attribute in the HTML tag.
<div id="foo" unselectable="on" class="unselectable">...</div>
Upvotes: 74
Reputation: 6466
As Johannes has already suggested, a background image is probably the best way to achieve this in CSS alone.
A JavaScript solution would also have to affect "dragstart" to be effective across all popular browsers.
JavaScript:
<div onselectstart="return false;" ondragstart="return false;">your text</div>
jQuery:
var _preventDefault = function(evt) { evt.preventDefault(); };
$("div").bind("dragstart", _preventDefault).bind("selectstart", _preventDefault);
Upvotes: 22
Reputation: 32065
I wrote a simple jQuery extension to disable selection some time back: Disabling Selection in jQuery. You can invoke it through $('.button').disableSelection();
Alternately, using CSS (cross-browser):
.button {
user-select: none;
-moz-user-select: none;
-khtml-user-select: none;
-webkit-user-select: none;
-o-user-select: none;
}
Upvotes: 248
Reputation: 19682
You can try this:
<div onselectstart="return false">your text</div>
Upvotes: 5
Reputation: 31
Yes, there are multiple ways.
You could simply add the user-select
CSS declaration and set it to none
, like this
div {
user-select: none;
}
Also you could accomplish this with the CSS ::selection
selector and set the selection background color to match your own. This could get tricky.:
p::selection {
background: rgba(0, 0, 0, 0)
}
Option 1 being the best option in most cases for obvious reasons!
Upvotes: 1
Reputation: 5125
Make sure that you set position explicitly as absolute or relative for z-index to work for selection. I had a similar issue and this solved it for me.
Upvotes: -1
Reputation: 2219
You can use pointer-events: none;
in your CSS
div {
pointer-events: none;
}
Upvotes: 25