tags:

views:

87

answers:

5
<html>
<head>
</head>
<body>
<div>
<a href="http://www.google.com"&gt;
  <span>title<br></span>
  <span>description<br></span>
  <span>some url</span>
</a>
</div>
</body>
</html>

I am pretty new to CSS, I have a simple case like the above. I would like to make the "title" and "some url" clickable but want to make description as non-clickable. Is there any way to do that by applying some CSS on the span so that whatever inside that span, it is not clickable. My constraint is that, I do not want to change the structure of the div, instead just applying css can we make a span which is inside an anchor tag, not clickable ?

+1  A: 

Not with CSS. You could do it with JavaScript easily, though, by canceling the default event handling for those elements. In jQuery:

$('a span:nth-child(2)').click(function(event) { event.preventDefault(); });
ceejayoz
Huh.Why every time somebody ask question related to html ,javascript etc ie client side... solution is Jquery. Question is not tagged with javascript nor with Jquery..I know you cant do this with css only but Jquery is not the solution.
piemesons
jQuery is being used here as an /example/ only. That said, in a large percentage of practical situations jQuery is a very, very good choice. The example here is very well suited to jQuery.
Jon Cram
This solution could be good, but like that you prevent the click on all span inside a tag... and title span needs to be clear for click event!
Zuul
@Zuul As @Jon Cram stated, this is example code. He'd need to `class` the description `span` and target it specifically.
ceejayoz
@piemesons Those of us who work in jQuery tend to give examples in jQuery because it's a lot easier than the absurdities non-frameworked JS requires you to go through.
ceejayoz
@ceejayoz, Yes... correct, but a working example would help Shamik better ;)
Zuul
@Zuul Not if he wants to learn instead of copy/pasting. :-)
ceejayoz
@ceejayoz, ok, ok...ok! ;)
Zuul
@piemesons I provided a vanilla example.
NateDSaint
@all Jquery fans.. I am not against of Jquery.@ceejayoz But you have to pay a cost for that ie 70kb for minified version of Jquery thats way you cant go for the Jquery solution everytime. You are thinking from developer's point of view.. Try to think from user point of view.
piemesons
70kb once, if you set your server up right, and potentially not even once if you use Google's hosted version.
ceejayoz
Also, it's 24KB minified and gzipped, according to jQuery.com.
ceejayoz
+3  A: 

CSS is used for applying styling i.e. the visual aspects of an interface.

That clicking an anchor element causes an action to be performed is a behavioural aspect of an interface, not a stylistic aspect.

You cannot achieve what you want using only CSS.

JavaScript is used for applying behaviours to an interface. You can use JavaScript to modify the behaviour of a link.

Jon Cram
Thank you for the detailed information. Now I will never forget where to use CSS and where to look for javascript :)
Shamik
A: 

CSS relates to visual styling and not behaviour, so the answer is no really.

You could however either use javascript to modify the behaviour or change the styling of the span in question so that it doesn't have the pointy finger, underline, etc. Styling it like that will still leave it clickable.

Even better, change your markup so that it reflects what you want it to do.

FinnNk
+4  A: 

Using CSS you cannot, CSS will only change the appearance of the span. However you can do it without changing the structure of the div by adding an onclick handler to the span:

<html>
<head>
</head>
<body>
<div>
<a href="http://www.google.com"&gt;
  <span>title<br></span>
  <span onclick='return false;'>description<br></span>
  <span>some url</span>
</a>
</div>
</body>
</html>

You can then style it so that it looks un-clickable too:

<html>
<head>
     <style type='text/css'>
     a span.unclickable  { text-decoration: none; }
     a span.unclickable:hover { cursor: default; }
     </style>
</head>
<body>
<div>
<a href="http://www.google.com"&gt;
  <span>title<br></span>
  <span class='unclickable' onclick='return false;'>description<br></span>
  <span>some url</span>
</a>
</div>
</body>
</html>
Elf King
Thanks for this feedback. On hover, the cursor is not changing to default *it stays as hand*, any idea how to fix that?
Shamik
http://www.echoecho.com/csscursors.htm :-)
Elf King
+1  A: 

In response to piemesons rant against jQuery, a Vanilla JavaScript(TM) solution (tested on FF and IE):

Put this in a script tag after your markup is loaded (right before the close of the body tag) and you'll get a similar effect to the jQuery example.

a = document.getElementsByTagName('a');
for (var i = 0; i < a.length;i++) {
  a[i].getElementsByTagName('span')[1].onclick = function() { return false;};
}

This will disable the click on every 2nd span inside of an a tag. You could also check the innerHTML of each span for "description", or set an attribute or class and check that.

NateDSaint
+1 for this solution.Love u..:-)
piemesons