Home > Web Front-end > CSS Tutorial > How to Prevent Text Selection within an Overlapping, Transparent DIV?

How to Prevent Text Selection within an Overlapping, Transparent DIV?

Mary-Kate Olsen
Release: 2024-11-28 02:31:11
Original
400 people have browsed it

How to Prevent Text Selection within an Overlapping, Transparent DIV?

Controlling Element Selectivity: Making a DIV Unselectable

Issue Description

When a transparent DIV overlays text within a textarea as a watermark, the watermark may inadvertently become selectable when the user clicks on the textarea. The goal is to prevent the watermark text from being selectable, despite its lower position in the z-index.

Resolution

jQuery Extension:

Embed the jQuery disableSelection extension into your code:

$('.button').disableSelection();
Copy after login

CSS Alternative (Cross-Browser):

Apply the following CSS properties to the target DIV:

.button {
    user-select: none;
    -moz-user-select: none;
    -khtml-user-select: none;
    -webkit-user-select: none;
    -o-user-select: none;
}
Copy after login

These properties disable user selection for the specified DIV element across various browsers.

The above is the detailed content of How to Prevent Text Selection within an Overlapping, Transparent DIV?. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template