0

It is possible to make the child of a link, instead of the link itself focusable. But is it also possible to follow the parent link on enter? (without javascript) So in the example below I want to go to Google if I hit enter if the div is focussed.

.padded-button {
  display: inline-block;
  padding: 20px;
  cursor: default;
}
.button {
  padding: 10px;
  background-color: blue;
  display: inline-block;
  color: #fff;
  cursor: pointer;
}
<a href="http://google.com" class="padded-button" tabindex="-1">
    <div class="button"  tabindex="0">button</div>
</a>

Note: I'm trying to create a button that acts like a regular button if you use it with a mouse / keyboard, but have a bigger tap area for touch.

klaasjansen
  • 537
  • 1
  • 6
  • 20

4 Answers4

2

Not entirely possible without js. You can apply styles to fake focus on the child element, ie:

.padded-button:focus .button{ outline: 3px solid red; }

But if you have actual focus on a child element of an anchor tag, pressing enter will not bubble up to the parent.

https://jsfiddle.net/Lentqf4u/

Serg Chernata
  • 12,280
  • 6
  • 32
  • 50
2

You can try this:

.padded-button {
  display: inline-block;
  padding: 20px;
  cursor: default;
}
.button {
  padding: 10px;
  background-color: blue;
  display: inline-block;
  color: #fff;
  cursor: pointer;
}
<form method="get" action="http://google.com" class="padded-button" >
    <button class="button" type="submit" >button</button>
</form>

Based on this: How to create an HTML button that acts like a link?

I am not sure about all browsers behaviours, though.

Community
  • 1
  • 1
er-han
  • 1,859
  • 12
  • 20
  • Sorry, this is not what I intended. Now tapping on the form element does not submit the form. Only tapping on the button does. – klaasjansen Jan 18 '17 at 14:46
0

.padded-button {
  display: inline-block;
  padding: 20px;
  cursor: default;
}
.button {
  padding: 10px;
  background-color: blue;
  display: inline-block;
  color: #fff;
  cursor: pointer;
}
.button:focus{ outline: 1px solid red; }
.button:hover{ outline: 1px solid red; }
<!--<a href="http://google.com" class="padded-button" tabindex="-1">
    <div tabindex="2" class="button" focus="true">button</div>
</a>-->
<form method="post" action="http://google.com">
    <button class="button" type="submit">button</button>
</form>
Manikandan Velayutham
  • 2,228
  • 1
  • 15
  • 22
0

Yes it is possible. The code below shows that if you click or focus the div it will redirect to the parent link. You can make the div look like a button just by altering it's CSS. Also you can increase the focusing or clicking area by increasing the height and width CSS attribute.

#btn
{
  width:100px;
    height:100px;
  background-color: red;
  }
<a href="https://www.google.co.in">
  <div id="btn">click me
    </div>
  </a>

Just make sure you don't have any other button or link inside the parent a tag. Also it is not a good practice to wrap a div inside a tag in HTML 4.01. Hope this helps.

neophyte
  • 6,540
  • 2
  • 28
  • 43