HTML5 Zone is brought to you in partnership with:

Paulund is a website dedicated to writing tutorials and code snippets about Web Development, the main subjects are PHP, Wordpress, jQuery, CSS3 and HTML5. Paul is a DZone MVB and is not an employee of DZone and has posted 70 posts at DZone. You can read more from them at their website. View Full User Profile

Disable Text Highlighting With CSS

08.06.2012
| 2478 views |
  • submit to reddit

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

Published at DZone with permission of Paul Underwood, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)