HTML Topics
- HTML Intro
- HTML Basic
- HTML Editors
- HTML CSS
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Event Attributes
- HTML Global Attributes
- HTML Attributes
- abbr
- accept
- accept-charset
- accesskey
- action
- align
- alt
- archive
- as
- async
- autocapitalize
- autocomplete
- autofocus
- autoplay
- axis
- bgcolor
- border
- cellpadding
- cellspacing
- char
- charoff
- charset
- checked
- cite
- class
- classid
- codebase
- codetype
- color
- cols
- colspan
- compact
- content
- contenteditable
- controls
- coords
- crossorigin
- data
- data-*
- datetime
- declare
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- formenctype
- formmethod
- formnovalidate
- formtarget
- frame
- frameborder
- frameset
- headers
- height
- hidden
- high
- href
- hreflang
- hspace
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Comments
- HTML Entity
- HTML Head
- HTML Form
- HTML IndexedDB
- HTML Drag & Drop
- HTML Geolocation
- HTML Canvas
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML usemap Attribute
Photo Credit to CodeToFun
🙋 Introduction
The usemap
attribute in HTML is a powerful tool for creating image maps, allowing developers to define clickable areas within an image.
By associating an image with a map, you can designate specific regions that act as links to different destinations, providing an interactive and engaging user experience.
🎯 Purpose of usemap
The primary purpose of the usemap
attribute is to connect an image with a client-side image map.
This attribute is typically applied to the <img> element and references a corresponding <map> element where the clickable areas are defined.
💎 Values
The usemap
attribute accepts a value that is the ID of a <map> element.
The value should start with a hash symbol (#) followed by the ID of the map. For example:
<img src="example.jpg" alt="Example Image" usemap="#exampleMap">
<map name="exampleMap">
<!-- Map areas defined here -->
</map>
🧠 How it Works
In this example, the usemap
attribute is set to #exampleMap, indicating that the image is associated with the map named "exampleMap."
📄 Example
Let's look at a simple example of how to use the usemap
attribute in an HTML document:
<img src="world-map.jpg" alt="World Map" usemap="#worldMap">
<map name="worldMap">
<area shape="rect" coords="0,0,100,100" href="north-america.html" alt="North America">
<area shape="rect" coords="100,0,200,100" href="europe.html" alt="Europe">
<!-- Add more areas as needed -->
</map>
🧠 How it Works
In this example, the usemap
attribute is set to #worldMap, connecting the image of a world map to a map with clickable areas for North America and Europe.
🔄 Dynamic Values with JavaScript
You can dynamically manipulate the usemap
attribute using JavaScript, providing flexibility in response to user interactions or certain conditions. Here's a basic example:
<script>
// Dynamically set usemap for an image
function updateMap() {
document.getElementById("dynamicImage").useMap = "#dynamicMap";
}
</script>
<button onclick="updateMap()">Change Map</button>
<img id="dynamicImage" src="dynamic-map.jpg" alt="Dynamic Map" usemap="#defaultMap">
<map name="defaultMap">
<!-- Default map areas defined here -->
</map>
<map name="dynamicMap">
<!-- Updated map areas defined here -->
</map>
🧠 How it Works
In this script, clicking the Change Map button triggers the updateMap function, dynamically changing the usemap
attribute for the image with the ID "dynamicImage."
🏆 Best Practices
- Ensure that the <map> element referenced by the
usemap
attribute contains the necessary <area> elements with correct coordinates and destination links. - Test image maps thoroughly on different browsers to ensure consistent behavior.
- Consider accessibility by providing alternative text for each clickable area defined within the map.
🎉 Conclusion
The usemap
attribute in HTML is a valuable tool for creating interactive image maps, allowing developers to define clickable areas within images.
By understanding and effectively using this attribute, you can enhance the navigability and interactivity of your web pages.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML usemap Attribute), please comment here. I will help you immediately.