Working With Area Tag in HTML5

Introduction 

 
The <area> tag is used only within a <map> tag. The <area> tag is used to define the areas on the image map that whenever the user clicks on the area the other page that the user is navigated to. The image map is used to divide the image into clickable areas and the clickable areas work like a hyperlink.
 
Syntax
  1. <area  
  2. alt="alternative text"  
  3. class="class name(s)"  
  4. coords="comma-separated list of values"  
  5. dir="ltr | rtl"  
  6. href="URL"  
  7. id="unique alphanumeric identifier"  
  8. lang="language code"  
  9. nohref="nohref"  
  10. shape="circle | default | poly | rect"  
  11. target="frame or window name | _blank | _parent | _self |_top"   
  12. title="advisory text"/>  
In the <area> tag there are very many element-specific attributes as described below.
 
Alt:
This attribute is used only when the web page cannot render the image and the href attribute on the web page. It is used as an alternative text message in place of an image if the image is not rendered.
 
Class: This attribute is used to specify a class name for an element. It associates a particular area within an image map with a CSS class.
 
Coords: This attribute is used to define the clickable area of any shape on the image map. It contains a set of values specifying the coordinate of the hotspot region. You can only set the area; not the shape directly here. If you want to set your shape then you should use a shape attribute also. The Shape attribute can contain a rectangle shape, circle shape, and polygon shape.
  • rect: left, top, right, bottom
  • circle: center-x, center-y, radius
  • poly: x1, y1, x2, y2,
dir: this attribute is used for text direction for the content in an element.
  1. <div>  
  2.     Text running Left to Right:  
  3.       
  4.     <bdo dir="ltr">  
  5.         <b>  
  6.             <q>My name is DON</q>  
  7.         </b>  
  8.     </bdo>  
  9. </div>  
  10. <div>  
  11.     Text running Right to Left.  
  12.       
  13.     <bdo dir="rtl">  
  14.         <b>  
  15.             <q>My name is DON</q>  
  16.         </b>  
  17.     </bdo>  
  18. </div>  
Output:
 
1.gif
 
href: This attribute is used for navigation. This attribute specifies the hyperlink for the area. You should use either href attribute or nohref attribute.
 
hreflang: This attribute is introduced by HTML5 and is used to specify the language of the target URL. This attribute is used only when the href attribute is used in the element.
 
id: This attribute is used to specify a unique id for an element.
 
lang: This attribute is used to specify a language code for the content in an element.
 
media: This attribute is used to specify a media format, which means whatever media/device that the target URL is optimized for. This attribute is used only when the href attribute is present. The value must be a valid media query. The default value is all.
 
ping: This attribute is used to specify the URL(s) that will be notified when a link is visited (when the user follows the hyperlink). You can specify more than one URL(s) but they must be separated by commas. This attribute is used only when the href attribute is present in an element.
 
rel: This attribute is introduced by HTML5 and it is used to specify the relationship between the current document and the destination or target  URI. If you don't specify the relation then the void is used. There are many values that you can use in rel attribute but using comma fro separating values. Possible values:
  1. alternate
  2. archives
  3. author
  4. bookmark
  5. external
  6. feed
  7. first
  8. help
  9. index
  10. last
  11. license
  12. next
  13. nofollow
  14. noreferrer
  15. prev
  16. search
  17. sidebar
  18. tag
  19. up
shape: This attribute is used to define the shape for the clickable area in the image map for the link. Possible values are:
 
default: you can specify default value also.
 
rect: rect value defines a rectangular region
 
circle: circle value defines a circular region
 
poly: poly value defines the polygon region.
 
target: Specifies where to open the target URL specified in href attribute.
  • _blank : blank value indicates a new window must be open.
  • _parent: parent value is used in the situation where a frameset file is nested inside another frameset file. A link in one of the inner frameset documents which uses "_parent" will load the new document where the inner frameset file had been.
  • _self: self value indicates the frame contains the source link. It works the same as if you had not used TARGET at all.
  • _top: top value indicates that it will use a full browser window.
type: This attribute is used to specify the type of media in the form of the MIME type for the hyperlink.
 
Note: One thing always remember whenever you use area tag in map tag. Your <img> tag must contain usemap value with #. If you ignore this your clickable image not work on Internet Explorer or FireFox, so always use # in usemap value of <img> tag.
  1. <img src="image/all balls.jpg" style="width: 450px; height: 450px;" alt="Balls" usemap="#BallMap" />   

Code:

 
HTMLPage.htm
  1. <!DOCTYPE >  
  2. <html>  
  3.     <head>  
  4.         <title></title>  
  5.     </head>  
  6.     <body>  
  7.         <img src="image/all balls.jpg" style="width: 450px; height: 450px;" alt="Balls" usemap="#BallMap" />  
  8.         <map name="BallMap">  
  9.             <area shape="circle" coords="78,81,76.5" href="white.htm" alt="white" />  
  10.             <area shape="circle" coords="260,81,76.5" href="blue.htm" alt="blue" />  
  11.             <area shape="circle" coords="84,254,76.5" href="javascript:alert('RED ball');" alt="red" />  
  12.             <area shape="circle" coords="366,232,76.5" href="yellow.htm" alt="yellow" />  
  13.             <area shape="circle" coords="238,370,76.5" href="green.htm" alt="green" />  
  14.         </map>  
  15.     </body>  
  16. </html>  
Green.htm
  1. <!DOCTYPE>  
  2. <html>  
  3.     <head>  
  4.         <title></title>  
  5.     </head>  
  6.     <body>  
  7.         <img src="image/green.gif" alt="green" />  
  8.     </body>  
  9. </html>  
Red.htm
  1. <!DOCTYPE>  
  2. <html>  
  3.     <head>  
  4.         <title></title>  
  5.     </head>  
  6.     <body>  
  7.         <img src="image/red.gif" alt="red" />  
  8.     </body>  
  9. </html>  
Blue.htm
  1. <!DOCTYPE >  
  2. <html >  
  3.     <head>  
  4.         <title></title>  
  5.     </head>  
  6.     <body>  
  7.         <img src="image/blue-b.gif" alt="blue" />  
  8.     </body>  
  9. </html>  
Yellow.htm
  1. <!DOCTYPE>  
  2. <html>  
  3.     <head>  
  4.         <title></title>  
  5.     </head>  
  6.     <body>  
  7.         <img src="image/yellow.gif" alt="yellow" />  
  8.     </body>  
  9. </html>  
White.htm
  1. <!DOCTYPE >  
  2. <html>  
  3.     <head>  
  4.         <title></title>  
  5.     </head>  
  6.     <body>  
  7.         <img src="image/white.gif" alt="White" />  
  8.     </body>  
  9. </html>  
Output:
 
IE
 
IEEE.gif
 
Chrome
 
chrome.gif
 
FireFox
 
firefox.gif
 
Click on the green ball to move to the green.htm page.
 
green.gif
 
Click on the red ball to get a message box as shown in the figure below.
 
messagebox.gif