Anchor that covers a whole html table row

Hi,

I have an html table. I put one anchor in one cell for each row.
Please can you tell me: can I extend the width and height anchor for each whole html table row?

This is the html code I have tried but without success:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
   <head>
   <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
   <link rel="stylesheet" href="include/layout.css" type="text/css">
   <title></title>
   </head>
   <body>
      <table>
         <tr>
            <td>Cell 01</td>
            <td><a href='#'>Cell 02</a></td>
            <td>Cell 03</td>
            <td>Cell 04</td>
         </tr>
         <tr>
            <td>Cell 01</td>
            <td><a href='#'>Cell 02</a></td>
            <td>Cell 03</td>
            <td>Cell 04</td>
         </tr>

      </table>
   </body>
</html>

this is the CSS:

table {
   border-collapse: collapse;
}

td {
   border: 1px solid black;
   width: 120px;
   height: 120px;
}


table tr td a {
   border: 1px solid red;
   display: block;
   margin-left: -100px;
   height: 120px;
}

many thanks.

You could add padding to fill up the space in each cell.

It’s not as graceful, but you could put an onclick event on the table row element which could do the same.

It is not completely clear to me what you actually want. If you want the entire row clickable why have those separate cells in the first place? Why not merge those cells and style the row so that the anchor is on the place you want.



td.yourClass {
width:480px;
border: 1px solid #000
}

td.yourClass a {
    display:block;
    text-decoration:none;
    margin-left: 120px;/*Or where =ever you would like to place the link */
}

/**link states**/

td.yourClass a {
    color: #000
}

td.yourClass:hover,
td.yourClass:active,
td.yourClass:focus {
    background-color:#FF0000;
}

Those link states won’t work in IE6 though

Perhaps she is concerned about the number of extra tags. Which is no big deal.

Anyway, this should do this:

HTML
<table>
<tr>
<td>Cell 01</td>
<td><a href=‘#’></a>Cell 02</td>
<td>Cell 03</td>
<td>Cell 04</td>
</tr>
<tr>
<td><a href=‘#’></a>Cell 01</td>
<td>Cell 02</td>
<td>Cell 03</td>
<td>Cell 04</td>
</tr>

CSS
tr{position:relative; background:pink; display:block;}
tr:hover td a{ position:absolute; top:0; left:0;width:100%; z-index:11; height:100%}

DO note, that this method changes the display context of the table row and as such it is not very robust and may have some side effects. It takes the content out of the anchor and that’s not good semantics or SEO and Also the anchor tag covers the other cells so you cant really put a background directly on the anchor, or it will cover up your content.

Alternatively, if you wanted to have ALL your cells change when someone hovers over the row… you could just use a TR selector as a general parent for your rule, like this:

TR:hover A{ declaration:value}

you would still have to put an anchor in each cell ( which is the semantically correct way of doing it anyway), but ALL anchors will be activated when the cursor passes over any area of the row

Spero che sono stato in grado di aiutare…

Please can you tell me: can I extend the width and height anchor for each whole html table row?

Hi,
Here is a nice example that was put together by Erik J. a while back. :slight_smile:

Another way to make the whole <tr> clickable without putting an <a> in each cell is to put the <a> in the first (or most obviously clickable) cell, and then use a Javascript onClick function on the <tr> to load the required page. That means less code, it should be pretty stable across browsers, and it is still fully accessible as you have the basic link there as well.

many thanks really to everyone!

the heart of the matter is to reduce the number of a href tags within the page.

Consider a table with many rows and every column has different text with an anchor with the same link inside. So I want to avoid all this waste of loading time doing an unique link for each row and “resize” this link to fit the entire row.

So the link suggested from Rayzur I think it’s perfect but maybe there’s a problem for my purposes. There isn’t any text within the anchor tags. If you take a look to that code you can see this:

<td class="prv-tab-middle link"><a href="#nogo1"></a>Talk about the book.</td>

but more correct, and more useful (the best, it would be perfect) for me, it would be in this way:

<td class="prv-tab-middle link"><a href="#nogo1">Talk about the book.</a></td>

.
So can you help, if possible, me to obtain this desired result?

Also the idea of Stevie D is very interesting but in all these suggestions try to see the page with a only-text browser, or without to load the CSS etc…, you can’t see any of that links in the page I think.

If you don’t have Javascript running, you will still have one cell that is clickable, and works with a normal <a href=…>, so you can still follow the link. If you have Javascript but not CSS, the rest of the cells will still be clickable but there won’t be any visual indication of it.

I don’t see that as a problem. If you are using a text-only browser, you won’t have Javascript or CSS at your disposal, so the only method that would make each cell clickable would be to put an <a href=…> in each individual cell - I agree that that is both a lot of work for you as author, and would make a very heavy page.

You can see an example of how my suggestion works here - http://getdown.org.uk/bus/search/whitby.shtml. OK, so it uses <div>s rather than <tr>s, but the principle is the same. In pure HTML, only the heading on each <div> is a clickable link, but a bit of Javascript makes the whole <div> clickable, and CSS gives a visual indication on :hover.

Hi,
That seems to be an easy request :slight_smile:

This is working for me in all but IE6 which would need a script for tr:hover.
http://www.css-lab.com/test/clickable-tr-link.html

It holds up well with CSS off and maintains it’s order. All I did was substitute a floated anchor for the AP anchor that was used in the link I posted. It does require some trickery with width, padding and negative margins as well as some relative positioning thrown in for stacking orders. :wink:

It would need some more work to be suited for large amounts of fluid text. As it is now though it survives about 4-5 text zooms before the line-heights overflow.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Clickable Table Row with Live Anchor</title>

<style type="text/css">
body {
    margin:0;
    padding:0;
    font: 100&#37;/1.3 arial,helvetica,sans-serif;
    background:#888;
}
h1 {
    font-size:1.5em;
    text-align: center;
}
#wrap {
    width: 530px;
    margin: 40px auto;
}
table {
    width: 100%;
    table-layout: fixed;
    border: 0 none;
    border-collapse: collapse;
    background: #aed7ff;
}
th {
    background: #000;
    color: #fff;
    text-align: left;
}
td, th {
    height: 1.5em;
    line-height: 1.5em;
    padding-left: 10px;
}
td.link {
    background: #0078f0;
}
#wrap td.link a {
    float:left;
    width: 292px;
    height: 1.5em;
    margin-left:-120px;
    padding:0 120px;
    position:relative;
    z-index: 2;
    background: url(no.gif);/* trick IE to paint the area transparent to cover content */
    text-decoration:none;
    color:#000;
}
#wrap td.link a:hover {
    color:#00F;
}
table tr:hover td {
    background: #fff;
    color: #000;
    position:relative;/*IE7 needs this*/
}
</style>
</head>
<body>
<h1>Clickable Table Row with Live Anchor</h1>

<div id="wrap">
    <table>
        <tr>
            <th width="100">Book Name</th>
            <th width="300">Description</th>
            <th width="100">Price</th>
        </tr>
        <tr>
            <td>Book 1</td>
            <td class="link"><a href="#1">Talk about the book</a></td>
            <td>$20.00</td>
        </tr>
        <tr>
            <td>Book 2</td>
            <td class="link"><a href="#2">Talk about the book</a></td>
            <td>$20.00</td>
        </tr>
        <tr>
            <td>Book 3</td>
            <td class="link"><a href="#3">Talk about the book</a></td>
            <td>$20.00</td>
        </tr>
        <tr>
            <td>Book 4</td>
            <td class="link"><a href="#4">Talk about the book</a></td>
            <td>$20.00</td>
        </tr>
        <tr>
            <td>Book 5</td>
            <td class="link"><a href="#5">Talk about the book</a></td>
            <td>$20.00</td>
        </tr>
        <tr>
            <td>Book 6</td>
            <td class="link"><a href="#6">Talk about the book</a></td>
            <td>$20.00</td>
        </tr>
        <tr>
            <td>Book 7</td>
            <td class="link"><a href="#7">Talk about the book</a></td>
            <td>$20.00</td>
        </tr>
        <tr>
            <td>Book 8</td>
            <td class="link"><a href="#8">Talk about the book</a></td>
            <td>$20.00</td>
        </tr>
    </table>
</div>

</body>
</html>

Yeah it’s simply perfect thank you Rayzur!! :slight_smile:

Also thanks for the explanation Stevie D.
Though I am curious to see the link you have suggested, it’s down for now.
I will try to take a look subsequently.

many thanks again.

Excuse me again. Reading the CSS code I have not understand three rows in the “#wrap td.link a”:


float:left;
position:relative;
z-index: 2;

I have added a border for each anchor. Why if I toggle the “float: left” the size of the anchor changes? And also what do exactly “position:relative;” and “z-index: 2;” in this case?

many thanks for your great patience.

Hi,
The float is what allows the anchor to take on dimensions, by default anchors are inline elements which cannot take on dimensions or vertical margins.

If your adding borders you will need to account for it in the width. I had a 292px width along with lt & rt paddings which gave the anchor 100% width. The side paddings were to restrict the text from the first and last columns.

And also what do exactly “position:relative;” and “z-index: 2;” in this case?
Well, IE7 needed position:relative; on the tr:hover td in order to show the BG color on hover.

In doing that I had to come back and position:relative; the anchor in order to stack it above all other tds’ with [FONT=Courier New]z-index[/FONT]. That was done to keep the anchor functional at the full width of the table row.

If you are not aware of those concepts you may want to read up on positioning and [URL=“http://reference.sitepoint.com/css/stacking”]stacking contexts.

Modern browsers were working correctly without any positioning and z-index, it was done to make it work in IE7. :wink:

If I put “display: block;” instead of “float:left;” I obtain the same result! Is it true or is there something other to know behind this choise?

And also if I load the page with my IE7 browser I can see it in this way:
http://img253.imageshack.us/img253/1158/clickablerowtable.gif
maybe do I have a buggy browser version? Do you see it in a right way in yours?

If I put “display: block;” instead of “float:left;” I obtain the same result!
Hi,
Upon further testing I don’t see a problem with using either one. A float simply generates a block box, we use it instead of display:block to kill some whitespace bugs in IE when using a UL but that is not a problem here with the table.

I have updated the code in my live example and it seems to be working fine in all but IE6 now.
http://www.css-lab.com/test/clickable-tr-link.html

And also if I load the page with my IE7 browser I can see it in this way:
We need to remove the default cellpadding from the table to close up the the gaps. Then all the box model calculations will be correct as long as your math is correct.

Need to do that in the html like so:

<div id="wrap">
    [B]<table cellpadding="0">[/B]
        <tr>
            <th width="100">Book Name</th>
            <th width="300">Description</th>
            <th width="100">Price</th>
        </tr>

I had a suspicion that the borders might complicate things and sure enough it did in IE7. It still needs position:relative on the last td of each tr in order to let the actual link extend the full width of the row.

In doing that it triggered some sort of border bug which doubled up the bottom border on the last td. I was able to target the last td with the “adjacent sibling selector”, luckily IE7 supports it. :slight_smile:


*+html #wrap tr:hover td+td+td { /*IE7 needs this mess*/
    position:relative;/*full access to the anchor (introduces new border bug)*/
    border-bottom:0 none;/*fix new border bug*/
}
*+html #wrap tr:hover td.last {/*IE7 needs a class on the last td to override the rules above*/
    border-bottom:1px solid red;
}

The override rule at the bottom simply restores the bottom border back to the last td in the table. We need to use a class for that in the html, I see no other way around it.

        <tr>
            <td>Book 8</td>
            <td class="link"><a href="#8">Talk about the book</a></td>
            [B]<td class="last">[/B]$20.00</td>
        </tr>
    </table>
</div>

IE7 was a little troublesome here but it was a nice challenge. :slight_smile:

Here is the complete revised code that is being used in the updated link:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
 "http://www.w3.org/TR/html4/strict.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Clickable Table Row with Live Anchor</title>

<style type="text/css">
body {
    margin:0;
    padding:0;
    font: 100%/1.3 arial,helvetica,sans-serif;
    background:#999;
}
h1 {
    font-size:1.5em;
    text-align: center;
}
#wrap {
    width: 534px;
    margin: 40px auto;
}
table {
    width: 100%;
    table-layout: fixed;
    border: 0 none;
    border-collapse: collapse;
    background: #cdf;    
}
th {
    background: #000;
    color: #fff;
    text-align: left;
}
td, th {
    height: 1.5em;
    line-height: 1.5em;
    padding-left: 10px;
    border:1px solid red;
}
td.link {
    background:#CEF;
}
#wrap td.link a {
    float:left;/*display:block;*/
    width: 292px;
    margin-left:-121px;
    padding:0 120px;
    position:relative;/*all browsers*/
    z-index: 2;/*all browsers*/
    background: url(no.gif);/* trick IE to paint the area transparent to cover content*/
    text-decoration:none;
    color:#000;
}
#wrap td.link a:hover {
    color:#00F;
}
#wrap tr:hover td {
    background: #fff;
    color: #000;
}
*+html #wrap tr:hover td+td+td { /*IE7 needs this mess*/
    position:relative;/*full access to the anchor (introduces new border bug)*/
    border-bottom:0 none;/*fix new border bug*/
}
*+html #wrap tr:hover td.last {/*IE7 needs a class on the last td to override the rules above*/
    border-bottom:1px solid red;
}
</style>
</head>
<body>
<h1>Clickable Table Row with Live Anchor</h1>

<div id="wrap">
    <table cellpadding="0">
        <tr>
            <th width="100">Book Name</th>
            <th width="300">Description</th>
            <th width="100">Price</th>
        </tr>
        <tr>
            <td>Book 1</td>
            <td class="link"><a href="#1">Talk about the book</a></td>
            <td>$20.00</td>
        </tr>
        <tr>
            <td>Book 2</td>
            <td class="link"><a href="#2">Talk about the book</a></td>
            <td>$20.00</td>
        </tr>
        <tr>
            <td>Book 3</td>
            <td class="link"><a href="#3">Talk about the book</a></td>
            <td>$20.00</td>
        </tr>
        <tr>
            <td>Book 4</td>
            <td class="link"><a href="#4">Talk about the book</a></td>
            <td>$20.00</td>
        </tr>
        <tr>
            <td>Book 5</td>
            <td class="link"><a href="#5">Talk about the book</a></td>
            <td>$20.00</td>
        </tr>
        <tr>
            <td>Book 6</td>
            <td class="link"><a href="#6">Talk about the book</a></td>
            <td>$20.00</td>
        </tr>
        <tr>
            <td>Book 7</td>
            <td class="link"><a href="#7">Talk about the book</a></td>
            <td>$20.00</td>
        </tr>
        <tr>
            <td>Book 8</td>
            <td class="link"><a href="#8">Talk about the book</a></td>
            <td class="last">$20.00</td>
        </tr>
    </table>
</div>

</body>
</html>

Whoops, needed to remove those width attributes from the <th> when using a strict dtd.

Added these classes to the code:

th.px100 {width:100px;}
th.px300 {width:300px;}
        <tr>
            <th class="px100">Book Name</th>
            <th class="px300">Description</th>
            <th class="px100">Price</th>
        </tr>

Link updated and now validating :wink:

Wow awesome!! Simply perfect!! :slight_smile:
Many thanks!!!