This question already has an answer here:
- How to disable text selection highlighting? 40 answers
I'm building an HTML UI with some text elements, such as tab names, which look bad when selected. Unfortunately, it's very easy for a user to double-click a tab name, which selects it by default in many browsers.
I might be able to solve this with a JavaScript trick (I'd like to see those answers, too) -- but I'm really hoping there's something in CSS/HTML directly that works across all browsers.
Any JavaScript or CSS method is easily circumvented with Firebug (like Flickr's case).
You can use the
::selection
pseudo-element in CSS to alter the highlight color.If the tabs are links and the dotted rectangle in active state is of concern, you can remove that too (consider usability of course).
In most browsers, this can be achieved using CSS:
For IE < 10 and Opera, you will need to use the
unselectable
attribute of the element you wish to be unselectable. You can set this using an attribute in HTML:Sadly this property isn't inherited, meaning you have to put an attribute in the start tag of every element inside the
<div>
. If this is a problem, you could instead use JavaScript to do this recursively for an element's descendants:Try this:
Simple, but effective... works in current versions of all major browsers.
For an example of why it might be desirable to suppress selection, see SIMILE TImeline, which uses drag-and-drop to explore the timeline, during which accidental vertical mouse movement causes the labels to be highlighted unexpectedly, which looks weird.
Here's a Sass mixin (scss) for those interested. Compass/CSS 3 doesn't seem to have a user-select mixin.
Though Compass would do it in a more robust way, i.e. only add support for vendors you've chosen.
EDIT
Code apparently comes from http://www.dynamicdrive.com