148

I'm building an HTML UI with some text elements, such as tab names, which look bad when selected. Unfortunately, it's very easy for a user to double-click a tab name, which selects it by default in many browsers.

I might be able to solve this with a JavaScript trick (I'd like to see those answers, too) -- but I'm really hoping there's something in CSS/HTML directly that works across all browsers.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Tyler
  • 28,498
  • 11
  • 90
  • 106
  • Depending on your needs http://stackoverflow.com/q/4117466/298479 might also be a valid solution for you. – ThiefMaster May 10 '11 at 06:35
  • 2
    While many of the examples listed here work, keep in mind nothing prevents someone from just looking at the source code and copying the text. – Ren Sep 16 '08 at 06:05

16 Answers16

208

In most browsers, this can be achieved using CSS:

*.unselectable {
   -moz-user-select: -moz-none;
   -khtml-user-select: none;
   -webkit-user-select: none;

   /*
     Introduced in IE 10.
     See http://ie.microsoft.com/testdrive/HTML5/msUserSelect/
   */
   -ms-user-select: none;
   user-select: none;
}

For IE < 10 and Opera, you will need to use the unselectable attribute of the element you wish to be unselectable. You can set this using an attribute in HTML:

<div id="foo" unselectable="on" class="unselectable">...</div>

Sadly this property isn't inherited, meaning you have to put an attribute in the start tag of every element inside the <div>. If this is a problem, you could instead use JavaScript to do this recursively for an element's descendants:

function makeUnselectable(node) {
    if (node.nodeType == 1) {
        node.setAttribute("unselectable", "on");
    }
    var child = node.firstChild;
    while (child) {
        makeUnselectable(child);
        child = child.nextSibling;
    }
}

makeUnselectable(document.getElementById("foo"));
thanksd
  • 54,176
  • 22
  • 157
  • 150
Tim Down
  • 318,141
  • 75
  • 454
  • 536
  • 18
    you can use this selector [unselectable=on]{...} then you avoid putting extra class – venimus Apr 29 '11 at 18:33
  • 1
    According to http://dev.l-c-n.com/CSS3-selectors/browser-support.php , this selector should be supported in IE8 and above, and possibly IE7. – EricP Nov 17 '11 at 05:14
  • @JoeCoder: Yes, I believe attribute selectors do work in IE 7, although not IE 6. – Tim Down Nov 17 '11 at 09:23
  • 2
    Just curious, is there an advantage to using *.unselectable over .unselectable in the stylesheet? – undefined Feb 06 '13 at 21:15
  • @BrianMortenson: None at all: they are equivalent. I have a very old habit of including the `*` in that kind of selector to remind myself explicitly that I'm matching all tag names. However, I no longer find it helpful and it's now just pure habit, so I've pretty much stopped doing it now. – Tim Down Feb 06 '13 at 23:51
  • funny that @TimDown harvested another 113 rep by copying his answer from this question's duplicate. oh wait! 114 ;) – ericsoco Jul 03 '13 at 05:23
  • @ericsoco: Yeah. It's an interesting issue: if others have posted answers that people are upvoting and I think I have a better answer, should I post it? The system still allows people to find, view and vote on answers to closed questions, after all. I can't deny that desire for rep has occasionally motivated me to add an answer to a duplicate question on a pet subject rather than vote to close it. – Tim Down Jul 03 '13 at 08:27
  • of course you should and of course you should post your answers everywhere they can be found. the point of answering, ultimately, is not about rep -- it's about providing answers in places they can be found. i'm all for it. – ericsoco Jul 03 '13 at 20:09
  • 1
    @venimus I would favor classes over xpath selectors like [unselectable=on] in CSS. In jquery they're good if you narrow down to an immediate container first but in CSS, selectors are parsed right to left so it's still a blanket check of every element and every element's unselectable attribute using the performance-meh xpath engine. Likewise, I believe, with the querySelector API which I'm guessing typically hooks into the CSS/xpath selector engines directly. This may change over time as browsers find perf tweaks but I would definitely keep this in mind when supporting IE<=8, maybe <=9. – Erik Reppen Oct 02 '13 at 16:28
  • Probably you are right, but I think early optimizations are evil. Also you won't need the JS if you use [unselectable=on] * {...} – venimus Oct 03 '13 at 16:09
  • @venimus: You can't avoid the JS if you want IE <= 9 support and don't want to put `unselectable="on"` on every element. All you can avoid with your selector is adding `class="unselectable"`. – Tim Down Oct 03 '13 at 16:18
41
<script type="text/javascript">

/***********************************************
* Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code

***********************************************/


function disableSelection(target){

    if (typeof target.onselectstart!="undefined") //IE route
        target.onselectstart=function(){return false}

    else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
        target.style.MozUserSelect="none"

    else //All other route (ie: Opera)
        target.onmousedown=function(){return false}

    target.style.cursor = "default"
}



//Sample usages
//disableSelection(document.body) //Disable text selection on entire body
//disableSelection(document.getElementById("mydiv")) //Disable text selection on element with id="mydiv"


</script>

EDIT

Code apparently comes from http://www.dynamicdrive.com

luqui
  • 59,485
  • 12
  • 145
  • 204
dimarzionist
  • 18,517
  • 4
  • 22
  • 23
36

All of the correct CSS variations are:

-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Blowsie
  • 40,239
  • 15
  • 88
  • 108
  • 1
    According to this [Answer](http://stackoverflow.com/a/25015048/642706), the order of those two webkit settings may be critical, where the `-webkit-user-select:` should come before the `-webkit-touch-callout:`. I have not verified this. – Basil Bourque Nov 15 '14 at 06:23
13

Try this:

<div onselectstart="return false">some stuff</div>

Simple, but effective... works in current versions of all major browsers.

Stephen M. Redd
  • 5,378
  • 1
  • 24
  • 32
11

For Firefox you can apply the CSS declaration "-moz-user-select" to "none". Check out their documentation, user-select.

It's a "preview" of the future "user-select" as they say, so maybe Opera or WebKit-based browsers will support that. I also recall finding something for Internet Explorer, but I don't remember what :).

Anyway, unless it's a specific situation where text-selecting makes some dynamic functionality fail, you shouldn't really override what users are expecting from a webpage, and that is being able to select any text they want.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jorge Alves
  • 1,148
  • 6
  • 13
9

I'm finding some level of success with the CSS described here http://www.quirksmode.org/css/selection.html:

::selection {
    background-color: transparent;
}

It took care of most of the issues I was having with some ThemeRoller ul elements in an AIR application (WebKit engine). Still getting a small (approx. 15 x 15) patch of nothingness that gets selected, but half the page was being selected before.

thanksd
  • 54,176
  • 22
  • 157
  • 150
jlleblanc
  • 3,510
  • 25
  • 26
6

Absolutely position divs over the text area with a z-index higher and give these divs a transparent GIF background graphic.

Note after a bit more thought - You'd need to have these 'covers' be linked so clicking on them would take you to where the tab was supposed to, which means you could/should do this with the anchor element set to display:box, width and height set as well as the transparent background image.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Dave Rutledge
  • 5,525
  • 7
  • 27
  • 24
4

For Safari, -khtml-user-select: none, just like Mozilla's -moz-user-select (or, in JavaScript, target.style.KhtmlUserSelect="none";).

kapa
  • 77,694
  • 21
  • 158
  • 175
Alan Hensel
  • 823
  • 7
  • 15
4

For an example of why it might be desirable to suppress selection, see SIMILE TImeline, which uses drag-and-drop to explore the timeline, during which accidental vertical mouse movement causes the labels to be highlighted unexpectedly, which looks weird.

pdc
  • 2,314
  • 20
  • 28
3

Here's a Sass mixin (scss) for those interested. Compass/CSS 3 doesn't seem to have a user-select mixin.

// @usage use within a rule
// ex. img {@include user-select(none);}
// @param assumed valid user-select value
@mixin user-select($value)
{
    & {
        -webkit-touch-callout: $value;
        -webkit-user-select: $value;
        -khtml-user-select: $value;
        -moz-user-select: $value;
        -ms-user-select: $value;
        user-select: $value;
    }
}

Though Compass would do it in a more robust way, i.e. only add support for vendors you've chosen.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rgb
  • 1,246
  • 1
  • 10
  • 14
3

"If your content is really interesting, then there is little you can ultimately do to protect it"

That's true, but most copying, in my experience, has nothing to do with "ultimately" or geeks or determined plagiarists or anything like that. It's usually casual copying by clueless people, and even a simple, easily defeated protection (easily defeated by folks like us, that is) works quite well to stop them. They don't know anything about "view source" or caches or anything else... heck, they don't even know what a web browser is or that they're using one.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Big Bill
  • 39
  • 1
1

Any JavaScript or CSS method is easily circumvented with Firebug (like Flickr's case).

You can use the ::selection pseudo-element in CSS to alter the highlight color.

If the tabs are links and the dotted rectangle in active state is of concern, you can remove that too (consider usability of course).

Community
  • 1
  • 1
Taylor D. Edmiston
  • 12,088
  • 6
  • 56
  • 76
1

Images can be selected too.

There are limits to using JavaScript to deselect text, as it might happen even in places where you want to select. To ensure a rich and successful career, steer clear of all requirements that need ability to influence or manage the browser beyond the ordinary... unless, of course, they are paying you extremely well.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kinjal Dixit
  • 7,777
  • 2
  • 59
  • 68
1

If it looks bad you can use CSS to change the appearance of selected sections.

Wedge
  • 19,513
  • 7
  • 48
  • 71
1

There are many occasions when turning off selectability enhances the user experience.

For instance allowing the user to copy a block of text on the page without copying the text of any interface elements associated with it (that would become interspersed within the text being copied).

kbcom
  • 11
  • 1
0

The following works in Firefox interestingly enough if I remove the write line it doesn't work. Anyone have any insight why the write line is needed.

<script type="text/javascript">
document.write(".");
document.body.style.MozUserSelect='none';
</script>
kapa
  • 77,694
  • 21
  • 158
  • 175
hbtdev
  • 1
  • 2
    I guess body might not be accessible in DOM before it has content, thus you can't set it's style before you write something in the document. – Hubro Nov 22 '10 at 11:17