/    Sign up×
Bounties /Pin to ProfileBookmark

What are the ways to prevent text selection highlighting in web pages?

+ 1,000
Copy linkTweet thisAlerts:
Dec 05.2022
to post a answer
CSSJavaScript

3 Replies

Copy linkTweet thisAlerts:
@threehymnsFeb 13.2023 — The best way to do this would be to use the user-select: none property. You could also apply this to only a single element or set of elements.

* {
user-select: none;
}

Live Demo

Or, if you want to only make the selection invisible, one option is the ::selection pseudo element.

::selection {
background-color: transparent;
}

Live Demo
Copy linkTweet thisAlerts:
@cmichaudFeb 17.2023 — I use:

*{
user-select: none;
}

Its the best way I have found
Copy linkTweet thisAlerts:
@JaySODec 05.2022 — There are a few ways to prevent text selection highlighting. One way is to use the user-select property in CSS and set it to none. This will prevent users from being able to select text on the page.

* {
user-select: none;
}


Another way to prevent text selection highlighting is to use the onselectstart event in JavaScript and prevent the default behavior when this event is triggered. This will also prevent users from being able to select text on the page.

For example, you could use the following JavaScript to prevent text selection highlighting on a page:

document.onselectstart = function() {
return false;
}


Note that using these methods will prevent users from being able to select text on the page, which may not be desired in all cases. It is important to consider the user experience before using these methods to prevent text selection highlighting.
×

Success!

Help @hq spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 2.22,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Samric24,
tipped: article
amount: 1000 SATS,

tipper: Anonymous,
tipped: article
amount: 10 SATS,

tipper: @neststayhome,
tipped: article
amount: 1000 SATS,
)...