79541195

Date: 2025-03-28 10:43:19
Score: 0.5
Natty:
Report link

Wrap your entire element in an tag (if you want a link) or a (if you want to trigger a JavaScript function). To make the entire SVG clickable regardless of its shape, set pointer-events: all; on the element itself (or on a container element around it). This will ensure clicks register even in the transparent areas within the SVG's bounding box.

React Example (Link):

import React from 'react';

function ClickableSVG() {
  return (
    <a href="https://www.stackoverflow.com" style={{ display: 'block' }}> 
      <svg style={{ pointerEvents: 'all', width: '200px', height: '200px' }} 
viewBox="0 0 100 100">

        {/* Your SVG path here */}

        <path d="M50,10 L90,90 L10,90 Z" fill="blue" /> {/* Example shape */}
      </svg>
    </a>
  );
}

export default ClickableSVG;

React Example (Button):

import React from 'react';

function ClickableSVG() {
  const handleClick = () => {
    alert('SVG Clicked!');
  };

  return (
    <button onClick={handleClick} style={{ display: 'block', padding: 0, 
border: 'none', background: 'none' }}>
      <svg style={{ pointerEvents: 'all', width: '200px', height: '200px' }} 
viewBox="0 0 100 100">

        {/* Your SVG path data here */}

        <path d="M50,10 L90,90 L10,90 Z" fill="blue" /> {/* Example shape */}
      </svg>
    </button>
  );
}

export default ClickableSVG;
Reasons:
  • Blacklisted phrase (1): stackoverflow
  • Long answer (-1):
  • Has code block (-0.5):
  • Low reputation (1):
Posted by: Sandro Tushurashvili