Home > Web Front-end > CSS Tutorial > How to disable selected text in css

How to disable selected text in css

青灯夜游
Release: 2023-01-05 16:12:30
Original
9035 people have browsed it

In CSS, you can use the user-select attribute to prohibit text selection. You only need to add the "user-select:none;" style to the text element. The user-select attribute can set or retrieve whether the user is allowed to select text. When the value is "none", it means that the text cannot be selected.

How to disable selected text in css

The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.

In CSS, you can use the user-select attribute to prohibit text selection.

The user-select attribute sets or retrieves whether the user is allowed to select text

Syntax:

user-select:none |text| all | element
Copy after login

Attribute value:

  • none: Text cannot be selected

  • text: Text can be selected

  • all: When all content is taken as a whole can be selected. If you double-click or contextually click on a child element, the selected part will be the highest ancestor element tracing back from that child element.

  • lement: Text can be selected, but the selection range is constrained by the element boundary

Code that prohibits text selection

PC side:

.not-select{
    -moz-user-select:none; /*火狐*/
    -webkit-user-select:none; /*webkit浏览器*/
    -ms-user-select:none; /*IE10*/
    -khtml-user-select:none; /*早期浏览器*/
    user-select:none;
}
Copy after login

Mobile:

.no-touch {
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    -khtml-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
}
Copy after login

Supplement: js method:

ontouchstart="return false;"
Copy after login

Add this on the dom that needs to be banned Code snippet, the support of the two methods for Android and IOS has not been tested separately. Using both at the same time can prohibit Android and IOS.

(Learning video sharing: css video tutorial)

The above is the detailed content of How to disable selected text in css. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
css
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template