How to make a table row a link (2020)
robertcooper.me
robertcooper.me
I'm literally BEGGING web devs to stop trying to re-implement anchor tags in javascript. Every time I use some shitty SPA that decides to ignore my middle clicks on links, or worse, treat them as left clicks and navigates away from the page I was trying to leave open, I die a little bit inside.
display: table;
display: table-row-group;
display: table-header-group;
display: table-footer-group;
display: table-row;
display: table-cell;
display: table-column-group;
display: table-column;
display: table-caption;
I'm not sure how cursed this is for screen readers. It certainly feels less evil than JS solutions.https://developer.mozilla.org/en-US/docs/Web/CSS/display
the topic ads one more top search result than fails to provide the solution. I didn't find a page that does but didn't look very hard.
Tables resize in a pretty clever way. I wonder if css has anything that forces one element to resize along with another. Probably not.
I was thinking about posting it but checked ARIA recommendations to figure out what additional attributes would be appropriate. It strongly recommends against switching interactive elements (a link) to non interactive (grid row). For example, this doesn't validate:
<!DOCTYPE html>
<html lang="">
<head>
<title>Test</title>
</head>
<body>
<div role=grid><div role=rowgroup><a href=#frag role=row>test</a></div></div>
</body>
</html>
I'd be inclined to agree, just leave links inside of table cells.In practice, most tables with each row being a resource have a single column which is the meaningful one to be a link (e.g. the title or name of the resource).
There are many “skin on a database” type use cases where it can be valuable to present information that’s both tabular (eg summary of columnar information for multiple rows) and navigable (eg to individual row details, edit interface, etc).
There are also many classic (non-web) interfaces with conceptually similar prior art. One that immediately comes to mind is music players, where it’s common to present song list data as tabular, and allow selection of the full row.
These presentations are relatively out of popular usage… but I think that would make them more appealing to a HN audience, which tends to skew heavily in the direction that older UI paradigms were more usable.
While not a link, each file in a MacOS Finder List view is selectable view a single click target that encompasses the entire table row. It's definitely a table, and the entire row is a click target. Is that also odd, or is the _link_ part of it the thing you specifically find odd?
A screen reader can absolutely navigate tables by cells, by row and by column.
But my mistake, VoiceOver calls Finder's list view a table and can navigate vertically in a column and you can navigate vertically without VoiceOver if you enable Full Keyboard Access (FKA).
tr {
position: relative;
}
a:before {
content: "";
position: absolute;
top: 0; bottom: 0;
left: 0; right: 0;
}
https://codepen.io/stevesunderland/pen/oNrVQrp> top: 0; bottom: 0; > left: 0; right: 0;
You can replace this with inset: 0;
I don't see any issues selecting the text?
If you remove the positioning from the `td` and add `tr { position: relative; }` then you'll find you can activate the link by clicking anywhere in the row but you can't select the text in the row (because the link's pseudo-element is "covering" it). However, if you start a text selection outside the table, you can select the whole table (or the first however many rows you want).
This post includes some JavaScript as a progressive enhancement that tries to differentiate text selection from clicks.
https://css-tricks.com/block-links-the-search-for-a-perfect-...
True, there is no workaround, but you can achieve something kind of similar by adding a title attribute (e.g. title="foo") to the <tr>.
I see no issues with having a href attribte on <tr><th> and <td> and nesting such links doesn't make it confusing what should happen.