tags:

views:

9227

answers:

5

For anchors that act like buttons (for example the Question, Tags, User, etc at the top of the SO page) or tabs, is there a CSS standard way to disable the highlighting effect if the user accidentally selects the text?

I realize this could be done with javascript and a little googling yielded the mozilla-only "-moz-user-select" option.

My question is if there is a standard compliant way to accomplish this with CSS, and if not, what is the "best practice" approach?

Thanks!

+5  A: 

Until CSS 3's user-select property becomes available only Gecko based browsers will support the -moz-user-select property you already found.

This off course is not supported in browsers that do not use the Gecko rendering engine.

There is no "standards" compliant quick and easy way to do it, using JavaScript is an option.

The real question is, why do you want users to not be able to highlight and presumably copy and paste certain elements? I have not come across a single time that I wanted to not let users high-light a certain portion of my website. Several of my friends, after spending many hours reading and writing code will use the high-light feature as a way to remember where on the page they were, or providing a marker so that their eyes know where to look next.

The only place I could see this being useful is if you have buttons for forms that should not be copy and pasted if a user copy and pasted the website.

X-Istence
The buttons thing would be exactly my motivation.
Kriem
This may be necessary for embedded devices. i.e. a device where a browser is used for rendering the UI.
Tim Kersten
Being able to [ctrl-a], [ctrl-c] only the text content portion of a site would be useful imo.
codeinthehole
Another reason this is needed is Shift-clicking to select multiple rows in a grid or table. You don't want to to highlight the text, you want it to select the rows.
Gordon Tucker
Kyle Fox
+2  A: 

Aside from the Mozilla-only property, no, there is no way to disable text selection with just standard CSS (as of now).

If you notice, Stack Overflow doesn't disable text selection for their navigation buttons, and I would recommend against doing so in most cases, since it modifies normal selection behavior and makes it conflict with a user's expectations.

htw
While I agree that it changes behaviour the user expects, it would make sense for things like the "Add Comment" button that is sitting next to this form field ...
X-Istence
But doesn't that expose needless implementation details? An input or button's text can't be selected.
@anon: Most users will probably not try to select the text of your button, so in practice, it shouldn't really matter much. Besides, in order to do so, they will have to start selecting *outside* of the button—if they click inside the button itself, the onclick handler will activate instead. Plus, certain browsers (e.g. Safari) actually let you select the text of normal buttons…
htw
+7  A: 

You can do so in Firefox and Safari (Chrome also?)

::selection { background: transparent; }
::-moz-selection { background: transparent; }
seanmonstar
This combined with the `onselectstart = function(){ return false; }` for IE gives me exactly what I wanted. Thanks.
Gordon Tucker
+13  A: 

This will disable user selection in Firefox and WebKit

-webkit-user-select:none;
-moz-user-select:none;
Sam Soffes
This works in Chrome and Safari by the way.
Sam Soffes
@Sam Soffes - that's because WebKit is the rendering engine used by Chrome and Safari.
Matt Ball
I added "cursor:default;" otherwise the cursor changes to the select cursor (at least in FF)
some
@Matt Ball +1 captain obvious point :)
Sam Soffes
@Sam Soffes: does that point go to me or to you? :P
Matt Ball
+5  A: 

A Javascript solution for IE is

onselectstart="return false;"
Pekka
Don’t forget about `ondragstart`!
Mathias Bynens