{"id":201868,"date":"2015-06-05T10:24:37","date_gmt":"2015-06-05T17:24:37","guid":{"rendered":"http:\/\/css-tricks.com\/?p=201868"},"modified":"2021-08-03T13:02:02","modified_gmt":"2021-08-03T20:02:02","slug":"copy-paste-the-web","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/copy-paste-the-web\/","title":{"rendered":"Copy & Paste & The Web"},"content":{"rendered":"

Have you ever tried to copy and paste some text only to find it frustrating, awkward, or even impossible? I bet you have. It’s not random. On the web, you have a decent amount of control over it. Let’s just lay a bunch of stuff out there as food for thought.<\/p>\n

<\/p>\n

Not Actually Text<\/h3>\n

Perhaps this is the most obvious problem, so let’s not dwell on it. If text can be “real text” on the web, please do that. Compared to an image of text, real text is more accessible, more indexable, and more useful. You can select it to copy and paste!<\/p>\n

Accidental Overlaps<\/h3>\n

Have you ever seen that effect where text fades out at the bottom? It’s often done by laying a gradient image over top of that text. You can see through part of it, but that element is still sitting on top of the text, meaning mouse interaction happens on the overlap, not the text.<\/p>\n

\"\"
Because of the overlap, I can’t properly select the text I want here. Worse, the link at the bottom is unclickable.<\/figcaption><\/figure>\n

In this demo<\/a>, the overlap is a pseudo element filled with a a gradient and positioned on top. Thankfully, this is largely a fixable situation. pointer-events: none;<\/code> on the overlap will go a long way in helping. With that set, pointer interactions will totally ignore the element. Clicks will go right through as if it wasn’t even there.<\/p>\n

That’s great, except IE 10 doesn’t support pointer-events. If you still wanted to rely on that, Modernizr v3<\/a> has a test for it, so you could only apply the overlay if you knew pointer-events worked.<\/p>\n

Or, instead of an overlay at all, you could color individual lines to make it look like a fadeout as we covered in this article<\/a>.<\/p>\n

The Post-Selection Popup<\/h3>\n

Some sites make some pretty big assumptions after you’ve selected some text. A common one is: OH GOOD YES HELLO HI I SEE YOU OBVIOUSLY MUST WANT TO SHARE THIS BIT OF TEXT ON SOCIAL MEDIA LET ME HELP. Not everyone<\/a> is a fan. Aside from being presumptuous and over-eager, it can get in the way<\/a> of screenshotting.<\/p>\n

\"\"<\/figure>\n

Highlighter.js<\/a> is a library that makes this all-too-easy.<\/p>\n

CSS Control Over Selectability<\/h3>\n

There is actually a CSS property specifically for controlling the selectability of text: user-select<\/code><\/a>.<\/p>\n

.unselectable {\n  -webkit-user-select: none;  \/* Chrome all \/ Safari all *\/\n  -moz-user-select: none;     \/* Firefox all *\/\n  -ms-user-select: none;      \/* IE 10+ *\/\n  user-select: none;          \/* Likely future *\/       \n}<\/code><\/pre>\n

Aside from a few browser quirks<\/a>, text with that set is unselectable. That can be pretty useful when trying to design better UX around the selectability of text on your site.<\/p>\n

On mobile, text selection in generally done by a long tap which brings up some text selection choices. You can prevent<\/a> that “callout” (and thus prevent the selection of text), with:<\/p>\n

-webkit-touch-callout: default   \/* displays the callout *\/\n-webkit-touch-callout: none      \/* disables the callout *\/<\/code><\/pre>\n

Other visible text that doesn’t copy<\/h3>\n

You can force text to be unselectable with the CSS above, but some text isn’t selectable automatically:<\/p>\n