spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / image_map / 1 To page 1current pageTo page 3
[previous][next]

Data Center Architect
The Computer Merchant, Ltd
US-MA-chelsea

Justtechjobs.com Post A Job | Post A Resume
Developer News
News Flash: Adobe Has iPhone Workaround
Adobe's Flash 10.1 Goes Mobile (Minus iPhone)
A Salute to Visionary CEOs


How to Create an Image Map Using CSS

This additional style information will change the definition list so that it looks like a list of links:

Styled (x)html list

Step 2

Now that we have the definition list in a suitable format, I will take you through the various stages of styling

Firstly and most importantly, make sure that you have the correct (X)HTML !DOCTYPE. Without this, most browsers will be thrown into 'quirks' mode which will lead to all sorts of incompatibility problems. W3C QA - List of valid DTDs has a list of valid DOCTYPES that can be used. Select from XHTML1.0 or XHTML1.1 as these are more suitable for this styling. I use XHTML1.1 for all my current web pages.

Step 3

The images

You will need to find or make a suitable main graphic for the Image Map. I have chosen a photograph of the Beatles that was available from the public domain. This image is 400px wide and 240px high.

The Beatles

Step 4

The <body> is first to be styled with the font-family for the whole page. I have chosen Tahoma as the first choice but you can select any font you wish.

<dl id="imap"> is next. This needs to be styled as a block with width and height values at the same size as your main image. Your main image is then placed as a background image with no-repeat (so that we only get one) and finally a we give this a relative position so that we can place the four <dd>'s using absolute positioning.

/* set up the font to be used for the page */
body {
  font-family: tahoma; arial, sans-serif;
  }

/* set the size of the definition list <dl> and add the background image */
#imap {
  display:block;
  width:400px;
  height:240px;
  background:url(../images/beatles_basic.jpg) no-repeat;
  position:relative;
  }
The results can be seen here:-

Example #1

You should see the main image with the text overlaid.

Step 5

Setting up the main image hover

Now that we have the main image in place, we can set up the image that will indicate the active link areas. This image will be displayed when we hover over the main image.

In my example we will need four 'hotspots.' I have added these to the main image as shown below using an opaque area of 85pixels (px) square with a white border. For the sake of simplicity I have defined all the 'hotspots' as being the same size. These could also have been rectangles of varying sizes.

It doesn't matter if the areas intersect each other as this will be taken care of in the styling.

The Beatles

Since the main image is held as a background image in the <dl> tag we need to place the hover image as a background image in the next tag available which will accept the pseudo class :hover style. This will be the link <a id="title" href="#nogo" title="The Beatles">

As a result, we style this link as below:

/* set up the definition list <dt><a> to hold the background image for the hover state */
#imap a#title {
  display:block;
  width:400px;
  height:0;
  padding-top:240px;
  overflow:hidden;
  position:absolute;
  left:0;
  top:0;
  background:transparent url(../images/beatles_hover.jpg) no-repeat 400px 400px;
  cursor:default;
  }
/* the hack for IE pre IE6 */
* html #imap a#title {
  height:240px;
  he\ight:0;
  }

Again we style this as a block with the same size as the hover image, but this time we use a height of 0px, a top padding of 240px and overflow hidden to move the text associated with the link out of sight. The hover image is set up as the background image and is also moved out of sight by giving it a position larger than the image size. The background color is transparent because we need the main image to show through.

I have also added a hack for Internet Explorer pre IE6 which corrects the box model bug.

With this style added there is no noticeable change to the page but to view this, click on the link below:

Example #2

Step 5

The actual main image :hover

Now that we have the main image for the hover state in place it is a simple matter to style this to show when the mouse is placed over the image.

/* the <dt><a> hover style to move the background image to position 0 0 */
#imap a#title:hover {
  background-position: 0 0;
  z-index:10;
  }

This moves the background image back into view at position 0 0. The z-index is added to ensure that the four 'hotspots' are always positioned above the hover background image (they will have a z-index of 20).

Example #3

You will see from the above example that we now have a change of image when hovered and also the title 'The Beatles' is also displayed next to the mouse.

Step 6

Placing the 'hotspots'

The position of each 'hotspot' is next to be styled. This requires the use of a paint package (or a little trial and error), but the paint package method being easier. What we need to know is the x/y positions of the top left corners of each 'hotspot'.

This information is then used to style the four <dd>s as follows;

/* place the <dd>s in the correct absolute position */
#imap dd {
  position:absolute;
  padding:0;
  margin:0;
  }
#imap #picp {
  left:36px;
  top:46px;
  z-index:20;
  }
#imap #picr {
  left:113px;
  top:76px;
  z-index:20;
  }
#imap #picj {
  left:192px;
  top:50px;
  z-index:20;
  }
#imap #picg {
  left:262px;
  top:60px;
  z-index:20;
  }

We set up the position absolute in a common #imap dd style, then style each unique dd with a top and left position and a z-index of 20 to make sure that they are situated on top of the background image(s).

Example #4

Step 7

Styling the links

The next step is to style the links that have been placed in each <dd>.

/* style the <dd><a> links physical size and the background image for the hover */
#imap a#paul, #imap a#ringo, #imap a#john, #imap a#george {
  display:block;
  width:85px;
  height:85px;
  background:transparent url(../images/hover.gif) -100px -100px no-repeat;
  text-decoration:none;
  z-index:20;
  }

Since all of my 'hotspots' are the same size, I can group the style together. If you have 'hotspots' of varying sizes then you will need to style each one separately.

Each link is styled as a block 85px square with a background image that will be used when hovering over the 'hotspot.' Normally, this image will not be displayed. The z-index is set at 20 to ensure that the links are always on top of the background image(s).

My link hover image is just a transparent gif with a white border, but you can use whatever you like.

Hover image

The above image shows the hover image over a blue background.

There is no noticeable change in the appearance of the page because we have not yet defined the :hover style for each link.

home / programming / image_map / 1 To page 1current pageTo page 3
[previous][next]

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs

webref The latest from WebReference.com Browse >
Building a Banking Application Home Page with OOP · Mixing Scripting Languages · Review: phpFox, a Social Networking CMS with all the Bells and Whistles
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Enterprise 2.0: Social Networking in the Cloud · BroadSoft Marketplace Hastens Pace of Telephony Innovation · Review: HTC Hero for Sprint

Created: March 27, 2003
Revised: March 29, 2005

URL: http://webreference.com/programming/image_map/1