Scrolling tables
Way back in the mists of time, before display: grid;
even before display: flex;
, we "webmasters" had to use the <table></table>
element for layout.
It wasn't neat and, at the time, liable to reduce one to tears π
Now we have much better ways to mark up designs but the table
element is still useful!
If we need to display data, the table
is exactly the correct element - yay, tables!
What would that actually look like, Thom?
<table>
<tr>
<th>ID</th>
<th>Name</th>
<th>City</th>
<th>Email</th>
<th>Website</th>
</tr>
<tr>
<td>1</td>
<td>Leanne Graham</td>
<td>Gwenborough</td>
<td>Sincere@april.biz</td>
<td>hildegard.org</td>
</tr>
<!-- truncated for brevity -->
</table>
Which gives us this lovely table!
ID | Name | City | Website | |
---|---|---|---|---|
1 | Leanne Graham | Gwenborough | Sincere@april.biz | hildegard.org |
2 | Ervin Howell | Wisokyburgh | Shanna@melissa.tv | anastasia.net |
3 | Clementine Bauch | McKenziehaven | Nathan@yesenia.net | ramiro.info |
4 | Patricia Lebsack | South Elvis | Julianne.OConner@kory.org | kale.biz |
5 | Chelsey Dietrich | Roscoeview | Lucio_Hettinger@annie.ca | demarco.info |
6 | Mrs. Dennis Schulist | South Christy | Karley_Dach@jasper.info | ola.org |
7 | Kurtis Weissnat | Howemouth | Telly.Hoeger@billy.biz | elvis.io |
8 | Nicholas Runolfsdottir V | Aliyaview | Sherwood@rosamond.me | jacynthe.com |
9 | Glenna Reichert | Bartholomebury | Chaim_McDermott@dana.io | conrad.com |
10 | Clementina DuBuque | Lebsackbury | Rey.Padberg@karina.biz | ambrose.net |
Now, if you're reading this on a narrow screen (and somewhere that has not stripped my CSS!), this table will have a very fancy horizontal scroll to accommodate all of that data.
If I've got this horribly wrong, the table is absolutely destroying my page!
To get the table to do that sexy side scroll instead of ruining your layout, you're going to need to override the table's natural state of beingβ¦
table {
display: block;
overflow-x: auto;
white-space: nowrap;
}
Yes, that's right - instead of display: table;
which is the default from the user agent stylesheet, we use display: block;
. This allows us to prevent text wrapping (white-space: nowrap;
) and the all-important overflow-x: auto;
which does 99% of the magic for us.
We should set the overflow value to auto
instead of scroll
so that the scrollbars will only show if the table needs to scroll and not all the time.
Conclusion
So, we've learned that there are situations where it is right and proper to use the table
element and we've learned how to prevent the table's base nature from ruining your design on narrow screens.
I hope this helps someone like it has helped me π
Fin
Cover image courtesy of Dimitra Peppa.
onward-journeys module
Real. Simple. Syndication.
Get my latest content in your favorite RSS reader.
I use InoReader but you don't have to.
Comments
In almost all cases, the comments section is a vile cesspool of Reply Guys, racists, and bots.
I don't want to have to deal with that kind of hell so I don't have a comments section.
If you want to continue the conversation, you can always hit me up on Mastodon (which is not a vile cesspool of Reply Guys, racists, and bots).