<area>
and <map>
Elements<area>
and <map>
ElementsThe <map>
element in HTML is used to define an image - map. An image - map is an image with clickable areas. Each clickable area is defined using the <area>
element. The <area>
element has attributes such as shape
(which can be set to poly
for polygons), coords
(which defines the coordinates of the polygon), and href
(which specifies the URL to navigate to when the area is clicked).
<img src="canada-outline.jpg" usemap="#canadaMap">
<map name="canadaMap">
<area shape="poly" coords="x1,y1,x2,y2,x3,y3,..." href="province-info.html">
</map>
Although the <area>
element itself doesn’t have direct CSS styling, we can use CSS to style the underlying image and create visual effects for the map. For example, we can change the opacity of the image on hover to give an interactive feel.
img[usemap] {
cursor: pointer;
}
img[usemap]:hover {
opacity: 0.8;
}
The coords
attribute in the <area>
element defines the polygon’s shape. The coordinates are a series of x,y
pairs that represent the vertices of the polygon. The origin (0,0)
is at the top - left corner of the image.
First, we need to set up the basic HTML structure. We’ll start with an image of the Canada outline and create a <map>
element associated with it.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF - 8">
<meta name="viewport" content="width=device-width, initial - scale=1.0">
<title>Canada Map</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<img src="canada-outline.jpg" usemap="#canadaMap" alt="Canada Map">
<map name="canadaMap">
<!-- Areas for provinces and territories will go here -->
</map>
</body>
</html>
We need to find the appropriate polygon coordinates for each Canadian province and territory. These can be obtained through image editing tools or online coordinate generators.
<map name="canadaMap">
<area shape="poly" coords="100,200,150,250,200,200" href="ontario-info.html" alt="Ontario">
<area shape="poly" coords="300,100,350,150,400,100" href="british-columbia-info.html" alt="British Columbia">
</map>
We can style the map to make it more visually appealing. For example, we can add a border to the image and change the color on hover.
img[usemap] {
border: 2px solid #333;
transition: opacity 0.3s ease;
}
img[usemap]:hover {
opacity: 0.8;
border-color: #ff6600;
}
To make the map responsive, we can use the max - width: 100%
and height: auto
properties on the image. This ensures that the map scales correctly on different screen sizes.
img[usemap] {
max - width: 100%;
height: auto;
}
We can add more interactivity by using JavaScript to show additional information when a province or territory is clicked. For example, we can display a tooltip with the province’s name and some basic facts.
<script>
const areas = document.querySelectorAll('area');
areas.forEach(area => {
area.addEventListener('click', function () {
alert(`You clicked on ${this.alt}`);
});
});
</script>
It’s important to add proper alt
attributes to the <area>
elements. This helps screen readers understand the purpose of each clickable area. Also, ensure that the color contrast between the map and any text or visual elements meets accessibility standards.
Keep your HTML, CSS, and JavaScript code organized. Use separate files for CSS and JavaScript. In the HTML file, group the <area>
elements by region or alphabetically for better readability.
Optimize the image used for the map. Compress the image to reduce its file size without sacrificing too much quality. Also, avoid using excessive CSS animations or JavaScript that can slow down the page.
Test the map on different browsers and devices to ensure consistent behavior. Use browser developer tools to debug any issues with the polygon coordinates or interactivity.
Creating a Canada map using CSS and HTML polygons is a powerful way to add an interactive and engaging element to your web page. By understanding the fundamental concepts, following the usage methods, implementing common practices, and adhering to best practices, you can create a high - quality map that is both visually appealing and user - friendly. With a bit of creativity and attention to detail, you can take your web design to the next level.