Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make link in table cell fill the entire row height

Tags:

html

css

I have a table of data and each cell is a link. I want to allow the user to click anywhere in the table cell and have them follow the link. Sometimes the table cells are more than one line but not always. I use td a {display: block} to get the link to cover most of the cell. When there is one cell in a row that is two lines and the others are only one line the one liners don't fill the entire vertical space of the table row. Here is the sample HTML and you can see it in action here http://www.jsfiddle.net/RXHuE/:

<head> <style type="text/css">   td {width: 200px}   td a {display: block; height:100%; width:100%;}   td a:hover {background-color: yellow;} </style> <title></title> </head> <body> <table>   <tbody>     <tr>       <td>         <a href="http://www.google.com/">Cell 1<br>         second line</a>       </td>       <td>         <a href="http://www.google.com/">Cell 2</a>       </td>       <td>         <a href="http://www.google.com/">Cell 3</a>       </td>       <td>         <a href="http://www.google.com/">Cell 4</a>       </td>     </tr>   </tbody> </table> </body> 
like image 636
Brian Fisher Avatar asked Oct 19 '10 07:10

Brian Fisher


People also ask

How do I link to a whole row in a table?

Using <a> tag inside <td> One more way to make the whole row clickable is to add an <a> inside every <td> element. Along with it add hover to the row which we want to make clickable and use display: block property to anchor to make the whole row clickable.

How do you make a cell fixed height in a table?

The height of rows 'tr' in a table can be fixed very easily. This can be done by adding the height attribute in the tr tag. If the height is not specified, the height of the row changes according to the content. The height can be specified either in pixels, or percentage.


2 Answers

Set an arbitrarily large negative margin and equal padding on the block element and overflow hidden on the parent.

td {     overflow: hidden; } td a {     display: block;     margin: -10em;     padding: 10em; } 

http://jsfiddle.net/RXHuE/213/

like image 188
zobier Avatar answered Sep 18 '22 23:09

zobier


You need a small change in your CSS. Making td height:100%; works for IE 8 and FF 3.6, but it doesn't work for Chrome.

td {   width: 200px;   border: solid 1px green;   height: 100% } td a {   display: block;   height:100%;   width:100%; } 

But making height to 50px works for Chrome in addition to IE and FF

td {   width: 200px;   border: solid 1px green;   height: 50px } td a {   display: block;   height:100%;   width:100%; } 

Edit:

You have given the solution yourself in another post here; which is to use display: inline-block;. This works when combined with my solution for Chrome, FF3.6, IE8

td {   width: 200px;   border: solid 1px green;   height: 100%} td a {   display: inline-block;   height:100%;   width:100%; } 

Update

The following code is working for me in IE8, FF3.6 and chrome.

CSS

td {   width: 200px;   border: solid 1px green;   height: 100%; } td a {   display: inline-block;   height:100%;   width:100%; } td a:hover {   background-color: yellow; } 

HTML

<table>   <tbody>     <tr>       <td>         <a href="http://www.google.com/">Cell 1<br>         second line</a>       </td>       <td>         <a href="http://www.google.com/">Cell 2</a>       </td>       <td>         <a href="http://www.google.com/">Cell 3</a>       </td>       <td>         <a href="http://www.google.com/">Cell 4</a>       </td>     </tr>   </tbody> </table> 

The example lays here

like image 25
Gaurav Saxena Avatar answered Sep 19 '22 23:09

Gaurav Saxena