HTML SVG Interactivity

Beginner
9 min read
Updated: Nov 2026
5 examples
Live preview

Definition and Usage

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

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.

Attributes

Common tools for interactive SVG:

NameDescriptionTry it
tabindex="0"Makes the shape keyboard-focusableTry it
role="button"Announces a control, not just a graphicTry it
aria-label / <title>Accessible name for the controlTry it
pointer-eventsHit testing: none, fill, stroke, …Try it
CSS :hoverVisual feedback without JavaScriptTry it
addEventListenerWire click / keydown in scriptTry it

Global Attributes

Interactive SVG shapes also support the Global Attributes in HTML (for example id, class, lang, title) when used in an HTML document.

Live Preview

Click or press Enter/Space on the circle to toggle its fill. Tab to focus it first for keyboard use.

Focus ring appears on keyboard focus.

SVG

fill="#2563eb" · clicks: 0

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Click and Keyboard Toggle

Same action for mouse and Enter/Space.

HTML
<svg width="200" height="140" viewBox="0 0 200 140"
     xmlns="http://www.w3.org/2000/svg">
  <rect id="toggle" x="40" y="30" width="120" height="80" rx="10"
        fill="#2563eb" tabindex="0" role="button"
        aria-label="Toggle rectangle color" />
</svg>
<script>
  var el = document.getElementById("toggle");
  function toggle() {
    var blue = el.getAttribute("fill") === "#2563eb";
    el.setAttribute("fill", blue ? "#ea580c" : "#2563eb");
  }
  el.addEventListener("click", toggle);
  el.addEventListener("keydown", function (e) {
    if (e.key === "Enter" || e.key === " ") {
      e.preventDefault();
      toggle();
    }
  });
</script>
Try It Yourself

How It Works

  1. Mark the control with tabindex, role, and aria-label.
  2. Toggle fill inside a shared function.
  3. Listen for click for pointer users.
  4. Listen for Enter/Space so keyboard users get the same result.

Example 2 — CSS Hover Highlight

Change fill on hover with no JavaScript.

HTML
<style>
  #dot { fill: #16a34a; cursor: pointer; }
  #dot:hover, #dot:focus-visible { fill: #15803d; stroke: #0f172a; stroke-width: 3; }
</style>
<svg width="200" height="140" viewBox="0 0 200 140"
     xmlns="http://www.w3.org/2000/svg">
  <circle id="dot" cx="100" cy="70" r="45"
          tabindex="0" role="button" aria-label="Hoverable circle" />
</svg>
Try It Yourself

How It Works

  1. Give the shape an id for a CSS selector.
  2. Style :hover for pointer feedback.
  3. Add :focus-visible so keyboard focus is clear.
  4. Keep CSS minimal — only the properties you need.

Example 3 — pointer-events="none"

Let clicks pass through an overlay circle to the rect below.

HTML
<svg width="220" height="140" viewBox="0 0 220 140"
     xmlns="http://www.w3.org/2000/svg">
  <rect id="base" x="30" y="30" width="160" height="80" rx="8" fill="#0f172a" />
  <circle cx="110" cy="70" r="36" fill="#facc15" pointer-events="none" />
</svg>
<script>
  document.getElementById("base").addEventListener("click", function () {
    this.setAttribute("fill", this.getAttribute("fill") === "#0f172a" ? "#2563eb" : "#0f172a");
  });
</script>
Try It Yourself

How It Works

  1. Draw the interactive rect underneath.
  2. Place a circle on top for decoration.
  3. Set pointer-events="none" so the circle is not a hit target.
  4. Clicks reach the rect even through the yellow area.

Example 4 — Group Button with Text

Put label text inside a <g>; disable text hit testing.

HTML
<svg width="220" height="120" viewBox="0 0 220 120"
     xmlns="http://www.w3.org/2000/svg">
  <g id="chip" tabindex="0" role="button" aria-label="Blue chip">
    <rect x="40" y="30" width="140" height="60" rx="12" fill="#2563eb" />
    <text x="110" y="66" text-anchor="middle" fill="#fff"
          font-family="system-ui,sans-serif" font-size="18"
          pointer-events="none">Blue</text>
  </g>
</svg>
<script>
  var chip = document.getElementById("chip");
  function flash() {
    var r = chip.querySelector("rect");
    var on = r.getAttribute("fill") === "#2563eb";
    r.setAttribute("fill", on ? "#7c3aed" : "#2563eb");
  }
  chip.addEventListener("click", flash);
  chip.addEventListener("keydown", function (e) {
    if (e.key === "Enter" || e.key === " ") { e.preventDefault(); flash(); }
  });
</script>
Try It Yourself

How It Works

  1. Wrap shape + label in a <g> control.
  2. Set pointer-events="none" on <text> so clicks hit the group/rect.
  3. Attach listeners to the group once.
  4. Update the rect fill inside the handler.

Example 5 — Many Clickable Shapes

One script handles several circles with event delegation.

HTML
<svg id="board" width="260" height="120" viewBox="0 0 260 120"
     xmlns="http://www.w3.org/2000/svg">
  <circle class="dot" cx="50" cy="60" r="28" fill="#2563eb"
          tabindex="0" role="button" aria-label="Dot 1" />
  <circle class="dot" cx="130" cy="60" r="28" fill="#16a34a"
          tabindex="0" role="button" aria-label="Dot 2" />
  <circle class="dot" cx="210" cy="60" r="28" fill="#ea580c"
          tabindex="0" role="button" aria-label="Dot 3" />
</svg>
<script>
  document.getElementById("board").addEventListener("click", function (e) {
    if (!e.target.classList.contains("dot")) return;
    e.target.setAttribute("opacity", e.target.getAttribute("opacity") === "0.45" ? "1" : "0.45");
  });
</script>
Try It Yourself

How It Works

  1. Mark each circle with a shared class.
  2. Listen once on the parent SVG.
  3. Ignore clicks that are not on a .dot.
  4. Toggle opacity on the clicked target.

Edge Cases & Pitfalls

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.

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 Chrome All modern
Full support
Mozilla Firefox All modern
Full support
Apple Safari All modern
Full support
Microsoft Edge All modern
Full support
Opera All modern
Full support
Samsung Internet All modern
Full support
Internet Explorer 9+
Partial
Events + pointer-events Widely supported

Bottom line: Safe for production UI icons and charts in modern browsers. Always test keyboard focus and touch hit areas.

Key Takeaways

  • SVG is DOM: CSS + JS work like HTML.
  • Accessibility: tabindex, role, name, Enter/Space.
  • Hit testing: control with pointer-events.
  • Navigation? Use SVG links.

One line: focusable shape + shared click/keyboard action.

Default CSS Settings

Shapes are not interactive by default beyond normal hit testing. Useful starter rules:

defaults.css
/* Conceptual defaults / helpers */
svg [role="button"] {
  cursor: pointer;
}

svg [role="button"]:focus-visible {
  outline: 2px solid #2563eb;
  outline-offset: 2px;
}

/* SVG default pointer-events ≈ visiblePainted */

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.

Next: SVG Links

Wrap shapes in <a href> for real hyperlinks.

SVG Links →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful