Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Link entire table row? [duplicate]

Tags:

html

css

I know it is possible to link an entire table cell with CSS.

.tableClass td a{    display: block; } 

Is there a way to apply a link to an entire table row?

like image 246
santa Avatar asked Feb 05 '11 03:02

santa


People also ask

How do I link to an entire 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.

Can a table row be a link?

Allowing a table row to be a link is not as simple as one might think. This article explains what the main issue is with making links out of table rows, provides some solutions to the problem, showcase some real-world examples, and question whether having a table row as a link is something that should even be done.

How do I make a data table clickable?

You can put the anchor inside the td and then set the anchor to display:block and it will fill the table cell. e.g. However if your design is more complicated than that then you can't put block level elements inside that anchor. You would then need to use javascript for IE6 as it only understands hover on anchors.


2 Answers

I agree with Matti. Would be easy to do with some simple javascript. A quick jquery example would be something like this:

<tr>   <td><a href="http://www.example.com/">example</a></td>   <td>another cell</td>   <td>one more</td> </tr> 

and

$('tr').click( function() {     window.location = $(this).find('a').attr('href'); }).hover( function() {     $(this).toggleClass('hover'); }); 

then in your CSS

tr.hover {    cursor: pointer;    /* whatever other hover styles you want */ } 
like image 121
Jeff Avatar answered Sep 29 '22 21:09

Jeff


Use the ::before pseudo element. This way only you don't have to deal with Javascript or creating links for each cell. Using the following table structure

<table>   <tr>     <td><a href="http://domain.tld" class="rowlink">Cell</a></td>     <td>Cell</td>     <td>Cell</td>   </tr> </table> 

all we have to do is create a block element spanning the entire width of the table using ::before on the desired link (.rowlink) in this case.

table {   position: relative; }  .rowlink::before {   content: "";   display: block;   position: absolute;   left: 0;   width: 100%;   height: 1.5em; /* don't forget to set the height! */ } 

demo

The ::before is highlighted in red in the demo so you can see what it's doing.

like image 33
Elliott Avatar answered Sep 29 '22 20:09

Elliott