Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

make html text unselectable

Tags:

html

First, I have been here How to make HTML Text unselectable
But that doesn't solve my issue, (the JavaScript version works well, but I need to make it using HTML and CSS, I cannot use JavaScript) I used the recommended CSS, but look at my DEMO drag the mouse from [drag from here] to [to here] you will see that the text is still selectable.
any way to make it really unselectable?
Thanks

like image 909
ilyes kooli Avatar asked May 04 '12 17:05

ilyes kooli


People also ask

How do you stop text highlighting in HTML?

You can use the user-select property to disable text selection of an element. In web browsers, if you double-click on some text it will be selected/highlighted. This property can be used to prevent this.

How do you make an element Unhighlightable in HTML?

For images not to be highlighted make them as background. You can also place a transparent div on top of the area where you don't want the selection to occur. Position the div so that they will appear on top of the element with a higher stacking order. For a greater stacking order you can use the z-index property.

What is Unselectable in HTML?

An element with the UNSELECTABLE attribute set to on can be included in a selection that starts somewhere outside the element. The UNSELECTABLE attribute is implemented as an expando. Setting the expando property of the document object to false precludes the functionality of all expandos.


1 Answers

You can use CSS like this:

CSS

.unselectable {
  -webkit-user-select: none;  /* Chrome all / Safari all */
  -moz-user-select: none;     /* Firefox all */
  -ms-user-select: none;      /* IE 10+ */

  /* No support for these yet, use at own risk */
  -o-user-select: none;
  user-select: none;
}

For older IE versions, the issue usually is harder to deal with, but you can use a behavior:

CSS

.unselectable {
   behavior: url(ieUserSelectFix.htc);
}

and the behavior file "ieUserSelectFix.htc":

<public:component lightweight="true">

    <public:attach event="ondocumentready" onevent="stopSelection()" />

    <script type="text/javascript">
    <!--
        function stopSelection() {
            element.onselectstart = function() { return(false); };
            element.setAttribute('unselectable', 'on', 0);
        }
    //-->
    </script>
</public:component>

With Javascript you can:

yourElement.onselectstart = function() { return(false); };
like image 99
Zuul Avatar answered Oct 13 '22 16:10

Zuul