1

I would like to create a link out of the following so that the image is clickable.

What would you do to get the background-image clickable?

HTML: (Displays an Image which I would like clickable)

<div class="some_image">
</div>

CSS:

.some_image{
    width: 200px;
    height:100px; 
    background-image: url(../images/image.png);
}
Spencer H
  • 653
  • 3
  • 12
  • 30
gpwr
  • 988
  • 1
  • 10
  • 21
  • I would like it compatible with older browsers. So this might not work for for the moment? – gpwr Aug 20 '15 at 14:20
  • If you want just the image area clickable, you must wrap the image with a link tag, or, if entire div can be clickable, you can add a click event to the div. – Ricardo Pontual Aug 20 '15 at 14:22

2 Answers2

1

You can't make a background image clickable with HTML alone. It's a property of an element and not an element itself. Simply wrap the div in an anchor. This is permissible in HTML5 spec.

More info

Of course, you could simply style the anchor:

.some_image {
    display: block;
    width: 200px;
    height:100px; 
    background-image: url(../images/image.png);
}

<a class="some_image" href=""></a>
Community
  • 1
  • 1
isherwood
  • 58,414
  • 16
  • 114
  • 157
0

You can't do this traditionally however you may achieve the effect with hacky techniques:

#heatSpot { 
    position: absolute;
    left: 20px; // over area of bg image
    top: 10px; // over area of bg image
    cursor: pointer;
}

then just fire the link or whatever your trying to do onClick with either wrapping above element in <a> tag -- or using jQuery / JavaScript.

Dr Upvote
  • 8,023
  • 24
  • 91
  • 204