Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent user copying text of a certain div using CSS?

I know one can use the below CSS to disable user text selection.

   .unselectable{
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    }

But when I have the following html:

<p>selectable text 1</p>
<p class="unselectable">unselectable text</p>
<p>selectable text 2</p>

Users can still copy the unselectable text by selecting from the very top of the page (selectable text 1) to the very bottom of the page (selectable text 2). Any ways to prevent that? Thanks.

like image 556
user2335065 Avatar asked Nov 19 '14 02:11

user2335065


People also ask

How do I block copy paste in CSS?

Use user-select CSS property with the HTML tag on those you want to disable the text selection of. You can target those HTML elements using a class or ID. In the below example, text selection is disabled for the 2nd div tag. Here, class is used to target the 2nd div.

How do I stop people from copying text messages?

You can allow text selection, but prevent copy and cut functions using the oncopy, oncut and onpaste event attributes. By adding these attributes into a textbox's <input> tag, you can disable cut, copy and paste features. The user is left with the option to enter the field manually with these attributes set.

How do I stop text selection 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 I make sure text stay on one line CSS?

If you want to limit the text length to one line, you can clip the line, display an ellipsis or a custom string. All these can be done with the CSS text-overflow property, which determines how the overflowed content must be signalled to the user.


1 Answers

Questions seems vague.

There is a difference between "Selection" and "Copying" on HTML pages.You can prevent "Selection" using CSS, but you can not prevent "Copying" using CSS. You need JS for that.

like image 83
Sudhir Kaushik Avatar answered Nov 15 '22 05:11

Sudhir Kaushik