CSS Trick: Turning a background image into a clickable link

While working on a design for a client’s site the other day, I had the need to make a background image a clickable link. Their logo was being used as a background image in the site’s header, and we wanted it to act as a link to the home page - like any good website should.

It’s simple to turn a regular image into a clickable link, but in some situations, it’s just not possible to use a regular image - so how do you turn a background image into a clickable link? First let’s take a look at my original markup (I’ve removed unnecessary elements for sake of simplicity):

<div id="header"></div>

#header {
background: #fff url(images/header.png) no-repeat;
height: 101px;
width: 800px;
}

So, how can we make our background image a clickable link? It turns out it can be done with a clever CSS trick. All we have to do is add the link markup between our header div tags, and apply the above markup to the link instead. All we have to add to the CSS is display: block to force the link to fill the entire space.

To handle browsers that don’t support CSS (those things are still around?), we should also add a span containing link text that we will hide with our CSS. Here’s what we end up with:

<div id="header"><a href="http://mysite.com" ><span>MySite.com</span></a></div>

#header a {
background: #fff url(images/header.png) no-repeat;
display: block;
height: 101px;
width: 800px;
}
#header a span {
visibility: hidden;
}

And there you have it - a quick CSS trick that turns your background images into clickable links.

This article was originally posted on my personal blog - brandoncwood.com.

Related Posts

  • No Related Post

20 Responses to “CSS Trick: Turning a background image into a clickable link”

  1. DENiAL Says:

    Handy tidbit of information, thanks for sharing it.

    Current score: 0
  2. Thom Says:

    Nice! Exactly what I needed, since the way I was doing it before was to use a block element inside the a tag… not sure what I was thinking. But this works great!

    Current score: 0
  3. Ria Says:

    It doesn’t work for me! I’ve done it exactly and it does not work. UGH!

    Current score: 0
  4. Ria Says:

    AHA. Positioning values cannot mimick the parent div, but must be set to left, top: 0px to match up. My hyperlinked div was sitting off somewhere else on the page hidden behind other content. May want to mention that positioning is very important!

    Current score: 0
  5. sirjonathan Says:

    I was tackling this and temporarily gave up just the other morning.. a huge thanks for sharing this tip :).

    Current score: 0
  6. sirjonathan Says:

    Using the was acting up a bit on Firefox.. An alternative is to use an and then, instead of setting visibility, set a text-indent with a large negative margin instead.

    Current score: 0
  7. RegGFX Says:

    Hey… THIS WORKED OUT GREAT…
    However.. one problem..

    How would you Suggest applying an alt image tag to a clickable background image?

    Is that even possible?
    Your thoughts?

    Thanks

    Current score: 0
  8. Gary Says:

    Nice tip - thanks for sharing it.

    Current score: 0
  9. Anthony Says:

    You can add an image alt attribute with this technique - which I use regularly:

    Where blank.gif is a 1px by 1px transparent gif.

    Here’s one for you:
    http://www.media3000.co.uk/justmedia.ie/catalog/images/pixel_trans.gif

    Current score: 0
  10. Daniel Says:

    Hi, does this technique not work if you are using absolute positioning? I have it working on a div that is positioned relative, but it won’t work for some reason on a div that is positioned as an absolute.

    Current score: 0
  11. Karel Says:

    Right on! Works like a charm :-)

    Current score: 0
  12. Nora Says:

    Thanks for sharing this! I’ve been trying to find a solution for ages :D

    Current score: 0
  13. Kimberly Says:

    Can’t believe I landed on this! This issue has been a nagging thorn in my side for months — thank you!!

    Current score: 0
  14. gingerfire Says:

    I searched and searched for how to do this. Thanks!

    Current score: 0
  15. Matt Danner Says:

    Thanks for the help.

    Current score: 0
  16. Mariusz Says:

    This is awesome hint mate! That’s exactly what I wanted !

    Cheers !!!

    Current score: 0
  17. SD Says:

    Thank you. Very useful tip. It works!! :)

    Current score: 0
  18. Dan Stephenson Says:

    Thanks for this tip.

    This could also be done using an image map.

    Current score: 0
  19. Mike Says:

    I tried and it worked……Thanks for a wonderful trick…..

    @DanStephenson could you please tell us how to do this using image maps?

    Current score: 0
  20. Nick Says:

    Perfect! Just what we needed for our myspace band page! Thank you!

    Current score: 0

Leave a Reply