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 shape Attribute
Photo Credit to CodeToFun
🙋 Introduction
The shape
attribute is a versatile feature in HTML used in conjunction with the <area> element to define the shape of a clickable area within an image map.
Image maps allow developers to create interactive regions on an image, each of which can be linked to different destinations.
The shape
attribute plays a crucial role in determining the clickable area's geometry.
🎯 Purpose of shape
The primary purpose of the shape
attribute is to specify the shape of the clickable area associated with an <area> element.
By defining the shape, developers can create image maps that include rectangular, circular, or polygonal clickable regions.
💎 Values
The shape
attribute accepts various values to define different shapes. The three main values are:
- rect: This value is used for creating rectangular clickable areas. It requires four additional attributes: coords (defining the coordinates of the top-left and bottom-right corners).
- circle: This value is used for creating circular clickable areas. It requires three additional attributes: coords (defining the coordinates of the circle's center) and radius (defining the radius of the circle).
- poly: This value is used for creating polygonal clickable areas. It requires the coords attribute to define the coordinates of the polygon's vertices.
📄 Example
Let's look at a simple example of how to use the shape
attribute in combination with the <area> element:
<img src="example.jpg" alt="Example Image" usemap="#exampleMap" />
<map name="exampleMap">
<area shape="rect" coords="0,0,50,50" href="destination1.html" alt="Clickable Area 1" />
<area shape="circle" coords="100,100,50" href="destination2.html" alt="Clickable Area 2" />
<area shape="poly" coords="200,0,250,50,200,100" href="destination3.html" alt="Clickable Area 3" />
</map>
🧠 How it Works
In this example, three clickable areas are defined using the rect, circle, and poly values of the shape
attribute within the <area> elements.
🔄 Dynamic Values with JavaScript
You can also dynamically set the shape
attribute using JavaScript, allowing for more interactive and adaptive image maps. Here's a brief example:
<script>
// Dynamically set the shape attribute for an area element
var areaElement = document.getElementById("dynamicArea");
areaElement.shape = "rect";
areaElement.coords = "50,50,100,100";
</script>
🧠 How it Works
In this script, the shape
attribute is dynamically set to rect for an area element with the id dynamicArea, and the coords attribute is updated accordingly.
This can be particularly useful when you want to modify image map areas based on user interactions or other dynamic conditions.
🏆 Best Practices
- Ensure that the coordinates specified in the coords attribute are accurate to define the clickable area correctly.
- Test image maps thoroughly to ensure cross-browser compatibility and responsiveness.
- Use the appropriate shape value based on the desired clickable area geometry.
🎉 Conclusion
The shape
attribute is a valuable tool for creating interactive image maps in HTML..
By understanding and using this attribute appropriately, developers can design visually engaging and interactive 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 shape Attribute), please comment here. I will help you immediately.