Welcome to a quick tutorial on how to make unselectable text in HTML. So you have a not-so-top-secret website that you want to protect? Stop people from highlighting and copying the text?
There are 2 common ways to create unselectable text:
- In CSS, simply add the
user-select: none
property. - In Javascript,
document.getElementById("TARGET").onselectstart = () => false;
That covers the basics, but let us walk through a few more examples in this guide – Read on!
TLDR – QUICK SLIDES
Fullscreen Mode – Click Here
TABLE OF CONTENTS
UNSELECTABLE TEXT
All right, let us now walk through how to stop text highlighting in this section, and a few more extra measures.
1) CSS NO TEXT SELECT
<style>
.nohigh {
user-select: none;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
}
.nohigh::selection {
background: transparent;
}
</style>
<span class="nohigh">
You shall not highlight this block of text!
</span>
- As with the above introduction,
user-select: none
is all we need to stop people from highlighting a block of text. - All the
-webkit -moz -ms
prefix is just-in-case, to maintain cross-browser compatibility. - Lastly,
.nohigh::selection { background: transparent; }
is a small trick to further confuse people with a transparent highlight – Should the “main”user-select: none
fails.
2) JAVASCRIPT HIGHLIGHT STOPPER
<script>
window.addEventListener("load", () => {
var all = document.getElementsByClassName("nohigh");
for (let a of all) { a.onselectstart = () => false; }
});
</script>
<div class="nohigh">
You shall not highlight this block of text!
</div>
<div class="nohigh">
Cannot be highlighted.
</div>
This is the Javascript way to prevent text highlighting, just hit return false
when the user selects a block of text. But please take note that there is a restriction with this method – Users can still select this block of text by starting the highlighting from somewhere else…
3) TOO MUCH EFFORT, NO PROTECTION. WORTH IT?
Yes, we can combine both CSS and Javascript for the so-called “extra protection”. But it will not stop the advanced users, as there are plenty of ways to bypass that “no text select”:
- Right-click > View source code > Copy.
- Right-click > Inspect element > Copy.
- Save HTML page as > Copy.
- Print as PDF > Copy.
I mean, having a lock is better than none, but is it really worth all the trouble to stop people from copying? You decide for yourself.
DOWNLOAD & NOTES
Here is the download link to the example code, so you don’t have to copy-paste everything.
SORRY FOR THE ADS...
But someone has to pay the bills, and sponsors are paying for it. I insist on not turning Code Boxx into a "paid scripts" business, and I don't "block people with Adblock". Every little bit of support helps.
Buy Me A Coffee Code Boxx eBooks
EXAMPLE CODE DOWNLOAD
Click here for the source code on GitHub gist, just click on “download zip” or do a git clone. I have released it under the MIT license, so feel free to build on top of it or use it in your own project.
EXTRA BITS & LINKS
That’s all for this guide, and here is a small section on some extras and links that may be useful to you.
THE REAL PROTECTION – LEGAL PROTECTION
Not a sponsored message, but if the contents are really that valuable, sign up with DMCA instead – It’s free. Get them to help with the legal takedown actions (legal and admin fee are separate), so long as you can prove the contents are yours.
LINKS & REFERENCES
- User Select – MDN
- Overriding The Default Text Selection Color With CSS – CSS Tricks
- Javascript: Disable Text Select – Stack Overflow
- Disable Copy Text In CSS & Javascript – Code Boxx
TUTORIAL VIDEO
INFOGRAPHIC CHEAT SHEET

THE END
Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!