Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase the clickable area of a <a> tag button?

I have learnt from this post that always use <a> tags or <button> tags to make button. Now I'm trying to use <a> tag. My question is: is there any way to increase the tag clickable area? Say I'm using <a> in a div box. I want the whole div box to become a button. Can I change the clicking area to the whole div box? Thanks for you help.

like image 955
Ivan Wang Avatar asked Jun 18 '12 07:06

Ivan Wang


People also ask

How do I increase the clickable area of a checkbox?

Since clicking the label essentially clicks the checkbox, I would add padding or width to the label surrounding the checkbox so that it extends as far as you need your clickable area to be. Note that, for accessibility, I've added aria-label to the checkbox.

How do you make a clickable button?

The <button> tag defines a clickable button. Inside a <button> element you can put text (and tags like <i> , <b> , <strong> , <br> , <img> , etc.). That is not possible with a button created with the <input> element!


2 Answers

To increase the area of a text link you can use the following css;

a {         display: inline-block;         position: relative;        z-index: 1;         padding: 2em;         margin: -2em;  } 
  • Display: inline-block is required so that margins and padding can be set
  • Position needs to be relative so that...
  • z-index can be used to make the clickable area stay on top of any text that follows.
  • The padding increases the area that can be clicked
  • The negative margin keeps the flow of surrounding text as it should be (beware of over lapping links)
like image 103
t1m0thy Avatar answered Sep 29 '22 00:09

t1m0thy


Edit:

@t1m0thy's answer is more elegant than mine, better follow his advices.

Also, nice link proposed by @aldemarcalazans in the comments: https://davidwalsh.name/html5-buttons.


Original answer:

Use <a /> when you need a link (the a of anchor). Use <button /> when you need a button.

That said, if you really need to expand an <a />, add the CSS attribute display: block; on it. You'll then be able to specify a width and/or a height (i.e. as if it were a <div />).

like image 35
sp00m Avatar answered Sep 28 '22 23:09

sp00m