“Something wicked this way comes.”
It was naivety, but when I first came across Flexbox, and then CSS Grid, I had this feeling all those positioning properties (align-self, justify-content…) would be available to use on block and inline elements; things like tables, lists, fixed elements, everything. The workings might be different under the hood, depending on the reference boxes, but the syntax and expected outcomes would be same: push to one edge, keep at the center, spread around evenly, etc.
Although that didn’t happen, what if I made something of that sort?
For example, I could take out the soul of an HTML table and stitch it back together with CSS Grid. In other words, it reads like a table, but acts like a grid, allowing me to use grid properties. Believe it or not, some form of this was used in the past to make tables responsive.
Before I kill off a table in this article though: there are a lot of advantages to straight-up tables. They provide a clean semantic layout that browsers perfectly understand and translate for assistive technology when needed. There’s even a <caption> element.
On the CSS side of things, there are indeed circumstances where CSS Grid is necessary for its intrinsic responsiveness, source order independence, and positioning. That’s why it’s an indispensable tool for layouts. Use that and let HTML tables to do the work of actually displaying tabular data!
But there’s the presentational benefit of table we should consider. Technically, <table> is not a presentational element like a <div>. It’s semantic: the browser and I share a common meaning for it. But whether the browser understands it as such or not, if I read <th> I believe it’s a heading followed by data. Let’s say that’s pseudo-semantic to me, if presentation is not the right term.
That pseudo-semanticism is still useful. It’s the difference between me having to read this:
<div class="product-title">Wall Calendar</div>
…and this:
<th>Wall Calendar</th>
Despite there being semantic elements for titles, like <h1> and <strong>, they don’t bring in the connection that <th> and <td> have, and it’s better to reserve <dt> and <dd> for textual structuring like glossaries rather than layouts.
So, is it okay if I use <th> this way? I mean, the table itself merely used as pseudo-semantic markup skin:
/* 🧟 */
table {
display: grid;
}
What if this HTML:
<article class="card">
<div class="card-number">
<strong>CARD NUMBER</strong>
<span>1234 5678 9123 4567</span>
</div>
<div class="card-bank">
<strong>CROWN <br>ATLANTIC <br>BANK</strong>
</div>
<div class="card-name">
<strong>CARD HOLDER</strong>
<span>JANE DOE</span>
</div>
<!-- more -->
</article>
…becomes this:
<table class="card">
<tr class="card-number">
<th>CARD NUMBER</th>
<td>1234 5678 9123 4567</td>
</tr>
<tr class="card-bank">
<th>CROWN <br>ATLANTIC <br>BANK</th>
</tr>
<tr class="card-name">
<th>CARD HOLDER</th>
<td>JANE DOE</td>
</tr>
<!-- more -->
</table>
Then, this CSS and its outcome (for the first markup):
.card {
display: grid;
grid-template-columns: auto auto 1fr;
gap: 2lh;
}
…changes to this (for the table markup):
.card {
display: grid;
grid-template-columns: auto auto 1fr;
gap: 2lh;
/* Soul stripping */
tbody {
/* Bypass tbody in the layout */
display: contents;
}
tr, tr > * {
/* Avoid table-row and table-cell styles */
display: block;
}
}
To me, the latter markup example — a pseudo-semantic table — is easier to scan than a handful of generic divs. I get the pseudo-semantics of table, and the ease of using CSS Grid for the layout. As for browsers, none of them have shunned this sort of Frankenstein creation, at least yet. The VoiceOver and keyboard navigation works as if it’s a real table.
But it’s understandable why it looks more than a little gross and against the grain of what we should be able to. We’re encouraged to maintain a separation of concerns between content and presentation and this does exactly that without affecting the tables semantics, such as its role. But maybe it should? There are plenty of other HTML elements that CSS is capable of busting.
Interesting concept. I like the idea of building out a data/stats card (say, one you’d see in the header of a CRUD edit/view page) with this method.
oh the horror!!!
yes this is indeed scary and i would not use it for these reasons:
– headings are more appropriate in this layout (slap a list or group on it if you want to be more verbose)
– changing the display alone already stripped the soul (semantic) of the table
– pseudo-semantic is only a developer convenience, not for the end users (VoiceOver is only one of many screen readers, not enough to confirm it works like a real table)
I would suggest a “ in your example. Also the bank does not have a label, correct would be `IssuerName of the bank`. So most of the time there are other ways before using a table with all its markup.
Ha! HTML elements got stripped. So I meant a definition list (dl) and the “Issuer” would be the “th” and “Name of the bank” would be the “td”.
You don’t need to resort to misusing HTML tables for this layout. Your original HTML version could be much improved by simply using sections, headings and paragraphs. In addition, you can also hand over the all-caps and line-breaks styling to CSS while you’re at it. No divs, spans, nor incorrect use of the strong element.
and
The pseudo-semantic angle is what really sold me here. I run an informational niche site that’s full of reference tables (status codes, payment schedules), and scanning raw markup with / pairs is so much easier to read than a pile of nested divs. That said, Mike Mai’s point about VoiceOver gives me pause — I’ll probably keep this trick for decorative layouts and leave the actual data tables semantically intact.
This is a fun experiment, and I especially liked the distinction you drew between a real table and a pseudo-semantic one. Once display: grid is applied, the browser no longer treats the th and td relationship the way it does in a normal table, so the visual connection stays for sighted readers while the structural meaning is lost. That trade-off is what makes Mike Mai’s point land for me: it is convenient for the author, but assistive technology does not get the same benefit. I would keep this pattern for a purely presentational card only where the same information is also available in a straightforward, accessible form, rather than using it for actual data.
This is a fun experiment, and I especially liked the distinction you drew between a real table and a pseudo-semantic one. Once display: grid is applied, the browser no longer treats the th and td relationship the way it does in a normal table, so the visual connection stays for sighted readers while the structural meaning is lost. That trade-off is what makes Mike Mai’s point land for me: it is convenient for the author, but assistive technology does not get the same benefit. I would keep this pattern for a purely presentational card only where the same information is also available in a straightforward, accessible form, rather than using it for actual data.
I’m curious as to why you avoided using a DL here. MDN says “Common uses for this element are to implement a glossary or to display metadata (a list of key-value pairs).” These days you can also wrap the
dt/ddindivelements to more easily style these pairs.They have for years. It was such a common barrier I made a function in 2018 to try to restore some table semantics. While it took years for browsers to catch up, even in 2022 tables with display properties were a mess and, unsurprisingly, there are still regressions and new bugs. Using
display: contentsis equally risky, and this is partly because it wasn’t well specified (meaning the spec did a poor job and browser vendors didn’t recognize all the gaps).In addition, this is not a well-formed table. A linter or other automated code checker should call this table out for missing a cell in row two:
The experience for a screen reader use navigating down the right column might be that they never hear that cell (and I assume that brand is important if it’s included). The user may also get incorrect row and column cell counts.
I encourage readers to test with more than VoiceOver, and additionally to test with daily screen reader users. Further, given the years of
displayregressions (looking at Safari in particular), to re-test this construct for every new browser release.