SVG shapes are real DOM nodes. You can style them with CSS (:hover, :focus-visible), listen for click / keydown in JavaScript, and control hit testing with pointer-events.
For navigation, prefer SVG links. For toggles and app actions, use scripts plus accessible names (aria-label or <title>).
Baseline · Widely available.
Pointer events, CSS hover, and DOM event listeners on SVG work across modern browsers since about July 2015.
Prefer listeners over onclick.
Inline onclick works but is harder to maintain. Use addEventListener in these lessons.
SVG-only pointer-events values.
Keywords like visiblePainted, fill, and stroke are for SVG. Using them on HTML is limited / experimental.
Syntax
Syntax
interactive.html
<svg width="200" height="140" viewBox="0 0 200 140"
xmlns="http://www.w3.org/2000/svg"
role="img" aria-label="Clickable rectangle demo">
<rect id="btn" x="40" y="30" width="120" height="80" rx="10"
fill="#2563eb" tabindex="0" role="button"
aria-label="Toggle color" />
</svg>
<script>
var btn = document.getElementById("btn");
function toggle() {
var blue = btn.getAttribute("fill") === "#2563eb";
btn.setAttribute("fill", blue ? "#ea580c" : "#2563eb");
}
btn.addEventListener("click", toggle);
btn.addEventListener("keydown", function (e) {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
toggle();
}
});
</script>
Pattern: focusable shape + name + same action for pointer and keyboard.
Most “dead click” bugs are hit areas, overlays, or missing keyboard wiring.
hit area
Empty paint is hard to click
Default hit testing follows painted fill/stroke. fill="none" with no stroke may ignore clicks unless you adjust pointer-events.
text
Labels steal clicks
Set pointer-events="none" on decorative <text> so the parent control receives the event.
a11y
Click-only is incomplete
Add tabindex="0", a name, and Enter/Space handling for button-like shapes.
nav
Use links for URLs
If the goal is navigation, use an SVG <a href> instead of a fake button script. See SVG Links.
Compatibility
Browser Support
SVG DOM events, CSS hover, and pointer-events are supported in every modern browser.
✓ Baseline · Widely available
SVG interactivity
Widely available since about July 2015. Chrome, Firefox, Safari, Edge, and Opera support click/keyboard listeners on SVG shapes and the pointer-events attribute. Prefer modern browsers for :focus-visible polish.
96%Global support (approx.)
Google ChromeAll modern
Full support
Mozilla FirefoxAll modern
Full support
Apple SafariAll modern
Full support
Microsoft EdgeAll modern
Full support
OperaAll modern
Full support
Samsung InternetAll modern
Full support
Internet Explorer9+
Partial
Events + pointer-eventsWidely supported
Bottom line: Safe for production UI icons and charts in modern browsers. Always test keyboard focus and touch hit areas.
Default SVG hit testing behaves like visiblePainted when pointer-events is unset.
Frequently Asked Questions
Select the element (by id or class) and add a click listener in JavaScript. For buttons, also add tabindex="0", role="button", aria-label, and handle Enter/Space.
Yes. Target the shape with CSS and change fill, stroke, opacity, or transform on :hover and :focus-visible.
It controls whether an element can be a pointer target. Use none to let clicks pass through; fill or stroke to limit hit areas.
Set tabindex="0", provide an accessible name, and listen for keydown so Enter and Space run the same action as click.
Check overlays, pointer-events, empty hit areas (fill none with no stroke), and that the listener runs after the SVG is in the DOM.
Use <a href> inside SVG for real navigation. Use scripts and roles when the shape toggles state or runs an app action.