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 coords Attribute
Photo Credit to CodeToFun
🙋 Introduction
The coords
attribute is used in HTML to define the coordinates of clickable areas within an image map.
This attribute is commonly associated with the <area> element, allowing developers to specify the shape and location of an interactive region within an image.
🎯 Purpose of coords
The primary purpose of the coords
attribute is to provide information about the position and shape of a clickable area within an image.
This is crucial when creating image maps, where different regions of an image can serve as hyperlinks to distinct destinations.
💎 Values
The coords
attribute takes different values depending on the shape of the clickable area. Here are the common shapes and their respective coords
attribute values:
- Rectangular Area (rect): Use four values—left, top, right, and bottom—to define the coordinates of the top-left and bottom-right corners of the rectangle.
rectangular-area.htmlCopied
<area shape="rect" coords="left, top, right, bottom" href="destination.html" alt="Description">
- Circular Area (circle): Specify three values—x, y, and radius—to define the center (x, y) and the radius of the circle.
circular-area.htmlCopied
<area shape="circle" coords="x, y, radius" href="destination.html" alt="Description">
- Polygonal Area (poly): Provide a series of x, y coordinate pairs to define the vertices of the polygon.
polygonal-area.htmlCopied
<area shape="poly" coords="x1, y1, x2, y2, x3, y3, ..." href="destination.html" alt="Description">
📄 Example
Here's an example of using the coords
attribute within an image map:
<img src="image.jpg" alt="Clickable Image" usemap="#imageMap">
<map name="imageMap">
<area shape="rect" coords="10, 10, 100, 100" href="destination1.html" alt="Area 1">
<area shape="circle" coords="150, 80, 50" href="destination2.html" alt="Area 2">
<area shape="poly" coords="220, 30, 300, 80, 250, 150" href="destination3.html" alt="Area 3">
</map>
🧠 How it Works
In this example, three clickable areas are defined within the image using different shapes and corresponding coords values.
🔄 Dynamic Values with JavaScript
You can dynamically set the coords
attribute using JavaScript.
This can be useful when you need to adjust the clickable areas based on user interactions or other dynamic factors. Here's a brief example:
<script>
// Dynamically set coords for a circular area
document.getElementById("dynamicArea").coords = "200, 120, 80";
</script>
🧠 How it Works
In this script, the coords
attribute is dynamically set for an area with the id dynamicArea, defining a circular shape with a center at (200, 120) and a radius of 80.
🏆 Best Practices
- Ensure that the coordinates provided match the actual dimensions and positions within the image to create accurate clickable areas.
- Test your image maps across different browsers to ensure consistent behavior.
- Consider accessibility by providing descriptive alternative text (alt attribute) for each clickable area.
🎉 Conclusion
The coords
attribute plays a crucial role in creating interactive image maps in HTML.
By understanding how to use and implement this attribute, you can enhance the interactivity and navigation 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 coords Attribute), please comment here. I will help you immediately.