Home > Web Front-end > CSS Tutorial > How Can I Disable Text Selection in My HTML UI?

How Can I Disable Text Selection in My HTML UI?

Linda Hamilton
Release: 2024-12-09 12:14:10
Original
445 people have browsed it

How Can I Disable Text Selection in My HTML UI?

Disable Text Selection for Enhanced UI Experience

When designing HTML user interfaces, some text elements, like tab names, can look undesirable when selected. To prevent this, consider implementing one of the following methods.

CSS Solution

For most browsers, the following CSS style can make text unselectable:

*.unselectable {
   -moz-user-select: -moz-none;
   -khtml-user-select: none;
   -webkit-user-select: none;
   -ms-user-select: none;
   user-select: none;
}
Copy after login

HTML Attribute for IE and Opera

For browsers like IE (below version 10) and Opera, use the unselectable attribute:

<div>
Copy after login

JavaScript Recursion for Extensive Unselectable Areas

For cases where nesting is involved, use JavaScript to recursively set the unselectable attribute:

function makeUnselectable(node) {
    if (node.nodeType == 1) {
        node.setAttribute("unselectable", "on");
    }
    var child = node.firstChild;
    while (child) {
        makeUnselectable(child);
        child = child.nextSibling;
    }
}

makeUnselectable(document.getElementById("foo"));
Copy after login

The above is the detailed content of How Can I Disable Text Selection in My HTML UI?. 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