Home > Web Front-end > CSS Tutorial > How Can I Prevent Text Selection on a Transparent Overlay DIV?

How Can I Prevent Text Selection on a Transparent Overlay DIV?

DDD
Release: 2024-12-09 18:39:15
Original
310 people have browsed it

How Can I Prevent Text Selection on a Transparent Overlay DIV?

Making a DIV Unselectable for Overlaying Text

In certain scenarios, a transparent DIV overlaying text, such as a watermark, can interfere with user interaction by becoming accidentally selectable. This issue occurs even when the DIV has a lower z-index, as browsers prioritize selection based on the visible content rather than its layer.

To make a DIV unselectable, there are two viable approaches:

Using jQuery:

To disable selection using jQuery, you can use the following extension:

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

Using CSS:

Alternatively, you can use CSS to achieve the same result cross-browser:

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

By applying this CSS property, you effectively disable user selection for the specified DIV, ensuring that the underlying text remains unselectable.

The above is the detailed content of How Can I Prevent Text Selection on a Transparent Overlay 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template