Clickable areas on image

I need clickable areas on an image. How can I do that? <map> and <area> doesnt seem to fit the usecase I have.
65 Replies
Wonderbear
WonderbearOP2w ago
@ἔρως
Doksuri
Doksuri2w ago
you could try an svg (full transparent) on top of the image
Chris Bolson
Chris Bolson2w ago
Buttons with opacity:0
Wonderbear
WonderbearOP2w ago
Okay so if I want a clickable circular area, then I would add a <circle> element to that position and then listen to the click event? Sure thats also possible Svg would help with positioning though
Doksuri
Doksuri2w ago
everything is possible... event divs 🤣 you just need to use the best tool
Chris Bolson
Chris Bolson2w ago
If it is for user clicking it really should be a button of some sort
Wonderbear
WonderbearOP2w ago
So no svg at all and only buttons over the image?
ἔρως
ἔρως2w ago
buttons wont work would need to be links
Wonderbear
WonderbearOP2w ago
I think @Chris means buttons without svg Just simply buttons
Chris Bolson
Chris Bolson2w ago
I haven’t read what was discussed in the chit-chat thread so I may have missed something.
Wonderbear
WonderbearOP2w ago
I just have a jpg image and this image must have clickable areas preferrably with hover effect too
Jochem
Jochem2w ago
the quick and dirty rule to choose between a button and a link is that buttons do something, and links take you somewhere. so if your clickable area navigates you to a new page, it should a a link if it just updates a counter or adds something to your cart, it should be a button
Wonderbear
WonderbearOP2w ago
Yeah link is defo not what I want, thats why I didnt want to use <map> and <area>
Chris Bolson
Chris Bolson2w ago
What will happen when you click on the areas?
Wonderbear
WonderbearOP2w ago
Well clicking there should actually create a checkmark So basically a "you have selected this area of the image"
Chris Bolson
Chris Bolson2w ago
Ok, so the clickable element has to have the shape of the area clicked. SVG does seem to be the way to go then.
Wonderbear
WonderbearOP2w ago
Yeah but the shape is always a circle
Chris Bolson
Chris Bolson2w ago
In that case you “could” use a button (or, better still, a checkbox)
Jochem
Jochem2w ago
I'd suggest a label pointing to a checkbox/radio-button
Wonderbear
WonderbearOP2w ago
Its basically this
No description
Wonderbear
WonderbearOP2w ago
You can check the numbers Damn thats a big brain move because thats exactly what should happen Clicking on it actually selects a checkbox in a list below the image So I just have to position the labels relative to the image at the given coordinates Thank you everyone
Jochem
Jochem2w ago
for future reference, if you'd included that in the problem description, you'd probably have had an answer much sooner
Wonderbear
WonderbearOP2w ago
Yeah I didnt realize that this was part of the problem
ἔρως
ἔρως2w ago
you have to use a <foreignObject> for this, by the way
Wonderbear
WonderbearOP2w ago
It just clicked for me when you said checkboxes, then I realized I have checkboxes foreignObject?
ἔρως
ἔρως2w ago
yes, you cant put html inside an svg you have to use a foreignObject tag to then put html
Wonderbear
WonderbearOP2w ago
Im not gonna use svg at all
ἔρως
ἔρως2w ago
alright, how will you position the checkboxes then?
Wonderbear
WonderbearOP2w ago
css?
ἔρως
ἔρως2w ago
yeah, but you have to be extremely careful if 2 are too close, one will be on top of the other
Wonderbear
WonderbearOP2w ago
What do you mean
ἔρως
ἔρως2w ago
if you position based on percentages, you need to check if they will be on top of eachother on mobile
Wonderbear
WonderbearOP2w ago
I want to position based on pixel
Chris Bolson
Chris Bolson2w ago
Can the image not be converted into an SVG? Judging from the small screen capture your provided earlier it looks like a drawing.
Wonderbear
WonderbearOP2w ago
Its too complex to be converted into svg Actually you might be onto something with % positioning
ἔρως
ἔρως2w ago
pixel positions will have even worse issues: you will have to interpolate the positions for smaller/larger screens if the screen is 2-3 checkboxes smaller/larger, your positions are all wrong
Wonderbear
WonderbearOP2w ago
Yeah, the thing is, I already have a working example of this app and I am currently experimenting with the apps provided data and it turned out the x and y coordinates are % values 😅
ἔρως
ἔρως2w ago
which work perfectly fine you might have some bugs with sub-pixel rendering
Wonderbear
WonderbearOP2w ago
I already have a prototype 😁 The label idea was lit
ἔρως
ἔρως2w ago
are you worried about accessibility?
Wonderbear
WonderbearOP2w ago
Yes
ἔρως
ἔρως2w ago
labels arent focusable
Wonderbear
WonderbearOP2w ago
Thats a concern for tabbing, right?
ἔρως
ἔρως2w ago
you can skip the lables and just use an input with area-label or whatever it is no yed yes but not just that
Wonderbear
WonderbearOP2w ago
I can use two inputs?
ἔρως
ἔρως2w ago
no, just 1
Wonderbear
WonderbearOP2w ago
What about the checkboxes below the image
ἔρως
ἔρως2w ago
checkboxes can be styled with ::before and ::after you want to show the checkboxes there?
Wonderbear
WonderbearOP2w ago
Yes Thats going to be a list with names and stuff inside an actual <form>
ἔρως
ἔρως2w ago
oh, well, that is a totally different thing
Wonderbear
WonderbearOP2w ago
So the labels idea was a perfect hit Except for the focus problem of course
ἔρως
ἔρως2w ago
you can use the checkbox hack then
Wonderbear
WonderbearOP2w ago
Which is?
ἔρως
ἔρως2w ago
throw all the checkboxes at the top, then style the elements based on which checkbox is checked you can even fake a checkbox on the list it is a terrible hack but works
Wonderbear
WonderbearOP2w ago
Why isnt there a simple "focusable" attribute for the label? 🙄
Chris Bolson
Chris Bolson2w ago
focus is for form elements (and a couple of other elements such as links and iframes). If they took the focus that would mean a double focus for the same element (the label and the related form field). I don't quite understand why you want them to get focus in your use case 🤔
ἔρως
ἔρως2w ago
accessibility the labels arent focusable
13eck
13eck2w ago
If it works is it really that terrible? :p
ἔρως
ἔρως2w ago
lol yes yesterday, ive put a <details> inside a <button> to make a shoddy bootstrap-like dropdown it works and it is a nightmare (ive redone it since, by the way)
13eck
13eck2w ago
ἔρως
ἔρως2w ago
🤣
13eck
13eck2w ago
Hey, I needed to post something so the thread appeared in the sidebar! did you not see the :p? Denoting humour?
ἔρως
ἔρως2w ago
i did, hence the 🤣 but i just wanted to shame myself for making it work yesterday 😦 (lets just say there was a lot of pointer-events everywhere)

Did you find this page helpful?