Skip to content
Open core software, built in Singapore Established 2018  /  Five utilities
Jering
Talk to us

CSS / Apr 27, 2018 / JeremyTCD

Card Style CSS Only Responsive Tables

A CSS only method for creating card style responsive tables. Each row becomes a card when space is limited, without a script and without duplicating the content.

Apr 27, 2018JeremyTCD6 min readCSS

Wide data tables are useful on a desktop and hostile on a phone. Horizontal scrolling hides columns, and shrinking the text makes every cell hard to read. This article describes a CSS only method that turns each row into a card when space is limited.

The method

The table keeps its semantic structure. The only markup change is that the contents of each td element are wrapped in a span. At narrow widths, CSS hides the table header and lays out each row as a card. Labels come from the data-label attribute on each cell.

Here is the result. Resize the window to see the table change shape.

Cat breeds, a small demonstration table
BreedLifespanCountry of originDescription
Persian Cat12 to 17 yearsIranThe Persian cat is a long haired breed known for its round face and short muzzle.
Maine Coon10 to 15 yearsUnited StatesOne of the largest domesticated cat breeds, with strong hunting instincts.
British Shorthair14 to 20 yearsGreat BritainA distinctively chunky body, a dense coat and a broad face.

HTML structure

The markup stays close to a normal table. Each cell contains one wrapper element. The wrapper gives the cell content its own box when the row is in card mode, so the label and the value can sit side by side.

HTML
<td data-label="Breed"><span>Persian Cat</span></td>

Without the wrapper, the value would be a bare text node beside the generated label. The wrapper makes the layout predictable without absolute positioning or duplicated markup.

CSS approach

The technique uses a media query, generated content and a two column grid or table display. The table header is visually hidden in card mode, and each row becomes a block. Each cell becomes a grid with the label in one column and the content in another.

The key idea is simple: the information is already in the DOM. The responsive layout only changes how it is presented. No JavaScript measures the viewport, no duplicate cards are hidden from assistive technology, and the source stays readable.

Accessibility

Use standard table elements. Keep the header cells in the markup and hide them visually rather than removing them. The generated labels are decorative because the data labels are already present in the markup; make sure the reading order still makes sense.

Test with a screen reader and with keyboard navigation. A table that becomes a grid of cards should still announce the relationship between each label and value. If that relationship is lost, a definition list may be a better choice for the content.

Alternatives

There are several other approaches. One is to allow horizontal scrolling inside a wrapper. That keeps the table semantics intact and is appropriate when comparison across columns matters more than reading each row. Another is to render a separate card list and hide one of the two representations at each breakpoint. That duplicates content and creates a maintenance burden.

The CSS only card method is a good fit for small data sets with short values. For large tables, keep the table scrollable and make the first column sticky. The right answer depends on the reading task, not on a preference for cards.

Conclusion

A responsive table does not need a framework or a script. It needs a clear content model, a wrapper that lets each cell become a box and a breakpoint that switches the presentation. The result is easier to maintain and easier to test.

Reference shelf