CSS Trick: Turning a background image into a clickable link

One of the things I most often get asked by people trying to master HTML and CSS is “How do I make a background image clickable?” It’s easy to wrap a regular image tag in a link, but what if your design or situation required that you use a background image? It’s actually quite simple. Just follow these steps and I’ll show you how to make a clickable background image like this:

Xavisys Website Development

Start with just a link exactly as you would make it for any other purpose, and make sure to give the link an id so that we can use that to apply our styles:

<a href="http://xavisys.com" title="Professional WordPress Development" id="xavisys-logo">Xavisys Website Development</a>

That’s all the (X)HTML you’ll need to make your background image clickable. Your link should look something like this:

So, how can we make a background image a clickable link? It turns out it can be done with a clever CSS trick. Let’s get started by adding the background image and make the link the same size as the image (so you can see the whole image). Since an anchor tag isn’t a block level element, we need to force it to display as “block” so that we can specify the size:

#xavisys-logo {
	background-image:url(/wp-content/uploads/2009/11/email_logo.gif);
	display:block;
	height:58px;
	width:200px;
}

At this point it should look something like this: Xavisys Website Development

Now all we need to do is hide the text. This can be done using “text-indent” and indenting the text completely off the screen like this:

#xavisys-logo {
	background-image:url(/wp-content/uploads/2009/11/email_logo.gif);
	display:block;
	height:58px;
	text-indent:-9999px;
	width:200px;
}

And the finished product looks like this: Xavisys Website Development

And there you have it – a quick CSS trick with clean markup that turns your background images into clickable links. The best thing is, these don’t adversely affect your SEO and can even be easily used inside of header tags if needed!

Related Posts:

About AaronCampbell
Aaron is the man behind the curtain here at Xavisys. He gets things done and really knows his way around the code.

Comments

36 Responses to “CSS Trick: Turning a background image into a clickable link”
  1. John says:

    I like it! I hadn’t realised you could use this for h1 tags. Many thanks.

  2. Nathan says:

    why not delete the text between the attribute tags instead of using text-indent?

  3. BC says:

    I’m trying to use your trick with a logo on the left and some text in a paragraph tag on the right side of my header but my paragraph is being pushed down by the block containing the link. How can I get the paragraph to appear on the same line as the bg image?

    • You’ll need to set it to display:block as well as float:left. Here’s some code:

      <a style="background-image: url(/wp-content/uploads/2009/11/email_logo.gif); display: block; height: 58px; text-indent: -9999px; width: 200px; float:left; margin:0 1em 1em 0;" title="Professional WordPress Development" href="http://xavisys.com" rel="nofollow">Xavisys Website Development</a><p>Here is some test text. ... Here is some test text.</p>

      Xavisys Website Development

      Here is some test text. Here is some test text. Here is some test text. Here is some test text. Here is some test text. Here is some test text. Here is some test text. Here is some test text. Here is some test text. Here is some test text. Here is some test text. Here is some test text. Here is some test text. Here is some test text. Here is some test text. Here is some test text. Here is some test text. Here is some test text. Here is some test text. Here is some test text.

  4. portis says:

    it seems you need to write the html too, only the css doesn’t work – i mean this is a trick, and not css code if i got it right?
    the issue is, i know up to now only how to add css in my blog, but not how to meddle into the html layers yet. so, no html, no trick?
    point is, at some special few places like free-text widgets you can add an image&href by html, some others you can’t. there, i just put my image as bdg no-repeat via css, but so i lose the possibility to link with href..

  5. FRV says:

    That’s really clever.

    10 points and thanks for solving my problem.

  6. Rokgoo says:

    Aaron thaks. I used your solution. Take care.

  7. Turquoiz says:

    This is what I do. I hope it works for others.

    html

    Xavisys Website Development

    css

    #xavisys-logo {
    background:url(/wp-content/uploads/2009/11/email_logo.gif);
    display:block;
    height:58px;
    width:200px;}

    #xavisys span {display:none;}

  8. Turquoiz says:

    sorry, don’t know if this will post, but

    html: a link, title, div with id “xavisys-logo”, span, text, close span, close div, close a link.

    html

     <a href="http://xavisys.com" title="Professional WordPress Development" <div id="xavisys-logo"><span>Xavisys Website Development</span></div></a>
  9. steven says:

    Hi

    I notice that when you click it in FF you don’t get any dotted borders to appear on the final one. I am following your steps, but due to the text indent FF is showing me in my website a dotted border around my link that extend far left. How did you remove this dotted border?

    Thanks!

  10. Bob says:

    I’m using wordpress and I’m very confused about where to put the HTML.
    Do I put the HTML code in index.php or in header.php?

    You explain all the other steps really well but you left me very confused on the html part.
    I’ve looked up other tutorials that describe how to do this but they all seem to assume that you know what file to edit for the html.
    So I’m probably missing something really obvious but I would really appreciate a explanation on where to put the html code.

    Thank you!

    • Different themes will have this in different places. Assuming you want to use this trick in the header of your site, a good place to start looking is header.php but there’s no guarantee. However, this particular trick can be used anywhere you want a clickable background image, which is why I don’t say where to use it.

  11. jamie says:

    Fantastic trick! It worked like a charm. Thanks for posting this bit of info, Aaron.

    Jamie
    fiftyfootpixel.com

  12. Erik says:

    The way I understand it, that’s gonna get you banned from search engines from hiding the text on an image.

  13. SpokeBuzz says:

    Thanks for the clever tip, solved a problem I was having!

  14. Liam says:

    Hi, thanks for the great tips, I’m really close to getting this trick but I’m missing the last step. I have the text link where I want it but I can’t seem to create the block I need to cover the entire image and I can’t seem to make the text disappear. Here is what I’ve input.

    Get An Online Tutoring Session From A Masters Or Phd Student For 3.95

    /*—:[ Trial Button Css Trick, input by Liam Martin to make a background image clickable]:—*/

    #trial_button { background-image: url(images/special_icon.png); display: block; height: 166px; text-indent: -9999px; width: 166px; }

    I’m also rocking thesis 1.6 and have a customized skin. There must be something stupid I’m doing, if you can point it out to me I’d be plenty thankful

  15. Aditya says:

    thank you, this was very useful :)

  16. Hi

    your trick helped me a lot.

    but i am stuk at one point

    I have 16X16 px favicons to be ligned up and i am using a box of height 25X25. but i want the link to remain on 16X 16 only.

    • If you change the width and height to something other than the image size, you should also add the following CSS rules:

      background-repeat:no-repeat;
      background-position:center center;

      The background-position can be used to place the image in a different place within the box, and the background-repeat is used to tell it not to tile the image.

    • The method itself validates. However, there are definitely error on this page in the comment section. Nothing I’ll take the time to fix, but you’re right…this page doesn’t validate.

  17. Al Elliott says:

    Wow! Great work! You solved a bit of an issue for me, and it’s so simple. You are a genuis. Thanks!

    Al

  18. Jonny Wright says:

    I have just been trying this and stubled along here looking for an explanation to my problem.

    I am trying to create a similar effect only with multiple images in an unordered list. Can anybody help?

  19. Rwany says:

    Aaron,
    Thanks for the tip, it worked well and my preview in Dreamweaver shows up great. But, (and I’m a novice) I placed the logo/link image in my header, which has a blue background.
    When I update the site, the logo disappears. is the color conflicting with the image? Can’t I have both?
    I’m designing a site for a class project (http://rwany.sibaja.net)

  20. Vladislav says:

    All is working without any tricks!

    HTML:


    CSS
    #xavisys-logo {
    background-image:url(/wp-content/uploads/2009/11/email_logo.gif);
    display:block;
    height:58px;
    width:200px;
    }
    #xavisys-logo a {
    display:block;
    height:58px;
    width:200px;
    }

  21. Vladislav says:

    HTML:



    CSS
    #xavisys-logo {
    background-image:url(/wp-content/uploads/2009/11/email_logo.gif);
    display:block;
    height:58px;
    width:200px;
    }
    #xavisys-logo a {
    display:block;
    height:58px;
    width:200px;
    }

  22. Great trick, just used it and working perfectly many thanks!

  23. Nicole says:

    I have tried following your trick and some of the tips in the comments, and most of it works nicely.
    But I cant seem to place the logo in the middle of the header and make the outline around the link disappear.
    Any ideas what I’ve done wrong?

    #grillhytte_logo {
    background-image: url(grillhytte_logo.png);
    background-repeat: no-repeat;
    background-position: center center;
    display:block;
    height:130px;
    width:500px;
    text-indent:-9999px;
    }

    #grillhytte_logo a{
    display:block;
    height:130px;
    width:500px;
    outline: none
    }

  24. Vladislav says:

    It is not necessary to create any text-ident and hide text and any other tricks. All you have to write block:display in a {} section

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!