tags:

views:

569

answers:

7

I'm trying to make a "clickable" region.

<a
  style="display: block"
  href="http://stackoverflow.com"&gt;
  StackOverflow
</a>

A is an inline element but the CSS made it a block.

If the above is valid, then the following should be valid too:

<a
  style="display: block"
  href="http://stackoverflow.com"&gt;
  <div>Some DIV that links to StackOverflow</div>
</a>

But validator.w3.org shouldn't be flagging it as invalid (which it is right now).

If it is invalid, what would be the most proper way to make a block element "clickable" and redirects to an arbitrary page. I'm aware I can use JS onclick to achieve this behaviour, but how will Google see this?

+7  A: 

The validator is correct - you can't put <div> inside <a>, no matter what you do afterwards with CSS.

The proper thing to do is what you did in your first code block - <a style="display: block;">

If you want something inside that you can do <a style="display: block;"><span style="display: block;">

Greg
+1, this is the widely accepted solution to using anchors around a lot of content that should behave blockily.
molf
+1  A: 

It doesn't follow that the one being valid implies the other has to be. There are nesting rules for HTML, and div-within-anchor doesn't fit them, which is why validator.w3.org is giving you a hard time.

If you truly must have a div, rather than text, images or <span style="display: block">s, that's clickable, then yes, you will have to use an onclick event. Google will not understand or acknowledge the existence of the link. (You may be able to cope with this by having an anchor on something that anchors can apply to, in addition to the onclick div.)

chaos
A: 

First you need to know whether you want to use strict or transitional XHTML (frameset is not useful here). Then you look into the DTD (link) and you'll see that A cannot have a DIV inside.

Lucero
As strict as possible, as always.
Adrian Godong
A: 

Why don't you use an area tag for this? It is supposed to define the clickable area in an imagemap.

jjclarkson
It may not just be an image, it should be usable for any arbitrary block element.
Adrian Godong
A: 

Google bots now follow simple javascript links, so using JS on the onClick event of your div is an option. Other search engine bots don't do that, but sooner or later they will.

More info in this article.

Nicolas
It's nice that Google supports this, but other search engines do not (as you mention). And what about people that have JS turned off? Or arcane user agents with no JS support? It's easily solved with HTML+CSS, so using onclick handlers for basic links is a terrible idea.
molf
molf: good point. not everyone is using JS (though that constitutes a very small number now).
Adrian Godong
You may find at some point that you want to use robot-type tools such as wget or a link checker. And you would find these tools useless if you have built a site using onclick handlers.
Liam
+1  A: 

Something I've done in the past with this sort of problem is invoke the click on the parent element (My example uses jQuery):

<div class="link">
  <a href="http://www.google.com" title="Google">Visit Google</a>
</div>

$(".link").click(function(){
  document.location = $(this).find("a:first").attr("href");
});

With styles you could make the entire area appear to be the link by setting the cursor, a roll-over state, etc.

Jonathan Sampson
I'm assuming that you'll need jQuery referenced for this piece of code to work.
Adrian Godong
+3  A: 

Don't confuse valid HTML with valid CSS. It is valid to use the display css property to make inline elements block. It is not valid to put block HTML elements within inline ones.

darasd