CSS Image Maps With Pop-Up Tool Tips

Using an unordered list, a couple of images, and a little CSS, we can create an accessible "image map" with pop-up tool tips that provide our readers more information on the links the map contains. There are several techniques out there for doing this, but, unlike some, the one outlined here has the advantage of working in older versions of Internet Explorer.

To begin, we'll need two images, the first one being our image map, and the second one being a 1px by 1px transparent GIF image, which we'll use as a background image for the map's "hot spots" in order to work around a bug in earlier versions of IE.

Here's the image I'll be using for the map itself:

Image M ap

Now we can move on to the HTML. Essentially, what we'll do is create an unordered list of links that each contain a span tag which holds information about the link for our tool tip pop-up. We'll give each link its own class name, so that we can define the area and position for the link's hot spot on the image.

Here's the HTML for my map.

HTML:
  1. <ul id="map">
  2.     <li><a class="bunny" href="#" title="Bunny"><span><b>Bunny Rabbit</b><br>
  3.         Cute and fuzzy, with long ears and a fluffy tail. this cuddly bunny
  4.         makes a great pet.</span></a></li>
  5.  
  6.     <li><a class="parrot" href="#" title="Parrot"><span><b>Parrot</b><br>
  7.         With a lifespan of up to 40 years, these bright, colorful birds
  8.         are wonderful life-long pets. One of only a few birds
  9.         capable of learning to speak, parrots are intelilgent and
  10.         highly social animals.</span></a></li>
  11.        
  12.     <li><a class="snail" href="#" title="Snail"><span><b>Snail</b><br>
  13.         Snails are a wonderful addition to any aquarium-- just make
  14.         sure that its tank-mates aren't going to eat it! Snails say
  15.         "Meow."</span></a></li>
  16.        
  17.     <li><a class="dog" href="#" title="Dog"><span><b>Dog</b><br>
  18.         Man's best friend, a protector and commrade of humans for
  19.         centuries, dogs are faithful, loyal and loving companions.
  20.         </span></a></li>
  21.        
  22.     <li><a class="cat" href="#" title="Cat"><span><b>Cat</b><br>
  23.         Another popular pet, these felines were once viewd as a
  24.         sacred creature amont the ancient Egyptians. Independant,
  25.         sociable, and with a rumbling purr, cats make wonderful
  26.         pets.</span></a></li>
  27. </ul>

Now let's move on to the CSS, starting with the list itself, which we've given the ID of "map." We want to set the margin and padding for our list to zero and then we want to define the size of the list, which should be equal to the size of the image we're using for our map (in this case, 400x200 pixels). Then we'll set that map image as the background. I'm also defining some font styles here, so I don't have to do it later.

CSS:
  1. #map {
  2.     margin:0;
  3.     padding:0;
  4.     width:400px;
  5.     height:200px;
  6.     background:url(map.jpg) top left no-repeat #fff;
  7.     font-family:arial, helvetica, sans-serif;
  8.     font-size:8pt;
  9. }

Then we'll do the list items themselves-- simple stuff, just get rid of the formatting applied by the browser.

CSS:
  1. #map li {
  2.     margin:0;
  3.     padding:0;
  4.     list-style:none;
  5. }

Now we need to apply some styling to the links. Each link will need its own dimensions and positioning (which is why we gave them class names), but they'll also share some common properties, like the fact that they should be absolutely positioned block elements, with the transparent GIF for their background.

CSS:
  1. #map li a {
  2.     position:absolute;
  3.     display:block;
  4.     /*
  5.        Specifying a background image
  6.        (a 1px by 1px transparent gif)
  7.        fixes a bug in older versions of
  8.        IE that causeses the block to not
  9.        render at its full dimensions.
  10.     */
  11.     background:url(blank.gif);
  12. }

Next, we simply need to hide the span inside of our links.

CSS:
  1. #map li a span { display:none; }

After we've done that, we need to define the style, size and position of the tool tip itself. We need to make it visible on mouse over and it should be relatively positioned in relation to the link we're hovering over, with a bit of an offset (20px x 20px in this case). Then we'll make it look pretty, giving it a white background, a border, padding, and lower the opacity slightly, so you can still see what's under the tool tip. We'll set a width of 200px, but we'll let the height be automatically determined by the amount of content in the tool tip. We'll also set the color of the text and get rid of the underline.

CSS:
  1. #map li a:hover span {
  2.     position:relative;
  3.     display:block;
  4.     width:200px;
  5.     left:20px;
  6.     top:20px;
  7.     padding:5px;
  8.     border:1px solid #000;
  9.     background:#fff;
  10.     text-decoration:none;
  11.     color:#000;
  12.     filter:alpha(opacity=80);
  13.     opacity:0.8;
  14. }

Now all that's left is to define our link hot spots and set the size and position of the links, by class name, that correspond with the images. You'll have to do a little bit of measuring here, just as you would with any image map. Just find X, Y coordinates where the top left corner of the hot spot should be, and then determine the height and width from there.

CSS:
  1. #map a.bunny {
  2.     top:20px;
  3.     left:60px;
  4.     width:80px;
  5.     height:90px;
  6. }
  7.        
  8. #map a.parrot {
  9.     top:1px;
  10.     left:275px;
  11.     width:50px;
  12.     height:50px;
  13. }
  14.        
  15. #map a.snail {
  16.     top:135px;
  17.     left:30px;
  18.     width:50px;
  19.     height:50px;
  20. }
  21.        
  22. #map a.dog {
  23.     top:100px;
  24.     left:150px;
  25.     width:115px;
  26.     height:95px;
  27. }
  28.        
  29. #map a.cat {
  30.     top:65px;
  31.     left:315px;
  32.     width:70px;
  33.     height:120px;
  34. }

The final image map can be viewed here.

30 Responses to “CSS Image Maps With Pop-Up Tool Tips”

Posted by: mowgli on August 18th, 2007 at 8:14 pm

wow..fantastic....that means no more worries about these things in javascript...thanks a lot...I will apply on my projects...!!!!

Posted by: JM on September 17th, 2007 at 12:02 am

Great work! Thank you for providing this.

I am new to working with css, but I would really like to adapt this for use on a web page layout that is not static. My layout repositions itself whenever the browser window is resized. This is great for my layout remaining in proper perspective, but this throws off the location of all the tool tips. I'm sure the fix for this is probably not extremely difficult, but so far I am at a loss. Ideas?

I notice that the method employed at http://www.frankmanno.com/ideas/css-imagemap/ works well on a layout that is not fixed, but the code looks a little bloated.

Posted by: Scott on September 18th, 2007 at 2:56 pm

Hey, this seems really cool. I am also new to CSS and Dreamweaver, so I'm kind of confused on what to do after I have all of this code? I kind of just put it on the page, and I got this http://www.builtinvacuum.com/cvtv/vids/flashtest1.html I just sort of copied your dimensions and numbers just so I could get an idea of how it worked, but I came up with that.

Could you help me out please?

If so, please E-mail me at scott@builtinvacuum.com

Thanks

Posted by: Teresa on December 10th, 2007 at 2:19 pm

The address above is not yet live...but I need to do an image map with rollovers for neighborhoods that this builder builds in. I LOVE the code you've written here, it's clear and simple and works amazingly well.

However, I have a question I'm hoping you can help me with. We use a proprietary software to build sites so that our clients can easily maintain them. When I put your image and code in...it shows up...but the rollovers are all the way up in the left corner of the screen...rather than being where the image is. The image shows up in the center of the web page...but the only way to get the rollovers to appear is to move the cursor around up in the corner...then they show up. Any suggestion to correct this? I have moved the coordinates for "dog" and it works ok now...just wondered if that's what will need to happen to the rest...coordinates need to be for the page as a whole rather than for the image?

test page: http://www.redsageonline.com/default.aspx?id=562

thanks so much!!!!

Posted by: CSSHowTo.com on December 10th, 2007 at 9:59 pm

In firefox all your rollovers work where they are supposed to, except for the dog!!

Yes the figures have to be adjusted slightly depending on your page and other styles within the page.

Posted by: Rudolf on December 11th, 2007 at 7:02 am

Great work.
Very helpful to avoid javascript.
Thank you.

Posted by: Kelley on December 12th, 2007 at 6:45 pm

Hey, this is excellent!
The only problem I am having is that my list text shows up all the time. When I mouse over the block, it changes form into the pretty little tooltip box, but when I mouseout, it's just this ugly text covering up my image. Any idea why? =/

Posted by: Kelley on December 14th, 2007 at 4:12 pm

I figured out my problem, but I am still having another issue: the same one that Teresa and JM had above. Has anyone found a fix for this?

Posted by: CSSHowTo.com on December 19th, 2007 at 8:11 pm

Do you have a link?

Posted by: Jay on December 26th, 2007 at 11:01 pm

Use this instead to move the image around (Thanks Teresa / Redsage )


#map {
position:relative;
margin:0;
padding:0;
width:400px;
height:250px;
background:url(../images/map.jpg) no-repeat #fff;
font-family:arial, helvetica, sans-serif;
font-size:8pt;
}

Posted by: Stefano on January 4th, 2008 at 7:27 am

Uhm ... using this in Safari 3.04 I get same problems with text tooltips not on the image and so on.
With last CSS code for #map it seems work only if you put list UL inside a DIV with position:relative.

Posted by: web designs on January 23rd, 2008 at 12:42 pm

this is great, i've been looking for an accesble way of using images maps and callouts without using javascript. Well done

Posted by: Devo! on February 15th, 2008 at 10:55 pm

Wow...this is really cool.

I am really impressed about how easy it was to implement this.

and thank you for the relative position patch for the map....

Posted by: CSSHowTo.com on February 16th, 2008 at 11:22 am

Excellent work guys, thanks for the community feedback in helping with the problems, this is what the Web is all about.

Glad you are all finding it helpful.

Posted by: Quentin on April 2nd, 2008 at 4:39 pm

Only two of my five hotspots work! I've checked and re-checked everything but can't figure out where the problem lies. Any help appreciated!

Cheers

Posted by: CSSHowTo.com on April 2nd, 2008 at 10:29 pm

I notice from the CSS file that there are some non standard characters that have gotten into the file, coincidently around all the classes that are not working, things like
Ê Ê Ê Ê

You can see it better here
http://www.quentinjamesdesign.co.uk/strategy/css/screen.css

Delete those characters from the CSS file and you should find it works fine. ;)

regards
Paul

Posted by: Quentin on April 4th, 2008 at 1:42 pm

Wow! Those pesky characters don't appear in BBEdit!

Thanks Paul.

Posted by: KW on April 12th, 2008 at 4:57 am

I seems like a good plan but you didn't tell us where to put these different parts; within whta tags? Do we just pile them upone behind another withing the body tag?

Posted by: mike k on April 21st, 2008 at 11:26 pm

It does seem to have spotty results in IE7, some of my hotspots work while others do not. Worked well in firefox though. I think this can be improved upon in IE7 by setting the display of "li a.span" to block, with an opacity of 0, and giving it a laid out width. This greatly improved the behavior on IE7 for me. More info here: http://www.quirksmode.org/css/opacity.html

Thanks, Mike.

Posted by: Derek on April 23rd, 2008 at 8:48 pm

I can't get this working in IE6 (even the final image map sample).
Has anyone else come across this issue?
Is there a fix?

Posted by: CSSHowTo.com on April 23rd, 2008 at 9:52 pm

Hi Mike,
thanks for posting the fix, although I cannot find any problems with the demo in IE7, if you have a particular link to problematic page, then please post it.

Derek, I don't currently have access to IE6 so cannot retest, originally it all worked fine, but I will get an VNC session set up with a IE6 machine and get back to you hopefully.

Posted by: Derek on April 28th, 2008 at 3:49 pm

Hi, and thanks,
Has anyone had a chance to test/debug this in IE6?

Posted by: CSSHowTo.com on April 28th, 2008 at 9:26 pm

Hi Derek, Please dont post comments twice as it gives me twice as much work to do when approving them. All comments are approved before appearing.

I used IE6 on Win xp with SP2 and the image map worked fine. Could you provide some more information on the URL you are using it on and what systems you are using.

Posted by: Derek on April 29th, 2008 at 5:57 pm

Hi, and apologies for the double post.
I'm using XP IE6.0 SP2....

I'm in a corporate environment, and it appears there's a problem with our machines, not your code.

I just checked out the page on our testing machine and it works great.

I'm so sorry to have wasted your time.

Please keep up the great work.

Posted by: CSSHowTo.com on April 29th, 2008 at 6:54 pm

No Worries, im just glad you got it fixed/sorted!

Posted by: FG on May 2nd, 2008 at 5:32 pm

This code was just what I needed for a tutorial I'm working on, thanks!

The problem that I'm having is that the image I'm using it with is centered and needs to stay that way to be consistent with other pages in the tutorial. When the window is max'd the hotspots are to the left of the images.

Did anyone come up with a resolution for this issue?
I'm having the problem in both IE6 and Firefox.

Posted by: EVS on May 20th, 2008 at 10:22 pm

Great Tutorial! I've been looking for this type of thing.

I am wondering if there is someway to have an additional link within the as I'm using this technique on a property map so that when the user selects a unit, it opens the window with an image, a description and hopefully, a link to a .pdf of a floorplan.

Any advice?

Thanks!

Posted by: stoffel on July 23rd, 2008 at 10:23 am

has anybody experience with poly shapes, instead of rectangles

Posted by: EJB on August 29th, 2008 at 12:05 pm

Hello I have a question: Is it possible to use an image instead of a text tooltip? So when you roll over the spot you can show an image.

thnx

Posted by: Jacob on September 6th, 2008 at 10:25 pm

Link is in name.

I've done this for one hotspot so far and it works fine in Firefox. I tested in IE7 and the hotspot is ten pixels or so lower than where it is supposed to be.
Can someone help me with this?



Make Your Mark

Allowed HTML: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>