SVG Anchor Tag: Linking Shapes and Elements

Wrapping shapes inside an SVG <a> (anchor) element turns vector graphics into interactive hyperlinks, allowing users to navigate to internal anchors, external URLs, or trigger browser actions upon clicking. This article explores how the SVG anchor element functions, how it differs from standard HTML links, and how it handles interactivity, styling, and geometry.

The primary function of the SVG anchor element is to establish a hyperlink context for all child vector elements nested within it, including <rect>, <circle>, <path>, <polygon>, and <text>. Unlike standard HTML where links generally wrap rectangular block or inline boxes, the SVG <a> element adopts the exact geometric boundaries of its child elements. The clickable hit area corresponds directly to the rendered fill and stroke of the enclosed shapes rather than a generic bounding box.

To define the link destination, the modern standard uses the standard href attribute:

<svg viewBox="0 0 100 100">
  <a href="https://example.com" target="_blank">
    <circle cx="50" cy="50" r="40" fill="blue" />
  </a>
</svg>

In older SVG specifications (SVG 1.1), the namespace attribute xlink:href was required. While modern browsers fully support standard href, combining both or using standard href ensures compatibility across modern renderers.

Beyond navigation, the anchor element provides several key capabilities: