2

I mainly use canvas where this isn't an issue but this is for a HTML project.

I have a png that is going to be a button and I want the transparent area of the png not to be picked up on mouse events. HTML deals with elements as rectangles, so is this even possible?

nontechguy
  • 751
  • 5
  • 21

1 Answers1

2

You can use an Image Map to define the clickable area of the image.

http://www.yourhtmlsource.com/images/imagemaps.html

Here is the gist of the link:

  1. Create the map code using a tool like https://www.image-maps.com/
  2. Use the ismap and usemap attributes on your img tag:

    <img src="media/image1.jpg" ismap="ismap" usemap="#metroid">
    

    metroid is the name of the map as specified in the map tag.

Please note that this approach creates clickable areas that act as a link. From your question, I assume that this is what you want as you were asking about a button.

If you need to react to more mouse events, you can use the approach outlined in Is it possible to style a mouseover on an image map using CSS?

Community
  • 1
  • 1
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443