How to disable text selection highlighting
January 2017 Update:According to Can I Use, Safari's alone is sufficient to achieve the desired behavior in all major browsers. ****Here are all the correct CSS variants:Run the code snippetPlease note that is in the standardization process (currently in the W3C Working Draft). It cannot guarantee working in all environments, and there may be differences in implementation across browsers. Additionally, browsers may drop support for it in the future.More information can be found in the Mozilla Developer Network documentation.The possible values for this property are , , , , , and . is also supported in most browsers.In most browsers, this can be achieved using proprietary variants of the CSS property, which was initially proposed in CSS 3 but later abandoned, and is now proposed in CSS UI Level 4:For Internet Explorer < 10 and Opera < 15, you will need to use the attribute on elements you want to be unselectable. You can set it using the HTML attribute:Unfortunately, this attribute is not inherited, meaning you must apply it to each . If this is a problem, you can use JavaScript to recursively apply it to descendants:*April 30, 2014 Update*: You may need to re-run this tree traversal whenever new elements are added to the DOM, but as per @Han's comment, this can be avoided by adding an event handler for on the target to set . Details can be found at http://jsbin.com/yagekiji/1.This still does not cover all possibilities. Although it is impossible to initiate selection within unselectable elements, in some browsers (e.g., Internet Explorer and Firefox), it is still impossible to prevent selection that starts before and ends after an unselectable element without making the entire document unselectable.Before the user-select property was available in CSS 3, browsers based on Gecko supported . WebKit and Blink-based browsers support .Of course, browsers not using the Gecko rendering engine do not support this.There is no quick and easy standard way to achieve this; using JavaScript is an option.The real question is, why do you want users to be unable to highlight and copy certain elements? I have never encountered a situation where I don't want users to highlight parts of my website. Several of my friends, after spending a lot of time reading and writing code, use the highlighting feature to remember their position on the page or to provide a marker so their eyes know where to look next.The only useful case is if you have form buttons that should not be copied and pasted if users copy and paste the website.The JavaScript solution for Internet Explorer is:If you want to disable text selection for all elements except , you can do this in CSS (note that allows overriding in child elements, which is allowed in other browsers):Disabling text selection highlighting typically refers to preventing users from selecting text on the page using a mouse or other input device. This is sometimes done to improve user interface experience or to prevent users from easily copying website content. This can be achieved in various ways, with the most common method being CSS or JavaScript.Disabling Text Selection via CSS:You can use the CSS property to control which elements' text can be selected by users. For example, to disable text selection on the entire webpage, you can add the following CSS rule to your stylesheet:If you only want to disable text selection for specific elements, you can change the selector from to the desired class, ID, or element.Disabling Text Selection via JavaScript:While the CSS method is simple and easy to use, if you need more control, such as disabling text selection after specific user actions, you can use JavaScript. Here is an example of how to do this:By using these methods, you can disable or enable text selection as needed. However, note that disabling text selection may affect user experience, and it does not completely prevent content from being copied (e.g., users may copy text by viewing the page source). Use this feature with caution.