Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Disable Text Highlighting With CSS

DZone's Guide to

Disable Text Highlighting With CSS

Learn how to disable test highlighting using CSS.

· Web Dev Zone ·
Free Resource

Learn how error monitoring with Sentry closes the gap between the product team and your customers. With Sentry, you can focus on what you do best: building and scaling software that makes your users’ lives better.

In a previous article I wrote about how you can change the browser selection colour when you highlight text in your browser.

All you need to do is use a CSS selector class of ::selection and define the style to use on your text when a visitors tries to highlight the test.

But what if you want to do the opposite and want to disable your visitor from highlighting the text all together?

There is an experimental property called user select which will allow you to define new instructions when the visitor tries to highlight your content. This feature can be a good way of making it harder for people to highlight your content and copy it into their own website.

All you have to do is use this CSS property.

.disable_text_highlighting {
-webkit-user-select: none; /* Webkit */
-moz-user-select: none;    /* Firefox */
-ms-user-select: none;     /* IE 10  */
/* Currently not supported in Opera but will be soon */
-o-user-select: none;
user-select: none;
}

The values on this property are:

  • Auto - Visitors can select all content in the element.
  • None - Selecting content is disabled.
  • Text - Can only select text content.

Browser Support

This is currently support on webkit, firefox and IE 10. To use the property you need to prefix it with the different browser prefixes but you should also place the non-prefixed version to future proof the property.

View the demo to see what it can do.

Demo

What’s the best way to boost the efficiency of your product team and ship with confidence? Check out this ebook to learn how Sentry's real-time error monitoring helps developers stay in their workflow to fix bugs before the user even knows there’s a problem.

Topics:
css ,html & xhtml ,web dev

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}