83

I have the following:

<td>
  some text
  <div>a div</div>
</td>

I'd like to make the entire <td>...</td> a hyperlink. I'd prefer without the use of JavaScript. Is this possible?

Abel
  • 56,041
  • 24
  • 146
  • 247
aeq
  • 10,377
  • 15
  • 45
  • 46
  • Does this answer your question? [How to make entire td a link?](https://stackoverflow.com/questions/6459806/how-to-make-entire-td-a-link) – Flimm Feb 10 '22 at 17:10

8 Answers8

107

Yes, that's possible, albeit not literally the <td>, but what's in it. The simple trick is, to make sure that the content extends to the borders of the cell (it won't include the borders itself though).

As already explained, this isn't semantically correct. An a element is an inline element and should not be used as block-level element. However, here's an example (but JavaScript plus a td:hover CSS style will be much neater) that works in most browsers:

<td>
  <a href="http://example.com">
    <div style="height:100%;width:100%">
      hello world
    </div>
  </a>
</td>

PS: it's actually neater to change a in a block-level element using CSS as explained in another solution in this thread. it won't work well in IE6 though, but that's no news ;)

Alternative (non-advised) solution

If your world is only Internet Explorer (rare, nowadays), you can violate the HTML standard and write this, it will work as expected, but will be highly frowned upon and be considered ill-advised (you haven't heard this from me). Any other browser than IE will not render the link, but will show the table correctly.

<table>
    <tr>
        <a href="http://example.com"><td  width="200">hello world</td></a>
    </tr>
</table>
Community
  • 1
  • 1
Abel
  • 56,041
  • 24
  • 146
  • 247
  • 7
    your first suggested solution does not work if the td has padding, as the div will only extend as far as the padding allows – Max Jun 01 '14 at 14:31
  • @max: while I wrote this a loooong time ago, I would say that padding is part of the box of the `td`. You should remove the padding if you want to add this to the hyperlinked region. An alternative fix would be to add a JS snippet and change the hover-cursor to `pointer`, which adds usability and does not hamper indexing by search bots. – Abel Jun 04 '14 at 14:44
  • I don't follow what you mean by changing the hover cursor - how would that extend the size of the link? The problem with removing the padding is.. that you are removing the padding - it messes up the layout. I guess I could remove the padding of the td and add it to the div, but it feels so dirty :/ – Max Jun 04 '14 at 15:36
  • 1
    Overwriting the padding of the td with 0 and setting the padding of the inner div accordingly was the only solution that worked for me, but I am really not happy with it. If someone finds a more elegant way, please update. – Max Jun 05 '14 at 18:34
  • @Max, it looks like a new question, you may want to open up a new one at SO. The short answer, the way I see it, is JS + CSS (i.e. with jQuery, can easily be done), to change the cursor and the react to the click-event using the url already there in the child-a-href. This means no change to the padding. Another alternative is remove the padding on the TD as you did, and add the padding to the inner block element. But I think the jQuery option is easier to implement. – Abel Jun 06 '14 at 13:22
  • Hmmm,,,, I like this solution. It's simple and easy to use. But I'm finding that the text in the is not correctly aligned. Setting display:block; moves it up too much. How can I center it? – Shane Gannon Sep 30 '15 at 14:29
  • Asked the question and found line-height to be the solution. Answer is here http://stackoverflow.com/questions/32869987/how-to-align-the-text-in-a-td-which-has-been-setup-as-a-hyperlink/32870306#32870306 – Shane Gannon Sep 30 '15 at 15:58
  • 1
    There is a more correct answer : Some text – Jean-Paul Oct 07 '15 at 08:43
  • 1
    @jp, that is not the same as making it a "link". You make it clickable and run a javascript that changes the location, using `a` makes it an actual link, semantically and lexically. – Abel Oct 07 '15 at 08:51
  • Help! Read all this and tried everything this one and the one below. I still cannot get the link to work. This td is right next to one that has more height, but even adjusting the padding manually, no link.I have:
    additional charges more info
    – Betty Mock Sep 30 '16 at 23:15
  • @BettyMock: most certainly that is then caused by other code or parts not shown here. Please ask a new question (see [ask]), with a complete reproducible example, then you have a better chance to get the most effective help. – Abel Oct 01 '16 at 01:13
  • In HTML5, your example is actually valid, as `` can be used as either a block or inline element. – Ferrybig Nov 19 '18 at 09:19
  • @ferrybig, true, and that was already possible in html4, or more accurately, css2, but it's not allowed as child of `tr` as far as I know – Abel Nov 19 '18 at 11:11
36

Use this code. It expands an <a> to fill the cell horizontally. To fill vertically, use the height property as well.

td a {
  width: 100%;
  display: block;
}

td {
  /* Cell styles for demonstration purposes only */
  border: 1px solid black;
  width: 10em;
}
<table><tr>
  <td>
    <a href="http://example.com">
      Hello World
    </a>
  </td>
</tr></table>
GKFX
  • 1,386
  • 1
  • 11
  • 30
Uzair Bin Nisar
  • 675
  • 5
  • 7
  • 10
    This appears to be the simplest solution. Note that if the TD has padding, that area is unclickable. Set padding: 0; on the TD and padding: 3px; (or whatever) on the A. Also, width and height of the A has to be auto, not 100%. – Per Lindberg Feb 21 '14 at 11:07
  • For text links in Chrome, I had to add a line-height property to the CSS for td a. I set it to the full height of the container. It'll only work for one-liners. – Qaz Mar 10 '16 at 12:43
16

I would recommend using an actual anchor element, and set it as block.

<div class="divBox">
    <a href="#">Link</a>
</div>

.divBox
{
    width: 300px;
    height: 100px;
}
.divBox a
{
    width: 100%;
    height: 100%;
    display: block;
}

This will set the anchor to the same dimensions of the parent div.

Dustin Laine
  • 37,935
  • 10
  • 86
  • 125
5

Here is my solution:

<td>
   <a href="/yourURL"></a>
   <div class="item-container">
      <img class="icon" src="/iconURL" />
      <p class="name">
         SomeText
      </p>
   </div>
</td>

(LESS)

td {
  padding: 1%;
  vertical-align: bottom;
  position:relative;

     a {
        height: 100%;
        display: block;
        position: absolute;
        top:0;
        bottom:0;
        right:0;
        left:0;
       }

     .item-container {
         /*...*/
     }
}

Like this you can still benefit from some table cell properties like vertical-align. (Tested on Chrome)

amp
  • 11,754
  • 18
  • 77
  • 133
0

I'd like to make the entire td a hyperlink. I'd prefer without javascript. Is this possible?

That's not possible without javascript. Also, that won't be semantic markup. You should use link instead otherwise it is a matter of attaching onclick handler to <td> to redirect to some other page.

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
0

This might be the most simple way to make a whole <td> cell an active hyperlink just using HTML.

I never had a satisfactory answer for this question, until about 10 minutes ago, so years in the making #humor.

Tested on Firefox 70, this is a bare-bones example where one full line-width of the cell is active:

<td><a href=""><div><br /></div></a></td>

Obviously the example just links to "this document," so fill in the href="" and replace the <br /> with anything appropriate.

Previously I used a style and class pair that I cobbled together from the answers above (Thanks to you folks.)

Today, working on a different issue, I kept stripping it down until <div>&nbsp;</div> was the only thing left, remove the <div></div> and it stops linking beyond the text. I didn't like the short "_" the &nbsp; displayed and found a single <br /> works without an "extra line" penalty.

If another <td></td> in the <tr> has multiple lines, and makes the row taller with word-wrap for instance, then use multiple <br /> to bring the <td> you want to be active to the correct number of lines and active the full width of each line.

The only problem is it isn't dynamic, but usually the mouse is closer in height than width, so active everywhere on one line is better than just the width of the text.

Daddy-o
  • 1
  • 1
0

The <td><a></a></td> methods left the cell-padding of the <td> unclickable unfortunately.

This method makes the whole TD clickable, all the way up to the borders.

Combining a few commenter's suggestions,

  • you set the <td>'s padding to 0, and then

  • embed an <a>-enclosed <div> with the desired cell padding. The padding is thus part of the <a href>.

Like this:

HTML:

<td class="fulltd">
    <a class="fulltd" href="https://soundcloud.com/demis-john">
    <div class="fulltd">
        Link To Soundcloud
    </div>
    </a>
</td>

CSS:

/* Remove <td>'s interior cell-padding */
td.fulltd {
    padding: 0em 0em 0em 0em;
}

/* Make the <a> fill the whole td */
td a.fulltd {
    display: block;
    width: 100%;
    height: 100%;
}

/* Let the <div> provide the clickable cell-padding */
div.fulltd {
    height:100%;
    width:100%;
    padding: 1.0em 1.0em 1.0em 1.0em;
}

(Kudos to these commenters for helping me finally solve it! Per Lindberg, Max)

Demis
  • 5,278
  • 4
  • 23
  • 34
-6

You can creat the table you want, save it as an image and then use an image map to creat the link (this way you can put the coords of the hole td to make it in to a link).

RedBelly
  • 89
  • 1
  • 2
  • 7
  • Although the final result might be working, this is a rather complicated and really bad way of achieving it. It also breaks when the image width might change with the screen size. – McVenco Nov 08 '17 at 12:12