Responsive Tables

Here is an interesting way I came across online of someone using responsive CSS to deal with tables.  Once the window width gets below 700px it switches from a table to more of a list of tables where each table becomes a row in the previous table. 

http://codepen.io/anon/pen/MwWwbY

It’s pulled off with this CSS (especially the ::last-child and ::before lines:

@media (max-width: 700px) {
table {
display: block;
width: 500px;
margin: 0 auto;
}
thead {
display: none;
}
tbody {
display: block;
}
tbody tr {
display: block;
border: 1px lightgray solid;
border-radius: 4px;
margin-bottom: 5px;
}
tbody tr td {
display: block;
overflow: hidden;
height: 1.2em;
border-bottom: 1px lightgray solid;
}
tbody tr td::last-child {
border: 0;
}
tbody tr td::before {
display: inline-block;
content: attr(title);
width: 25%;
font-weight: 600;
text-align: right;
border-right: 1px lightgray solid;
padding-right: 10px;
margin-right: 10px;
}
}

This:

image

changes into this:

image

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s