Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make text not copyable HTML

Tags:

html

css

This is not a duplicate of the question above

I use material-icons on my website. In order to add an icon, you'd have to make something like this:

<p class="material-icons">info_outline</p>

If I now copy that icon, it would copy the text "info_outline". I know you can make an element unselectable using user-select: none; inside you css, but there is a problem with that.

Take my snippet for example. If I create an p element, with a span inside which has user-select: none; you won't be able to select (and therefor copy) the span. However, if you copy the whole p element, you will still get the contents of the span. How can I prevent this from happening?

span {
  user-select: none;
}

input {
  width: 100%;
}
<p>Copy this text with a <span>unselectable</span> piece of text... Or is it?</p>
<input type="text" placeholder="Past text here">

Edit: Since a lot of people say it's a duplicate question to the questions where the answer is user-select: none;, just take another look at my question.

I know how user-select works! I know you can make a element unselectable. However, if you select the element around it and copy it's contents, it will copy all it's content, even the element with the user-select: none;

like image 772
FlorisdG Avatar asked Apr 04 '17 09:04

FlorisdG


People also ask

How do I stop text from copying in HTML?

You can disable cut, copy, and paste using the oncut, oncopy, and onpaste event attributes with the target HTML elements. If you want to disable cut, copy, and paste for the complete web page, you need to use these event attributes with the body tag.

How do I stop copying text?

This process is done by copying or right-clicking on your website. If you are a website owner and would like to prevent your content from being copied, we recommend disabling copy and right-click buttons from posts and pages. Another way of copying a website content is by using hotkeys like Ctrl+V, Ctrl+C, Ctrl+A, etc.

How do you make text copyable in HTML?

Add the following javascript code to your page:The function copies the visible text of the element to the clipboard. This works as if you had selected the text and copied it with ctrl+c. Use the parameter "id" to select the element you want to copy.


1 Answers

First of all make the element unselectable:

-webkit-touch-callout: none;
-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;

This already works in Firefox. To get it work in other browsers, you have to work with pseudo-elements and data-attribute.

Use a data-attribute like this:

<span data-unselectable="unselectable content"></span>

Now we can add this text in the content of our pseudo-element ::before or ::after:

span::before {
  content: attr(data-unselectable);
}

This works because the dom will not be updated by the content attribute.

like image 189
Huelfe Avatar answered Oct 09 '22 00:10

Huelfe