How to click on whitespace in HTML

Reading Jeff Atwood’s latest post reminded me of this:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
  <a href='http://www.colourcoding.net/'><h1>
    Can click on whitespace
  </h1></a>
  
  <h1><a href='http://www.colourcoding.net/'>
  Can't click on whitespace
  </a></h1>
</body>
</html>

This is actually a really useful technique, especially if you’re using a background image behind your title.  You can click anywhere on the first line and it will follow the link.  Is it portable?  To the best of my knowledge every browser interprets this the same way.  Is it standard?

Err… no.  Actually, the standard doesn’t even allow you to put an h1 tag within an a tag.  There are no other ways of doing this (we’ll ignore the possibility of using javascript to cloak it).  I think I lost faith with validation at that point.

Technorati Tags: ,

Published by

Julian Birch

Full time dad, does a bit of coding on the side.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s