PrefaceWhen I was typing my own personal blog, I wanted to have different ways of copying different contents on the blog details page. For example, I want readers to be able to copy the code block with just one click, which makes it easier for readers to debug locally. As for the text description part, I hope to not allow readers to copy it. As a staunch extremist who believes in using CSS over JS, I eventually found user-select in CSS3. compatibilityuser-selectUsed to control whether the user can select text. Select all, Select some. Select All In many cases, users may want to copy complete content at one time, such as a piece of code, a password, or some keys. h2 { user-select: all; } code { user-select: all; width: 500px; display: block; padding: 10px; color: #31808c; background-color: #f5f4ef; } div { user-select: all; } <h2>Click to try it</h2> <pre> <code> const num = 1; const result = (function () { delete num; return num; })(); console.log(result); </code> </pre> <p> const num = 1; const result = (function () { delete num; return num; })(); console.log(result); </p> However, all also has an embarrassing shortcoming. As long as you set all, you cannot select some content. Disable selectionFor elements on a web page, you can use user-select: none; to prevent users from selecting content. Partially selectedWhy is there such a saying? For ordinary web pages, we can select specific content. For example, in the following page, we can partially select the content. But the title part here mainly refers to the elements that cannot be selected on the opposite side. For example, there is a tag called sup in HTML, which is mainly used to add superscripts to elements. <p>I have a corner mark behind me<sup>1</sup>I have a corner mark in front of me</p> When you want to copy this text: I have a corner mark 1 behind me and I have a corner mark in front of me, this corner mark will also be copied. sup { -webkit-user-select: none; user-select: none; } Extension: Set the selected styleCSS provides the ::selection pseudo-element to style text selections You can style text selections by targeting the ::selection pseudo-element. However, only the following properties can be set:
For example p::selection { color: #fffaa5; background-color: #f38630; text-shadow: 2px 2px #31808c; } The effect after selection is as follows: This concludes this article on how to use pure CSS to prevent users from copying web page content. For more information on how to use CSS to prevent users from copying content, please search 123WORDPRESS.COM’s previous articles or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future! |
First, create a tomcat folder. To facilitate the ...
1. What is pip pip is a Python package management...
I recently encountered a problem at work. The doc...
jQuery form validation example / including userna...
The first method: Use Junge's one-click scrip...
The virtual machine used is CentOS 8.4, which sim...
1. Command Introduction The stat command is used ...
Preface Today, after synchronizing the order data...
Preface After reading the previous article about ...
Table of contents Preface use Component Writing D...
There are many loopholes in the simple seamless s...
The filter attribute defines the visual effect of...
1. Understand the WEB Web pages are mainly compos...
Web front end 1 Student ID Name gender age 01 Zha...
After installing MySQL, enter mysql -u root -p in...