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.
The following works in Firefox interestingly enough if I remove the write line it doesn't work. Anyone have any insight why the write line is needed.
All of the correct CSS variations are:
I'm finding some level of success with the CSS described here http://www.quirksmode.org/css/selection.html:
It took care of most of the issues I was having with some ThemeRoller
ul
elements in an AIR application (WebKit engine). Still getting a small (approx. 15 x 15) patch of nothingness that gets selected, but half the page was being selected before.If it looks bad you can use CSS to change the appearance of selected sections.
For Safari,
-khtml-user-select: none
, just like Mozilla's-moz-user-select
(or, in JavaScript,target.style.KhtmlUserSelect="none";
).For Firefox you can apply the CSS declaration "-moz-user-select" to "none". Check out their documentation, user-select.
It's a "preview" of the future "user-select" as they say, so maybe Opera or WebKit-based browsers will support that. I also recall finding something for Internet Explorer, but I don't remember what :).
Anyway, unless it's a specific situation where text-selecting makes some dynamic functionality fail, you shouldn't really override what users are expecting from a webpage, and that is being able to select any text they want.