How Can I Disable Text Selection in My HTML UI?
Dec 09, 2024 pm 12:14 PMDisable 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; }
HTML Attribute for IE and Opera
For browsers like IE (below version 10) and Opera, use the unselectable attribute:
<div>
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"));
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!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Adding Box Shadows to WordPress Blocks and Elements

Create a JavaScript Contact Form With the Smart Forms Framework

Making Your First Custom Svelte Transition

Demystifying Screen Readers: Accessible Forms & Best Practices

Comparing the 5 Best PHP Form Builders (And 3 Free Scripts)
