/    Sign up×
Community /Pin to ProfileBookmark

Table CSS Responsive

I’ve got a table on my website, well several between 6-10 columns wide.

Looking to do responsive code for tablet and mobile view.

My understanding is you can’t force a table row (TR ) over two lines?

My thought was to use DIV’s in leu of table cells (TD). Put them in a container display flex?

Is this the best approch?

How do you manage when only some flow onto a new row?

to post a comment
CSS

2 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJan 10.2021 — Making tables responsive is a complex subject and there are many CSS solutions and scripts out there, e. g. here:

https://www.jqueryscript.net/blog/best-responsive-table.html

A fine approach is to modify the structure in that way that the header is on the left side instead of on top:

https://www.cssscript.com/pure-html5-css3-responsive-table-solution/

Might be modified that way that more columns are visible on a medium sized screen but I didn't take the time to find such.
Copy linkTweet thisAlerts:
@SempervivumJan 10.2021 — >Might be modified that way that more columns are visible on a medium sized screen

I did so in the meantime. This javascript adds the data-label attributes to the table cells:
const tbl = document.querySelector('table');
const headCells = tbl.rows[0].cells;
for (let i = 1; i < tbl.rows.length; i++) {
const row = tbl.rows[i];
for (let j = 0; j < row.cells.length; j++) {
const cell = row.cells[j];
cell.setAttribute('data-label', headCells[j].textContent);
}
}

And the CSS that modifies the display dependant on the screen width:
table {
border: 1px solid #ccc;
border-collapse: collapse;
margin: 0;
padding: 0;
width: 100%;
}

<i> </i> table tr {
<i> </i> background: #f8f8f8;
<i> </i> border: 1px solid #ddd;
<i> </i> padding: .35em;
<i> </i> }

<i> </i> table th,
<i> </i> table td {
<i> </i> padding: .625em;
<i> </i> text-align: center;
<i> </i> }

<i> </i> table th {
<i> </i> font-size: .85em;
<i> </i> letter-spacing: .1em;
<i> </i> text-transform: uppercase;
<i> </i> }

<i> </i> @media screen and (max-width: 800px) {

<i> </i> table {
<i> </i> border: 0;
<i> </i> }

<i> </i> table thead {
<i> </i> display: none;
<i> </i> }

<i> </i> table tr {
<i> </i> border-bottom: 3px solid #ddd;
<i> </i> display: block;
<i> </i> margin-bottom: .625em;
<i> </i> }

<i> </i> table td {
<i> </i> border-bottom: 1px solid #ddd;
<i> </i> display: block;
<i> </i> font-size: .8em;
<i> </i> text-align: right;
<i> </i> }

<i> </i> table td:last-child {
<i> </i> border-bottom: 0;
<i> </i> }

<i> </i> @media screen and (min-width: 701px) and (max-width: 800px) {
<i> </i> tbody {
<i> </i> display: grid;
<i> </i> grid-template-columns: 2fr 1fr 1fr;
<i> </i> }


<i> </i> tbody tr:nth-child(3n-2) td:before {
<i> </i> content: attr(data-label);
<i> </i> float: left;
<i> </i> font-weight: bold;
<i> </i> text-transform: uppercase;
<i> </i> }

<i> </i> }

<i> </i> @media screen and (min-width: 561px) and (max-width: 700px) {
<i> </i> tbody {
<i> </i> display: grid;
<i> </i> grid-template-columns: 2fr 1fr;
<i> </i> }

<i> </i> tbody tr:nth-child(2n-1) td:before {
<i> </i> content: attr(data-label);
<i> </i> float: left;
<i> </i> font-weight: bold;
<i> </i> text-transform: uppercase;
<i> </i> }
<i> </i> }

<i> </i> @media screen and (max-width: 560px) {
<i> </i> tbody {
<i> </i> display: grid;
<i> </i> grid-template-columns: 1fr;
<i> </i> }

<i> </i> tbody td:before {
<i> </i> content: attr(data-label);
<i> </i> float: left;
<i> </i> font-weight: bold;
<i> </i> text-transform: uppercase;
<i> </i> }
<i> </i> }
<i> </i> }
×

Success!

Help @kiwis spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...