Image map in css

Friday 17 April 09 by Onsitus in CSS Tutorials

In this tutorial, we will see how to add clickable area to a background image to create in css the effect of image mapping without using the html element <map>.

This method is used to render part of an image interactive with the mouse on click and is valid for rectangular area.

First, I created a background image representing a top picture (type header) and a bottom menu with 4 buttons:

css image map

To this image, I will ad 5 clickable area, the first being the picture of the frog, the other 4 being the 4 buttons of the bottom menu.

To understand better what we will create, I duplicated the above picture and added a white border to each clickable area, their width and height and their position (top and left):

css image mapping

The html structure

The html structure consists of a simple block element div with an unique id which contains 5 elements <a> (the links), each one having their own id (to give each one their unique style) and a class name to the links of the bottom menu which share the same css style (and avoid to repeat the same css rules).

<div id="image_map">
<a href="#nogo" id="link1"></a>
<a href="#nogo" id="link2" class="menu"></a>
<a href="#nogo" id="link3" class="menu"></a>
<a href="#nogo" id="link4" class="menu"></a>
<a href="#nogo" id="link5" class="menu"></a>
</div>

The css rules to create the image mapping

We will start with the css style of the main div (id=”image_map”), we specify:

  • the width: the same width as the background image
  • the height: the same height as the background image
  • the background image using the css property background
  • the position relative needed next to position its sub elements in an absolute way

#image_map{
width:450px;
height:400px;
background:url(css_image_map.jpg) no-repeat;
position:relative;
}

The css style of the links inside ‘image_map’:

#image_map a{
display:block;
position:absolute;
}

All the links of the bottom menu share the same css property, for this reason I chosed to use a commune class to avoid to repeat the same css rule: the width of 100px, the height of 50px and their position at 340px form the top border of the main div.

#image_map a.menu{
width:100px;
height:50px;
top:340px;
}

Other then that, each has a unique id (link1, link2, link3…) used to impost their specific style.

For the first link which will cover the area of the picture of the frog: the width of 430px, the height of 320px, the vertical position at 10px from the top border and horizontal of 10px from the left border of the main div.

#image_map a#link1{
width:430px;
height:320px;
top:10px;
left:10px;
}

As we have already specify the css rules for the class ‘menu’ of the 4 bottom links, it will remain to specify for each one of them their horizontal position from the left border of the main div.

#image_map a#link2{
left:10px;
}
#image_map a#link3{
left:120px;
}
#image_map a#link4{
left:230px;
}
#image_map a#link5{
left:340px;
}

The result of the image map





13 comments

Stay updated with our RSS Feed

{ 13 comments… read them below or add one }

Lara 04.29.09 at 04:05

Thank you SO much for this guide! I got the image map done on my FIRST TRY, I didn’t even need to adjust it! This was a great guide and it really made it easy for me to get the job done. Thanks again! :)

Onsitus 04.29.09 at 06:49

Nice, I am happy to see that my tutorials are appreciated!

Jim 05.09.09 at 19:11

Thanks for this great tutorial, exactly what I’ve been looking for.
I’ve mapped 9-hotspots on a common background, each to be linked to a separate photo album. The only problem is I can’t get it to run with IE 7. However, it runs well on 3-other browsers. Any suggestions?

Onsitus 05.11.09 at 06:59

Ciao Jim, do you have a link to your testing page?

Jim 05.11.09 at 21:46

Onsitus,
Sure here it is:
http://halicho.com/home/gallery/#nogo
Jim

Onsitus 05.12.09 at 07:05

Ciao,
In the html structure, remove the element img (not necessary as the background image is loaded via css) and remove the p and br (simply put the link in the div ‘image_map’). That should system the problem.

Jim 05.12.09 at 19:34

Yes, that was the problem, it now works well on the five browsers I’ve tested.
Thank you very much for your help with this, and for your quick response.
-jim

rakesh 06.24.09 at 08:42

good one. helps alot..thanks

Jesica 06.25.09 at 19:08

OMG. I read a ton of tutorials and was starting to put together the code. Then I found yours, and POOF! I was able to make yours work with very little effort. Oh, thank you, thank you, thank you!!!!

egorbrandt 10.09.09 at 11:37

thank you for a well-written and concise tutorial!

is there any way of putting an extra dropdown menu on top of the background image-mapping one? i can’t seem to figure it out… either i’m left with working links on the background image and a non-functioning dropdown menu bar or the other way round.

your help is much appreciated!

Ravi 11.07.09 at 01:42

Thanks for the tutorial…. very useful one! :)

squareart 01.11.10 at 21:21

Thanks a million mate! I was searching for a fix to put a div with text on an image map - but this method solves the problem!
Great tut! THANK YOU: )

Asjad 02.17.10 at 22:41

So Nice, Thank you so much, very very use full

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Previous post: Css image gallery

Next post: Italian map in css