How to Disable Text Selection in CSS Using One Rule

Ferenc Almasi β€’ 2022 September 12 β€’ πŸ“– 1 min read

Use the user-select property in CSS to disable text selection on elements.

.element {
    user-select: none;
    -webkit-user-select: none;
}
Copied to clipboard!

Take a look at the following example, where the text on the left can be selected, the text on the right however has the user-select rule applied which prevents selection altogether.

Try to select me to see you will have no problem doing so!

Try to select me, however, you won't be able to.

According to caniuse, This CSS property is widely supported in all major browsers, however, to make it work in Safari too, you need to use the -webkit- prefix as of the writing of this article.

Want to keep the selection but change the selection color? Check out the following tutorial on how to achieve it:

Change the Color of a Selected Text in CSS with One Rule
Did you find this page helpful?
πŸ“š More Webtips
Frontend Course Dashboard
Master the Art of Frontend
  • check Unlimited access to hundred of tutorials
  • check Access to exclusive interactive lessons
  • check Remove ads to learn without distractions
Become a Pro

Recommended