HTML Basic
HTML Reference
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Attributes
- accept
- accept-charset
- accesskey
- action
- align
- alt
- as
- async
- autocomplete
- autofocus
- autoplay
- bgcolor
- border
- charset
- checked
- cite
- class
- color
- cols
- colspan
- content
- contenteditable
- controls
- coords
- data
- data-*
- datetime
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- headers
- height
- hidden
- high
- href
- hreflang
- 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 Global Attributes
- 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.