0

I have a button inside a link, how can I make it so that clicking on the button will not cause a redirect, but clicking outside the button will.

<a href="http://google.com" class="block">
  <button>Don't redirect on click</button>
</a>

Is it possible to do with css only?

See here: https://jsfiddle.net/d4mr7mm6/

Vic
  • 8,261
  • 6
  • 42
  • 55
  • 2
    That's invalid markup. Interactive content is not allowed within `a` elements, and `button` is interactive content. – zzzzBov Jan 08 '16 at 19:29
  • I mean, it doesn't have to be a button, feel free to change it to a `span` if that helps you solve the issue – Vic Jan 08 '16 at 19:31
  • Consider using positioning to have the button *appear* over the link, alternatively, a JS callback that calls `stopPropagation` will prevent the link from being clicked. There are many ways of doing this, although the UX is questionable. – zzzzBov Jan 08 '16 at 19:34
  • Trying to solve it without js – Vic Jan 08 '16 at 19:36

3 Answers3

1

This will provide what you are looking for. However, you will need to style the disabled button to accommodate the aesthetics. As said before in a previous post, this is not recommended.

<a href="http://google.com" class="block">
  <button type="button" disabled>Click Me!</button>
</a>

https://jsfiddle.net/d4mr7mm6/1/

Andrew
  • 56
  • 6
0

Just add a class inside you'r button with the following attributes :

<a href="http://google.com" class="block">
  <button class="no-redirect">Don't redirect on click</button>
</a>

.no-redirect{
   pointer-events: none;
   cursor: default;
}

see : Disable link using css

Also, it's illegal to nest interactive element inside an anchor.

Community
  • 1
  • 1
wmehanna
  • 394
  • 3
  • 15
-2

It isn't a good html structure, but if you want to stay with this structure I think this will work:

<a href="http://google.com" class="block" onclick="event.preventDefault();">
  <button>Don't redirect on click</button>
</a>

Similar answers here:

Community
  • 1
  • 1