Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Disabling Text Selection with only CSS 1

Status
Not open for further replies.

evilm

Programmer
Jul 18, 2007
4
US
Good Afternoon,
I attempted to search for help on this topic earlier and got nowhere, hence me starting this thread.

I have developed a simple ordered list navigation, and I wanted to build a class which would disable selection of the text in the menu options so they behave more like images than plain text.

I understand there is a javascript workaround, however, in the interest of keeping the site lean, I'd like to know if there is a way to do this only using CSS.
 
Can't be done with CSS unless you put an empty element over the top of the text. That may work, but seems a bit pointless.

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
You can do it like this:

Code:
.unselectable {
   -moz-user-select: none;
   -khtml-user-select: none;
   user-select: none;
}

That works in most major browsers except IE. For IE, you'd have to use either JS:

Code:
el.unselectable = "on";

or put it as an HTML attribute:

Code:
<el unselectable="on" ...>

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Well I never knew that.

Hrmph! ;)

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
Hmm, it's kind of starting to blur the line between style and behaviour. This seems to go against the idea of separating content, style and behaviour.

Interesting.

<honk>*:O)</honk>

Tyres: Mine's a pint of the black stuff.
Mike: You can't drink a pint of Bovril.
 
Thank you very much for the help. The code worked beautifully—too bad IE doesn't support that CSS property.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top