I want an inline element to be easier to click. I can add padding, but I don’t want to impact other elements. I can undo that with negative margin, but it leads to odd results:
span {
padding: 5px;
margin: -5px;
background-color: red;
cursor: pointer;
}
ab<span>c</span>def
If you try to hover over it, the pointer does not show up in the whole red area but goes away to the right of the c
(where the d
is). An onClick
callback would work the same. Why does this happen, and how do I prevent it?
4
Answers
A quick fix would be to add an anchor tag around both your inline element and some surrounding elements as well.
Otherwise you could dynamically insert an absolutely-positioned element to be your click target.
I would not recommend this.
Imagine you have a text with several clickable words. And know imagine they are neighbours, vertically and/or horizontally.
The clickable areas will overlap each other.
This will not end good for UXP.
You can use a CSS class that’ll define a pseudo element
::before
which expands the clickable area without affecting the layout:You can expand the clickable area by adding before and after pseudo elements to the span.
This snippet adds a 5px transparent clickable area before and after. That way the adjacent characters are not disturbed, and it is clear which character is clickable.