tags:

views:

418

answers:

5

I'm using CSS to create a header graphic:

#header {
    height:125px;
    background:url(/Content/images/header_footer.jpg) -0 0 no-repeat;
}

then:

<div id="header">
    <!-- navigation START -->
    <ul id="main_navigation">

Is there a way to make the graphic (space above the nav UL) into a clickable link?

thx

+3  A: 

Certainly: add a link tag. CSS is great at adding graphics and visual elements to pages, but if you want the page to do anything (e.g., to link somewhere) that has to be expressed somewhere in the HTML.

A common solution to what you're trying to do is to add an empty <a> tag, styled with a width and height that match the graphic you're using.

VoteyDisciple
In general, when you want a clickable image, dont use backgrounds, as they dont show the hand cursor the way they should. Use img tag.
pokrate
I disagree with @pokrate. that's why we have the "cursor: pointer;" property in CSS. Use it.
Eric Meyer
A: 

Erm, wouldn't this do it (or have I misunderstood?):

<a href="yourpage.html"><div id="header"></a>

You'll probably also want to add border:none to your #header

Dan Diplo
A: 

I don't think this is a good approach. If you want only the graphic to be a link put in a separate element:

#header {
    height:125px;

}

#headerImg
{
    display:block;
    height:100px;
    background:url(/Content/images/header_footer.jpg) -0 0 no-repeat;
}
<div id="header">
    <a href="http://www.stackoverflow.com"&gt;&lt;span id="headerImg"></span></a>
    <ul id="main_navigation">
Gergely Orosz
As this looks to be the most compliant approach, i'm concentrating on implementing it. However i'm having problems with spacing/formatting. What's the reasoning behind including teh 100px height in #headerImg?
justSteve
That was just random, as I assumed that the image was 100px high. However it is complicated indeed, you probably don't need the span, you could just go with assigning an ID to the <a> tag.
Gergely Orosz
A: 
<a href="/whereever.php">
    <div id="header">
        <!-- navigation START -->
        <ul id="main_navigation">

Is that what you wanted?

But I agree that the above answer is a better method

Dorjan
Except you can't put a `<div>` or a `<ul>` inside an `<a>`.
VoteyDisciple
Yes you can. At least you can for 100% certaintly put a Div inside. Not sure about a ul
Dorjan
While you can, certainly, 'put a `<div>` or `<ul>` inside an `<a>`' I don't believe that it's valid html or xhtml to put a block-level element (`<div>` or `<ul>`) within an inline-element (`<a>`). You could use a `<span>` in place of a `<div>`, though.
David Thomas
Fair point ricebowl.
Dorjan
+1  A: 

The above answers are correct in that you need an anchor tag in your HTML, but how that plays out depends entirely on what the image is that you are linking.

I don't see any reason to ever have an empty anchor tag. That's meaningless. Most likely you are either linking a logo or wordmark or site title or some combination. That should go in your HTML code, even if you plan to replace it with an image.

The first consideration is whether your header image itself is content or design. In the case of logos, it sometimes is content. In the case of site titles or wordmarks I would more often say the image is simply design, and the text is content.

For an image that is content in it's own right:

<div id="header">
  <a href="/"><img src="logo.png" alt="My company"></a>
  <!-- navigation START -->
  <ul id="main_navigation">

For an image that is replacing content:

<div id="header">
  <h1><a href="/">My Company</a></h1>
  <!-- navigation START -->
  <ul id="main_navigation">

and style:

#header h1 a { 
  display: block; 
  text-indent: -999em; 
  height: ??px;
  background-image: url(path/to/image.png);
}

In either case, you have given semantic meaning to the area used as a link. I can't quite imagine a situation where you would want a meaningless link.

Eric Meyer