11

How can I make certain parts of my html not selectable?

I have some absolutely positioned divs that keep getting selected if a user tries to select the content of my page.

Is there a way to make it so certain elements (such as those divs) aren't selectable?

EDIT: The main issue is when someone copies these absolute div's and then pastes them into the rich text editor on my site, the rich text editor breaks on IE.

Kyle
  • 21,377
  • 37
  • 113
  • 200

5 Answers5

9

For IE, add the attribute unselectable="ON". (EDIT: Although it won't completely help)

For Gecko, add the CSS style -moz-user-select: none.

For WebKit, add the CSS style -webkit-user-select: none.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • Tried `unselectable="ON"` and it didn't work. Reading MSDN says: `An element with the UNSELECTABLE attribute set to on can be included in a selection that starts somewhere outside the element.` So it still gets unintentionally selected when they try to copy the content area. – Kyle Nov 30 '10 at 02:10
  • +1 Great! And the unselectable attribute works in Opera, too. – Šime Vidas Nov 30 '10 at 02:13
4

If you want to just hide selection rectangle, you can use

element::selection { background: transparent }

and it's counterpart for Gecko

element::-moz-selection { background: transparent }

If you want to help your users select right text to copy to clipboard, you can move the element away. Usually browsers retain order of elements as they are defined in HTML when selecting. While it is not perfect solution, you can place the element's code in HTML either at the very beginning or very end of <body> tag. This way, if user selects something in the middle of the page, the element will be too "far away" to interfere.

Athari
  • 33,702
  • 16
  • 105
  • 146
  • +1 - Yes, I want to help them select the right text for copying. Good suggestion about putting the absolute divs too far away to be selected. – Kyle Nov 30 '10 at 02:13
  • +1 @Athari : This is the correct solution. Since your `div`s are absolute position'ed, you can reorder where they appear within the HTML. This would help user's select the right text.In fact, I commend you @Kyle for taking this into consideration, as I do get really annoyed when random `div`s get selected when I try to clip selections. – bguiz Nov 30 '10 at 02:19
4

this article might help with a cross-browser solution: (uses jQuery) http://aleembawany.com/2009/01/20/disable-selction-on-menu-items-with-this-jquery-extension/

in theory, you can extend the jQUery core using this:

jQuery.fn.extend({ 
    disableSelection : function() { 
            return this.each(function() { 
                    this.onselectstart = function() { return false; }; 
                    this.unselectable = "on"; 
                    jQuery(this).addClass('unselectable');
            }); 
    } 
}); 

and applying it like this:

// disable selection on selected objects 
$('.myClass').disableSelection(); 

you must make sure you have this in your stylesheet:

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

looks as if it basically does what SLaks and Sime have mentioned. i haven't tried it, but i'd be interested to know if it works

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
3

You can use the ::selection pseudo-element to make the selection of a certain element "invisible":

p::selection { background-color:transparent; }  
p::-moz-selection { background-color:transparent; }

However, that doesn't work in IE.

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
2

jQuery Solution:

$(".unselectable").disableSelection();
Jeaf Gilbert
  • 11,495
  • 19
  • 78
  • 105