views:

13085

answers:

15

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.

A: 

Your best bet might be to force a Javascript deselect whenever text is selected. The only other way I can think of (in my extremely limited web-dev experience) would be to render each character individually as an image on the server-side. This would likely feel a little strange to the client though due to differences in fonts.

Daniel Spiewak
A: 

You can use images of the text. Have you played with the mouseover event?

Allan Wind
+7  A: 

Absolutely position divs over the text area with a z-index higher and give these divs a transparent gif background graphic.

Note after a bit more thought - You'd need to have these 'covers' be linked so clicking on them would take you to where the tab was supposed to, which means you could/should do this with the anchor element set to display:box, width and height set as well as the transparent background image.

davebug
That's a very simple and elegant hack. Great!
qwertyuu
This is what flickr does
Harry
This kind of hack is terrible. I'd avoid it personally.
Doug
+1  A: 

images can be selected too. there are limits to using javascript to deselect text, as it might happen even in places where you want to select. To ensure a rich and successful career, steer clear of all requirements that need ability to influence or manage the browser beyond the ordinary... unless, of course, they are paying you extremely well.

kinjal
+7  A: 

For firefox you can apply the CSS declaration "-moz-user-select" to "none" Check out their docs: http://developer.mozilla.org/En/CSS/-moz-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 IE, but 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.

Jorge Alves
Likewise in Safari/Chrome/etc. -khtml-user-select:none;
Brandon DuRette
Is this likely to appear in the CSS standards though?
Harry
+11  A: 
<script type="text/javascript">

/***********************************************
* Disable Text Selection script- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code

***********************************************/


function disableSelection(target){

    if (typeof target.onselectstart!="undefined") //IE route
        target.onselectstart=function(){return false}

    else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
        target.style.MozUserSelect="none"

    else //All other route (ie: Opera)
        target.onmousedown=function(){return false}

    target.style.cursor = "default"
}



//Sample usages
//disableSelection(document.body) //Disable text selection on entire body
//disableSelection(document.getElementById("mydiv")) //Disable text selection on element with id="mydiv"


</script>

EDIT

Code apparently comes from http://www.dynamicdrive.com

dimarzionist
Is there any update for the Opera or making elements unselectable still not supported?
jayarjo
Why are you changing the cusor style to 'default' in the last case? Otherwise +1
HRJ
Infact, you are changing it in all cases (indent is misleading)
HRJ
+2  A: 

A good rule of thumb: Don't put text on the internet that you don't want copied.

Edit: Didn't read the problem correctly.

dawnerd
LOL you might be right - certainly future readers might be more interested in text protection
Harry
If your content is really interesting, then there is little you can ultimately do to protect it
Harry
+7  A: 

Try this:

<div onselectstart="return false">some stuff</div>

Simple, but effective... works in current versions of all major browsers.

Stephen M. Redd
This doesn't work for me in firefox.
Tyler
This is IE only attribute. which makes it redundant.
vsync
Worked perfectly for our app (we only target IE users).
Lee Fogel
A: 

If it looks bad you can use CSS to change the appearance of selected sections.

Wedge
A: 

While many of the examples listed here work, keep in mind nothing prevents someone from just looking at the source code and copying the text.

Ren
+2  A: 

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.

pdc
A: 

I'm finding some level of success with the CSS described here http://www.quirksmode.org/css/selection.html:

::selection {
    background-color: transparent;
}

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.

jlleblanc
+3  A: 

For Safari, "-khtml-user-select: none", just like Mozilla's "-moz-user-select" (or, in JavaScript, target.style.KhtmlUserSelect="none";).

Alan Hensel
A: 

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.

document.write("."); document.body.style.MozUserSelect='none';
hbtdev
A: 

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).

tedmiston