Open In App

How to Disable Text Selection Highlighting using CSS?

Last Updated : 17 Nov, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The user-select property in CSS is used to disable text selection highlighting in CSS. This feature is useful when we need to disable the copy option of content.

Syntax

user-select: none;
-webkit-user-select: none; /* Chrome, Opera */
-webkit-touch-callout: none; /* Safari */
-moz-user-select: none; /* Firefox */
-ms-user-select: none; /* Internet Explorer/Edge */

Disable Text Selection Highlighting using CSS

To disable the text selection, we use user-select property. This property controls whether users can select text or not. It prevents the user from selecting content.

Note: The user-select property is widely supported, older versions of some browsers may require vendor prefixes (e.g., -webkit-, -moz-) for compatibility.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <style>
        .disable-text {
            user-select: none;
            -webkit-user-select: none;
            -webkit-touch-callout: none;
            -moz-user-select: none;
            -ms-user-select: none;
        }
    </style>
</head>

<body>
    <h3>Disabled Text Selection</h3>
    
    <div class="disable-text">
        <h4>Hello World</h4>
        <p>This text cannot be selected by the user.</p>
    </div>
</body>

</html>

Output

Disabled-Text-Selection

When/Why to use Disable Text selection?

Ideally, it is not recommended to disable text selection on your webpage, as this impacts user accessibility. These are some situations where you might want to disable text selection:

  • When you intentionally want user to copy/select given information. For example, price, terms & conditions etc.
  • In case of interactive elements, like button, dropdowns, or drag-drop related elements (web-tools, games etc). For these cases, accidental text selection can interfere with functionality.
  • In case of Widgets where disabling text selection is intended. For example, Ratings, slider labels, or form descriptions etc.

Next Article
Article Tags :

Similar Reads

three90RightbarBannerImg
  翻译: