views:

282

answers:

3

Question for CSS designers. How do I add anchor behavior to any css class without "<a href.." attribute. Here is what I mean.

I'm going to have this html code:

<span class="my_link">LINK TO SOMETHING </span>

and this text should have link behavior (visited color, active color and underlining, "pointing hand pointer").

Is it possible to make this in css?

+1  A: 

The visited and active color will have to be done in Javascript. The pointer and underline can be done like this:

.my_link { cursor: pointer; text-decoration: underline; }
Isaac Waller
+3  A: 

span.my_link { text-decoration:underline; cursor:pointer; }

You could make use of :hover if you want to apply hover styles to it. Though I'm really not sure why you can't use an anchor.

meder
Thanks, that's exactly what I need.
Maksim
+1  A: 

Unless you put it in an tag, you can not get the visited, active, etc colors without javascript. You can however get the pointing hand cursor, and the ability for it to go somewhere when you click on it. To make it have the correct pointer use this:

.my_link{ cursor: pointer; }

and for the clicking, use.

$(".my_link.").click(function(){
location.href="page";
}
Chacha102
+1 Best answer, aside from the typo in `cursor`. Are you overcompensating with British spelling? ;)
Thorarin
Its more like French .. but edited :)
Chacha102