linkedin github twitter

Teaching <map> & Teaching <area>

Discovering awesome HTML elements

Starting Point

Say you have a good-looking, sharp picture.

Picture of a chalk-board

Wouldn't it be nice to have it reference some nice URL?

HTML can do that!

Simply wrap the <img> element in between anchor link tags. The following markup for instance:

      
      <a href="nice_url">
        <img
          src="img_url"
          alt="Nice sharp image" />
      </a>
      
    

Allows such a feat:

Picture of a chalk-board

Wouldn't it be great to have it reference multiple URLs, according to which section of the image was clicked?

HTML can do that!

You just need to get acquainted with two new friends: <map> and <area>

Map

The <map> element is used primarily for two reasons:

  1. To group <area> elements;
  2. To provide an identifier through the name attribute, as to later reference the map itself.
      
      <map name="mapName">
        <area/>
        <area/>
      </map>
      
    

Area

The <area> element is instead used to define a region, or regions, responsible for specified URLs.

The element itself has several attributes, each solving its own purpose:

      
        <area
          shape="circle"
          coords="100, 100, 50"
          href="nice_url"
          target="_blank"
          alt="Nice sharp area" />
      
    

Map & Area

To fulfill the overarching purpose, two additional steps are required

  1. Nest the <area> element(s) in a wrapping <map> element;
          
          <map name="mapName">
            <area
            shape="circle"
            coords="100, 100, 50"
            href="nice_url"
            target="_blank"
            alt="Nice sharp area" />
          </map>
          
        
  2. Reference the map in the usemap attribute of an <img/> element, alongside its other attributes.
          
          <img
            usemap="#mapName"
            src="img_url"
            alt="Nice sharp image"/>
          
        

In Practice

Including the previous logic for the image included in the beginning of this simple tutorial, circle areas allow to have each "thought" bubble reference a particular URL

Picture of a chalk-board

All with the following source code:

      
      <map name="bubbles">
        <area
          shape="circle"
          coords="100, 80, 50"
          href="#"
        />

        <area
          shape="circle"
          coords="100, 230, 50"
          href="#"
        />

        <area
          shape="circle"
          coords="120, 350, 40"
          href="#"
        />

        <area
          shape="circle"
          coords="600, 80, 50"
          href="#"
        />

        <area
          shape="circle"
          coords="600, 230, 50"
          href="#"
        />

        <area
          shape="circle"
          coords="580, 350, 40"
          href="#"
        />

      </map>

      <img
        usemap="#bubbles"
        src="img_url"
        alt="Picture of a chalk-board"/>
      
    

Parting Thoughts

Together, the <map> and <area> elements provide intriguing possibilities.

That being said, you should consider the following:

Other than that, tinker and experiment with the HTML elements at will. Hit the tab key to "visualize" the areas specified by the elements and have a blast.

I sure had some fun #gainwithstewartilondanga