SVG pointer-events on Transparent Areas

The pointer-events property in Scalable Vector Graphics (SVG) determines how graphical elements respond to mouse interactions, clicks, and touch events based on their rendering state and transparency. Unlike standard HTML elements where interactions generally apply to a bounding box, SVG provides granular control over whether transparent fills, invisible strokes, or completely unpainted areas capture pointer events. Understanding these specific values allows developers to create precise hit targets, pass-through overlays, and transparent interactive regions without relying on visual workarounds.

How SVG Hit Testing Works

In SVG, an element consists of geometry defined by a path, which can have a fill (the interior) and a stroke (the outline). By default, whether an area responds to mouse events depends on three factors: 1. The visibility attribute (visible, hidden, or collapse). 2. The presence of paint (fill or stroke set to an actual color rather than none). 3. The element’s pointer-events value.

Standard CSS offers basic values like auto and none, but SVG extends this with specialized values designed to target fills, strokes, and transparent regions independently.

Key pointer-events Values and Transparency

1. visiblePainted (Default)

This is the default SVG behavior. Mouse events only fire on areas that are both visible and explicitly painted. * Transparent Areas: If fill="none" or stroke="none", those areas ignore mouse interactions, allowing clicks to pass through to elements underneath. * Opacity: Elements with opacity="0" or fill-opacity="0" are still considered “painted” and will capture clicks.

2. all

The all value causes the element to capture pointer events across its entire interior fill and stroke area, regardless of whether they are painted or visible. * Transparent Areas: Even if fill="none", stroke="none", or visibility="hidden", clicking anywhere within the geometry triggers an event. This is ideal for creating invisible hit zones.

3. fill

The fill value directs hit testing solely to the interior of the shape. * Transparent Areas: The interior captures events even if fill="none" or opacity="0". Stroke areas only capture events if they overlap the fill.

4. stroke

The stroke value directs hit testing solely to the shape’s outline. * Transparent Areas: The stroke path captures pointer events even if stroke="none". The interior fill area lets mouse events pass through.

5. visible

The visible value triggers events over the entire interior and stroke as long as visibility="visible". * Transparent Areas: If the element is visible, it captures events across its entire shape regardless of whether fill or stroke is set to none.

6. painted

Similar to visiblePainted, but it ignores the visibility property. * Transparent Areas: Unpainted regions (fill="none") do not capture events, but painted areas capture events even if visibility="hidden".

7. none

The element never receives pointer events. All interactions pass straight through to whatever lies beneath the SVG element.

Comparison Summary

Value fill="none" Captures Events? stroke="none" Captures Events? Requires visibility: visible?
visiblePainted No No Yes
painted No No No
visible Yes Yes Yes
visibleFill Yes No Yes
visibleStroke No Yes Yes
fill Yes No No
stroke No Yes No
all Yes Yes No
none No No N/A

Practical Applications