Search code examples
htmlhtml-table

Wrapping HTML table rows in <a> tags


Is it possible to wrap entire table rows in <a> tags? I want the the entire row to be a clickable link.

If I try the following, the links get rendered above and outside the table:

This:

<table>
<a href="value_url"><tr><td>value</td><td>value</td></tr></a>
<a href="value_url"><tr><td>value</td><td>value</td></tr></a>
</table>

Renders like this:

<a href="value_url"></a>
<a href="value_url"></a>
<table>
<tr><td>value</td><td>value</td></tr>
<tr><td>value</td><td>value</td></tr>
</table>

Solution

  • Edit 2021:
    It seems nowadays there's better options that are more semantic and more screen-reader-friendly. Check out e.g. Jans solution.

    Original answer:
    as a link in each td is not a good alternative and using js is a bit dirty, here is another html/css approach:

    HTML:

    <div class="table">
        <a class="table-row" href="/mylink">
            <div class="table-cell">...</div>
            <div class="table-cell">...</div>
            <div class="table-cell">...</div>
        </a>
    </div>
    

    CSS:

    .table { display:table; }
    .table-row { display:table-row; }
    .table-cell { display:table-cell; }