Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

User-Select still copies to clipboard

I just discovered the user-select attribute of CSS. I was wanting to find a way for people to copy the displayed results on a page, without copying the headings as well (and a few other things). Each browser is a bit different when they attempt to select something. But I've been testing mainly in Chrome. Fiddle Code

HTML

<div>
    <span class="no-select heading">Heading 1 - can't select it</span>
    <p>This text you can select & copy.</p>
    <span class="no-select heading">Heading 2 - can't select it</span>
    <p>This text you can select & copy.</p>
</div>

CSS

.no-select {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -moz-user-select: -moz-none;
    -ms-user-select: none;
    -o-user-select: none;
    user-select: none;
}

The results:

user-select highlighting

To me, it looks like they would only be able to copy the highlighted text. However, when copying what is highlighted - it does have heading 2, but it did not copy heading 1. Why is this?

This text you can select & copy.

Heading 2 - can't select it
This text you can select & copy.
like image 938
EnigmaRM Avatar asked Jun 07 '13 17:06

EnigmaRM


People also ask

How do I copy text to clipboard?

Select the first item that you want to copy, and press CTRL+C. Continue copying items from the same or other files until you have collected all of the items that you want. The Office Clipboard can hold up to 24 items.

What does copied to clipboard mean?

Android can cut, copy and paste text, and like a computer, the operating system transfers the data to the clipboard. Unless you use an app or extension like Clipper or aNdClip to retain your clipboard history, however, once you copy new data to the clipboard, the old information is lost.

How do I stop copying text from a website?

You can disable cut, copy, and paste on a web page using the jQuery bind() function. In the bind() function, you have to specify the cut, copy, and paste events that are fired when a user tries to cut, copy, or paste anything on the web page.

How many items can clipboard hold at maximum?

In Excel 2007 this can be displayed by displaying the Home tab and clicking on the small icon in the bottom right corner of the Clipboard group. This clipboard can hold a maximum of 24 items (text and graphics) and is also commonly referred to as the Clipboard viewer.


2 Answers

I don't really think its all that surprising, the user-select property is to prevent a user from selecting an elements' content. Nowhere in the Basic UI Module does it mention the behaviour regarding copying content. I would imagine this is why there are variations among different browsers.

MDN also states:

Controls the appearance (only) of selection. This does not have any affect on actual selection operation.

The comments in this WebKit Bugzilla report also say the same thing.

like image 67
Adrift Avatar answered Nov 08 '22 14:11

Adrift


I had the same problem and found the following solution. https://danoc.me/blog/css-prevent-copy/

html:

<p data-pseudo-content="Lorem Ipsum"></p>

css:

[data-pseudo-content]::before {
  content: attr(data-pseudo-content);
}
like image 41
domruf Avatar answered Nov 08 '22 15:11

domruf