How to Build Interactive Image Annotations & Visual Markup in CSS & SVG
Interactive image annotations are built by positioning coordinate markers inside a relative container using percentage values (e.g. top: 35%; left: 62%) rather than static pixels. This ensures annotations remain anchored to exact image features when the layout resizes across mobile and desktop. SVG overlay paths provide resolution-independent callout arrows, rough sketch circles, and connectors, while accessible popovers present rich contextual metadata on click or hover.
Step 1: Percentage-Based Coordinate Architecture for Responsive Pins
Static pixel offsets break as soon as an image scales on different screens. Store pin coordinates as percentages (0% to 100%) and position pins with CSS transform: translate(-50%, -50%) so the center of the marker aligns perfectly over the target pixel.
.annotation-canvas {
position: relative;
display: inline-block;
width: 100%;
}
.annotation-canvas img {
display: block;
width: 100%;
height: auto;
}
.annotation-pin {
position: absolute;
transform: translate(-50%, -50%);
width: 28px;
height: 28px;
border-radius: 50%;
background: var(--accent);
color: #ffffff;
display: flex;
align-items: center;
justify-content: center;
font-weight: 700;
cursor: pointer;
z-index: 5;
}
Step 2: Drawing Curved Connector Arrows with SVG Paths
Overlay an SVG canvas on top of the image container with viewBox="0 0 100 100" preserveAspectRatio="none". This allows SVG bezier paths and marker heads to scale dynamically with the underlying raster photo.
<!-- Vector overlay matching container dimensions -->
<svg class="annotation-svg-layer" viewBox="0 0 100 100" preserveAspectRatio="none" style="position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none;">
<defs>
<marker id="arrowhead" markerWidth="6" markerHeight="6" refX="5" refY="3" orient="auto">
<polygon points="0 0, 6 3, 0 6" fill="#6366f1" />
</marker>
</defs>
<!-- Responsive Bezier connector line from (20%, 30%) to (45%, 60%) -->
<path d="M 20 30 Q 30 50 45 60" fill="none" stroke="#6366f1" stroke-width="0.8" stroke-dasharray="1.5 1.5" marker-end="url(#arrowhead)" />
</svg>
Step 3: Interactive Popovers with Accessible Keyboard Focus
Ensure each annotation pin is rendered as an accessible HTML <button> with aria-expanded and aria-controls. Use CSS focus-visible outlines so keyboard users can tab through markup points seamlessly.
<div class="annotation-pin-wrapper" style="top: 35%; left: 62%;">
<button type="button" class="annotation-pin" aria-expanded="false" aria-label="Pin 1: Optical Sensor" id="pin-1" onclick="togglePopover(this)">
1
</button>
<div class="annotation-card" role="region" aria-labelledby="pin-1">
<h4>Optical Sensor Module</h4>
<p>High-speed telemetry unit with dual IR transmitters.</p>
</div>
</div>
Step 4: Saving and Exporting Coordinate Datasets as JSON
Store user markup in clean JSON format specifying the relative percentage coordinates, title, author, and timestamp for serialization to a REST API or local storage.
[
{
"id": "ann-101",
"xPercent": 34.5,
"yPercent": 61.2,
"label": "Thermal Exhaust",
"description": "Inspect for micro-cracks before thermal cycle testing.",
"color": "#ec4899",
"timestamp": "2026-11-18T19:30:00Z"
}
]
Next-Level Addons: Elevating Your Implementation
Take this technique further by combining it with complementary design token studios and animation tools:
Here's my golden rule when implementing this in production: test across multiple devices and browsers early. Keep your DOM hierarchy shallow, optimize assets with modern formats (like WebP/AVIF), and verify keyboard navigation for accessibility.
Frequently Asked Questions
Practical answers to common implementation questions, browser quirks, and performance tips.
Why should image annotation coordinates always be stored in percentages rather than pixels?
If you store coordinates in static pixels (e.g. top: 250px), the pin will drift away from its intended visual target whenever the image resizes on a smaller screen or higher-density display. Percentage coordinates (e.g. top: 42.5%) scale proportionally with the image aspect ratio.
How do I make image annotations keyboard-accessible for screen readers?
Render each pin as a native HTML <button> element, provide descriptive aria-label text (e.g. "Pin 1: Engine manifold"), and connect the popup card using aria-expanded and aria-controls.
How do curved SVG connector lines stay anchored to moving image pins during resize?
By setting the SVG overlay viewBox="0 0 100 100" preserveAspectRatio="none" and defining path endpoints using relative percentage coordinate numbers, the vector lines stretch and scale in exact unison with the HTML pins.
What is the best way to handle annotations when an image uses object-fit: cover or contain?
Avoid using object-fit: cover on annotated images because the browser crops variable regions depending on the container ratio. Instead, wrap the image in a tight display: inline-block wrapper where the natural image dimensions determine the container size.
Can annotations be exported into PDF or shared documentation images?
Yes. By rendering the background image and SVG annotation overlay onto an HTML5 <canvas> context, you can export a composite flattened PNG or JPEG via canvas.toDataURL().