Valid XHTML Clickable Header Images

This is a tutorial on how to make your blog or website header image clickable with valid XHTML. This is assuming that you’re using a CSS background image for your header or that you want to.

I was recently designing a theme that I wanted to add a clickable header image to, but had forgotten the best way to accomplish this task. So, I quickly searched Google and thought I found an easy way to do this with the WordPress clickable header tutorial, but quickly realized that it wasn’t valid XHTML. Many other tutorials showed this same example as well.

Basically, they were wrapping the <a> element around the <h1> element. This is bad. Very bad. CSS can do so much. Why ruin its beauty with poorly structured HTML?

After playing around with the CSS for a while, I came up with a solution.

First, you obviously need a header image. For the sake of this tutorial, the width and height of this image will be 350px by 90px. You can change the values in the CSS to match with your header image. Add this code to your header file or wherever your header image will go.

XHTML

<div id="header">
<h1>
<a href="/index.php" title="Blog Title"><span>Blog Title</span></a>
</h1>
</div>

Now, we need to style this thing.

CSS

#header {
	float: left;
	width: 100%;
	}
#header h1 {
	float: left;
	margin: 0;
	padding: 0;
	width: 350px;
	}
#header h1 a {
	padding: 0;
	margin: 0;
	width: 350px;
	height: 90px;
	display: block;
	background: url(images/header1.gif) no-repeat top left;
	}
#header h1 a span { display: none; }

These CSS values aren’t going to be exact all the time. Some things can change depending on your layout. Your positioning, height, width, margin, and padding are all variables that may need to change. The most important things are these lines:

#header h1 a {
	width: 350px;
	height: 90px;
	display: block;
	background: url(images/header1.gif) no-repeat top left;
	}

Giving the link (which has a background image) a width, height, block display, and adding the image are the things you must do.

That should pretty much do it. If you have any problems, please let me know through the comments. Otherwise, enjoy using your valid XHTML clickable header image.

You can also use this technique to make any element clickable with a background image, not just header images.

5 Responses to “Valid XHTML Clickable Header Images”

  1. Hi Justin, this was exactly what I was originally wrapping a DIV with a LINK. Now it’s all fixed and our site is valid XHTML :)
    Thanks for posting!

    Chris

  2. No problem Chris. I’m glad somebody finally used this tutorial. I expect the masses are still wrapping the <a> element around <h1> or <div>.

  3. Awesome Justin!!

    Thank you very much - makes it nice and clean! Exactly what I’ve been looking for!

    Tony

  4. You are a legend , well done. I’ve been searching online for hours looking for a valid markup example of this and your solution is perfect. Works perfectly. Much appreciated for this post

    excellent work

  5. No problem guys.

    Brian
    I’ve searched and searched before because I’m a bit of a lazy coder sometimes. I was so surprised that there were so many invalid XHTML examples out there, and these were blog posts with 100+ comments sometimes thanking the author.

Leave a reply

Log in or Register



XHTML: You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> 
<blockquote cite=""> <cite> <code> <del datetime=""> <em> 
<q cite=""> <strong>