admin管理员组文章数量:1356077
I have an issue making a clickable svg image in React. My image has a non-ordinary shape, that's why it's difficult for me. I want to know if it's possible to make all image clickable. Maybe it's can be solved in Html/Css
I have an issue making a clickable svg image in React. My image has a non-ordinary shape, that's why it's difficult for me. I want to know if it's possible to make all image clickable. Maybe it's can be solved in Html/Css
Share Improve this question asked Mar 28 at 8:27 Roman SorokaRoman Soroka 71 bronze badge1 Answer
Reset to default 1Wrap your entire <svg>
element in an <a>
tag (if you want a link) or a <button>
(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" 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;
本文标签: htmlHow to use svg image as a button in ReactStack Overflow
版权声明:本文标题:html - How to use svg image as a button in React - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744048474a2581978.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论