EN JA ZH ES

How to Create HTML Image Maps and Modern Alternatives - Clickable Map Implementation Guide

· About 9 min read

HTML Image Map Basics - Structure of map and area Elements

HTML image maps define multiple clickable regions within a single image. By combining <map> and <area> elements, you can assign links or actions to specific coordinates on an image. They've been used for maps, floor plans, anatomical diagrams, and organizational charts where different parts of an image need to provide different information.

Basic structure:

<img src="office-map.png" usemap="#officemap" alt="Office floor map">
<map name="officemap">
<area shape="rect" coords="0,0,200,150" href="/meeting-room-a" alt="Meeting Room A">
<area shape="circle" coords="300,100,50" href="/lounge" alt="Lounge">
<area shape="poly" coords="400,0,500,50,450,150" href="/reception" alt="Reception">
</map>

Shape attribute types:

  • rect: Rectangle. coords="x1,y1,x2,y2" specifies top-left and bottom-right corners
  • circle: Circle. coords="cx,cy,r" specifies center coordinates and radius
  • poly: Polygon. coords="x1,y1,x2,y2,..." lists each vertex coordinate
  • default: Entire image. Fallback for areas not covered by other regions

The usemap attribute value is # followed by the <map> element's name value. All coordinates are in pixels with the image's top-left corner as origin (0,0).

Coordinate Design for Image Maps - Efficient Region Definition with Tools

Manually calculating image map coordinates is inefficient. Dedicated tools let you generate accurate coordinates simply by clicking on the image.

Online tools:

  • image-map.net: Upload images in the browser and draw rectangular, circular, and polygonal regions with the mouse. Auto-generates HTML code
  • Maperative: Specialized for creating complex polygonal regions. Intuitive vertex addition, deletion, and movement
  • GIMP: Image editor with a built-in image map creation tool in its filter functions

Coordinate design best practices:

  • Ensure click regions are at least 44x44 pixels (WCAG touch target size guideline)
  • Leave 2-4 pixel gaps between adjacent regions to prevent misclicks
  • Keep polygon vertex counts to the minimum necessary. Overly complex shapes impact performance
  • Specify coordinate values as integers. Decimals cause unexpected behavior in some browsers

Debugging methods: During development, you cannot visually highlight area elements with CSS alone, but you can read area element coords with JavaScript and draw overlay visualizations on a Canvas. Additionally, hovering over area elements in browser developer tools highlights the corresponding region.

Responsive Design Challenges - The Biggest Weakness of Image Maps

The biggest problem with HTML image maps is that coordinates are absolute pixel values, so regions become misaligned when image size changes. In modern web development where responsive design is standard, this limitation is critical.

Concrete example: If the original image is 800x600 pixels with a rectangular region defined as coords="200,150,400,300", and the image is displayed at 400x300 pixels due to screen width, the coordinates remain at 200,150,400,300, causing the click region to extend beyond the image's bottom-right.

Dynamic coordinate transformation with JavaScript:

function resizeImageMap() {
const img = document.querySelector('img[usemap]');
const naturalWidth = img.naturalWidth;
const displayWidth = img.clientWidth;
const scale = displayWidth / naturalWidth;
const areas = document.querySelectorAll('area');
areas.forEach(area => {
const original = area.dataset.originalCoords.split(',');
const scaled = original.map(c => Math.round(c * scale));
area.coords = scaled.join(',');
});
}

This approach stores original coordinates in a data-original-coords attribute and recalculates them based on the scale ratio when the image resizes. Use ResizeObserver to monitor image size changes and trigger recalculation.

Library options: image-map-resizer (jQuery plugin) and RWD-Image-Maps provide automatic coordinate resizing, but both have stalled maintenance. For new projects, SVG-based alternatives described below are recommended.

Modern SVG Alternative - Responsive and Interactive

SVG (Scalable Vector Graphics) enables image map functionality in a responsive and accessible way. Being vector-based, SVG maintains coordinate accuracy at any size.

SVG overlay approach:

<div class="map-container" style="position: relative;">
<img src="floor-plan.jpg" alt="Floor plan">
<svg viewBox="0 0 800 600" style="position: absolute; inset: 0; width: 100%; height: 100%;">
<a href="/room-a">
<rect x="50" y="30" width="200" height="150" fill="transparent" class="hotspot">
<title>Meeting Room A</title>
</rect>
</a>
<a href="/lounge">
<circle cx="500" cy="300" r="60" fill="transparent" class="hotspot">
<title>Lounge</title>
</circle>
</a>
</svg>
</div>

SVG approach advantages:

  • viewBox automatically adapts the coordinate system to image size, eliminating responsive issues
  • CSS hover effects (fill, opacity, stroke) can be freely applied
  • CSS transitions and animations enable smooth interactions
  • <title> elements embed accessibility information directly
  • Works completely without JavaScript

Hover effect CSS:

.hotspot { transition: fill 0.2s, opacity 0.2s; cursor: pointer; }
.hotspot:hover { fill: rgba(59, 130, 246, 0.3); }
.hotspot:focus { outline: 2px solid #3b82f6; outline-offset: 2px; }

CSS Grid and Flexbox Alternatives - Image Splitting Techniques

Physically splitting an image and arranging tiles with CSS Grid or Flexbox is another effective alternative to image maps. This approach works particularly well when all regions are rectangular.

CSS Grid implementation:

<div class="image-grid">
<a href="/section-1" class="grid-cell" style="grid-area: 1/1/2/3;">
<span class="sr-only">Section 1</span>
</a>
<a href="/section-2" class="grid-cell" style="grid-area: 1/3/3/4;">
<span class="sr-only">Section 2</span>
</a>
</div>

Set the background image on .image-grid and place transparent link elements as grid cells. Each cell's grid-area defines its region, and CSS Grid automatically handles responsive behavior.

Image slice approach: Split the image into multiple tiles, each placed as individual <img> or <a> elements. Use Photoshop's slice feature or Sharp's extract() method to divide images. This approach is practical in HTTP/2 environments where multiple request overhead is minimal.

CSS clip-path for irregular shapes: When non-rectangular shapes are needed, clip-path: polygon() can clip each link element's visible area into irregular shapes. Percentage values are supported, making responsive adaptation easy. However, coordinates must be carefully designed to ensure adjacent region boundaries align precisely.

Accessibility and Optimal Solutions by Use Case - Technology Selection Criteria

When choosing an image map implementation method, base your decision on accessibility requirements and use case specifics. Each technology has strengths and weaknesses with no universal solution.

Accessibility requirements:

  • Always set alternative text (alt or aria-label) for all clickable regions
  • Verify all regions are accessible via keyboard navigation (Tab key)
  • Display clear focus indicators (control with :focus-visible in SVG approach)
  • Test that screen readers convey region purpose and current position
  • Confirm touch device tap areas are sufficiently large (44x44 CSS pixels minimum)

Recommended technology by use case:

  • Simple rectangular links: CSS Grid + transparent link elements. Simplest and most maintainable
  • Irregular shape hotspots: SVG overlay. Best responsive support and interaction flexibility
  • Legacy system maintenance: Existing HTML image map + JavaScript resize. Minimal changes required
  • Interactive maps: Leaflet.js or Mapbox GL JS. When zoom, pan, and layer control are needed
  • Data visualization: D3.js + SVG. When dynamic data-driven region generation is required

Performance comparison: HTML image maps have the fewest DOM elements and are lightest. SVG overlay is moderate. CSS Grid approach increases DOM nodes with many regions, but within typical usage (under 50 regions), performance differences are negligible.

Related Articles

Image Accessibility - Writing Alt Text and Understanding Contrast Ratio Standards

Learn proper image handling for web accessibility, including alt text writing rules, decorative image treatment, and contrast ratio requirements.

Responsive Images Guide - srcset, sizes, and picture Element Best Practices

Implement responsive images that serve optimal file sizes for every device. Master srcset resolution switching, sizes viewport hints, picture element art direction, and automated build-time generation.

SVG Fundamentals and Practical Usage - From Vector Basics to Animation

Complete guide to SVG from basic structure to practical applications. Learn paths, shapes, text, filters, animation implementation, and optimization techniques for web development.

Dark Mode Image Implementation Guide - Switching with picture Element and CSS

Learn how to implement dark mode-optimized images. Covers picture element media attributes, CSS prefers-color-scheme, SVG color inversion, and other practical techniques.

Implementing Before/After Image Comparison Sliders - UI Design and Optimization

Build image before/after comparison sliders with HTML, CSS, and JavaScript. Covers responsive design, touch support, accessibility, and performance optimization techniques.

Embedding Images with Data URIs - Base64 Encoding Mechanics and Best Practices

Learn how Data URI scheme embeds images directly in HTML/CSS. Understand Base64 encoding mechanics, performance implications, appropriate use cases, and when to avoid inline images.

Related Terms