Open In App

SVG pointer-events Attribute

The pointer-events attribute allows us to define whether or when an element may be the target of a mouse event. It is applied on the following elements: <a>, <circle>, <clipPath>, <defs>, <ellipse>, <foreignObject>, <g>, <image>, <line>, <marker>, <mask>, <path>, <pattern>, <polygon>, <polyline>, <rect>, <svg>, <switch>, <symbol>, <text>, <textPath>, <tspan>, <use>.

Syntax:

pointer-events = bounding-box | visiblePainted | visibleFill | 
                 visibleStroke | visible | painted | fill |
                 stroke | all | none

Attribute Values: The pointer-events attribute accepts the values mentioned above and described below:



Below examples illustrate the use of  pointer-events attribute.

Example 1:






<!DOCTYPE html>
<html>
  
<body>
    <div style="color: green;">
        <h2>
            GeeksforGeeks
        </h2>
  
        <svg viewBox="0 0 100 10" 
            xmlns="http://www.w3.org/2000/svg">
  
            <rect x="3" y="0" height="10" 
                width="10" fill="green" />
  
            <ellipse cx="8" cy="5" rx="5" ry="4" 
                fill="black" 
                pointer-events="visiblePainted" />
        </svg>
    </div>
      
    <script>
        window.addEventListener(
            'mouseup', (e) => {
                let geekColor =
                    Math.round(Math.random() *
                        0xFFFFFF)
                let fill =
                    '#' + geekColor.toString(16).
                        padStart(5, '0')
                e.target.style.fill = fill
            });
    </script>
</body>
  
</html>

Output:

Example 2:




<!DOCTYPE html>
<html>
  
<body>
    <div style="color: green;">
        <h2>
            GeeksforGeeks
        </h2>
  
        <svg viewBox="0 0 100 10" 
            xmlns="http://www.w3.org/2000/svg">
  
            <rect x="3" y="0" height="10" 
                width="10" fill="green" />
  
            <ellipse cx="8" cy="5" rx="5" 
                ry="4" fill="black" 
                pointer-events="none" />
        </svg>
    </div>
      
    <script>
        window.addEventListener(
            'mouseup', (e) => {
                let geekColor =
                    Math.round(Math.random()
                        * 0xFFFFFF)
                let fill =
                    '#' + geekColor.toString(16).
                        padStart(6, '0')
                e.target.style.fill = fill
            });
    </script>
</body>
  
</html>

Output:


Article Tags :