HTML link with padding and CSS style active does not work

两盒软妹~` 提交于 2019-12-05 05:57:23

With padding the text moves away from where you clicked. This is your code: http://jsfiddle.net/ctrlfrk/3KsRx/

hold down the mouse button on 'Stack' and you will see that the text moves away from under the mouse.

What are you trying to achieve? This is working the way it should. If internet explorer follows the link then it is wrong.

[Edit] Clarifying:

The real problem here is that a 'click' event only seems to fire if the mousedown event target matches the mouseup event target. When you click on the text in your example, the mousedown target is a text node which is a child of the anchor tag. This text node then moves away, so that the mouseup event target is simply the anchor tag itself.

With :hover, the text node moves away before you click, so the mousedown event target is the anchor tag, and the mouseup event is also the anchor tag, so the link is followed.

[/Edit]

I found a solution! http://jsfiddle.net/rydl/Yu6vp/3/

David is right, the problem is caused by the moving text-node. So the solution must prevent the text-node from being clicked at all. I used the anchor's :after-pseudo-element to cover the whole button, while being invisible:

a:after {
  position: absolute;
  top: 0;
  left: 0;
  content: ' ';
  height: 100%;
  width: 100%;
}

Try changing the padding with margin, As the padding is changed element position changes as well.

a {
    margin:0 0 0 35px;
}

To browsers(firefox,chrome etc) A valid mouse click is one which is pressed and released on same element.

EDIT: I cant change the behavior of browser (no solution in pure css), but you can use javascript to get this done

<a href="http://stackoverflow.com/" onmousedown="window.location=this;">StackOverflow</a>

EDIT: :hover works instead :active because of the reason as element does not change position between time pressed and time released

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!