aboutsummaryrefslogtreecommitdiff
path: root/build/tables/index.html
blob: 224fd6b966e35d2333d015387f6076b73f4af0da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<!doctype html>
<html lang="en">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<link rel="icon" href="data:,">
	<title>Making Tables Responsive With Minimal CSS</title>
	<link href="/atom.xml" type="application/atom+xml" rel="alternate" title="Atom feed for blog posts" />
	<link href="/rss.xml" type="application/rss+xml" rel="alternate" title="RSS feed for blog posts" />
<style>*{box-sizing:border-box;}body{font-family:sans-serif;line-height:1.33;margin:0 auto;max-width:650px;padding:1rem;}img{max-width:100%;}pre{border:1px solid;overflow:auto;padding:5px;}table{text-align:left;width:100%;}.footnotes{font-size:90%;}</style>
</head>

<nav>
	<a href="#menu">Menu &darr;</a>
</nav>

<main>
<h1 id="making-tables-responsive-with-minimal-css">Making Tables Responsive With Minimal CSS</h1>

<p>2019-06-11</p>

<hr/>

<p>I find that the need to create responsive table layouts pops up far more often than most developers would expect. The gut reaction might to be implement some sort of custom grid-system or pull in a pre-built library. Don&#8217;t do this - just use tables and some simple CSS.</p>

<p>My recent article, <a href="/html-like-1999">Write HTML Like It&#8217;s 1999</a>, received far more attention than I ever expected on HackerNews. With this attention came a few comments mentioning how <code>table</code> elements don&#8217;t play nice with mobile devices or that it&#8217;s not possible to have a useable layout on smaller screens. <em>This simply isn&#8217;t true</em>.</p>

<p>Included below are two separate demos showing how to optimize <code>table</code> HTML for mobile devices using only a minimal amount of CSS. These implementations may not be perfect, but they are far superior to injecting a bunch of custom <code>div</code> elements to <em>look</em> like tables.</p>

<h2 id="demo-1-just-let-them-scroll">Demo 1: Just let them scroll</h2>

<p>Okay I will admit, this implementation isn&#8217;t the <em>greatest</em> but I find it does work well with huge datasets. Simply set a <code>min-width</code> on your parent <code>table</code> element and the browser will just require the user to scroll the contents horizontally.</p>

<pre><code>table {
    min-width: 800px; &#47;* Set your desired min-width here *&#47;
}
</code></pre>

<p>Check out the CodePen below to see it in action:</p>

<p><a href="https://codepen.io/bradleytaunt/pen/ewObbW/">Live CodePen Example</a></p>

<p>I actually prefer this method because of its simplicity and function. Users on mobile are familiar with scrolling since it is one of the most basic actions required. Seeing a &#8220;cut-off&#8221; table gives them an instant visual cue that they have the ability to scroll the content.</p>

<h2 id="demo-2-more-flexible-than-you-think">Demo 2: More flexible than you think</h2>

<p>Using something like <code>flexbox</code> tends to work better when you are working with smaller table datasets. All you need to do is add some minor <code>flexbox</code> layout at your targeted mobile screen size.</p>

<pre><code>&#47;* Using 800px as mobile screen in this example *&#47;
@media(max-width: 800px) {
    &#47;* Hide the table headings *&#47;
    table thead {
        left: -9999px;
        position: absolute;
        visibility: hidden;
    }
    table tr {
        border-bottom: 0;
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        margin-bottom: 40px;
    }
    table td {
        border: 1px solid;
        margin: 0 -1px -1px 0; &#47;* Removes double-borders *&#47;
        width: 50%;
    }
}
</code></pre>

<p><a href="https://codepen.io/bradleytaunt/pen/mZbvOb/">Check out the CodePen demo</a></p>

<p>There are some caveats with this approach:</p>

<ol>
<li>We currently hide the <code>thead</code> row when in mobile view (only visually - screen readers can still scan it)</li>
<li>Some more custom work might be needed depending on how many items per <code>flexbox</code> row makes sense (based on project and dataset)</li>
</ol>

<p>You could keep the table headings and style them the same as the <code>tbody</code> contents, but I find hiding them a little cleaner. That choice is entirely up to your personal preference. You can also decide to add heading <code>span</code> elements inside the main <code>tbody</code> elements like so:</p>

<pre><code>&#47;* Default span styling - hidden on desktop *&#47;
table td span {
    background: #eee;
    color: dimgrey;
    display: none;
    font-size: 10px;
    font-weight: bold;
    padding: 5px;
    position: absolute;
    text-transform: uppercase;
    top: 0;
    left: 0;
}

&#47;* Simple CSS for flexbox table on mobile *&#47;
@media(max-width: 800px) {
    table thead {
        left: -9999px;
        position: absolute;
        visibility: hidden;
    }
    table tr {
        border-bottom: 0;
        display: flex;
        flex-direction: row;
        flex-wrap: wrap;
        margin-bottom: 40px;
    }
    table td {
        border: 1px solid;
        margin: 0 -1px -1px 0;
        padding-top: 35px; &#47;* additional padding to avoid heading overlap *&#47;
        position: relative;
        width: 50%;
    }
    &#47;* Show the heading span *&#47;
    table td span {
        display: block;
    }
}
</code></pre>

<p><a href="https://codepen.io/bradleytaunt/pen/mZdzmZ/">Live CodePen Example</a></p>

<p><strong>Updated</strong>: As pointed out by user <a href="https://news.ycombinator.com/user?id=mmoez">mmoez</a>, it is far less repetitive to use <code>:nth-child</code> pseudo selectors to implement the heading fields on mobile (as outlined in this <a href="https://css-tricks.com/responsive-data-tables/">CSS-Tricks article</a>).</p>

<h2 id="why-should-i-care-to-use-elements">Why should I care to use <code>table</code> elements?</h2>

<p>Simply put: <strong>accessibility</strong> and <strong>proper semantics</strong>.</p>

<p>Why use a screwdriver when you need a hammer? Sure, you can make that screwdriver look and <em>almost</em> work the same as a hammer, but for what purpose? Just use the damn hammer1.</p>

<p>Have fun making your tables responsive!</p>

<p>1 I know, this is a terrible analogy&#8230;</p>
<footer role="contentinfo">
    <h2>Menu Navigation</h2>
    <ul id="menu">
        <li><a href="/">Home</a></li>
        <li><a href="/projects">Projects</a></li>
        <li><a href="/uses">Uses</a></li>
        <li><a href="/wiki">Wiki</a></li>
        <li><a href="/resume">Resume</a></li>
        <li><a href="/colophon">Colophon</a></li>
        <li><a href="/now">Now</a></li>
        <li><a href="/donate">Donate</a></li>
        <li><a href="/atom.xml">RSS</a></li>
        <li><a href="#top">&uarr; Top of the page</a></li>
    </ul>
    <small>
        Built with <a href="https://git.sr.ht/~bt/barf">barf</a>. <br>
        Maintained with ♥ for the web. <br>
        Proud supporter of <a href="https://usefathom.com/ref/DKHJVX">Fathom</a> &amp; <a href="https://nextdns.io/?from=74d3p3h8">NextDNS</a>. <br>
        The content for this site is <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>.<br> The <a href="https://git.sr.ht/~bt/bt.ht">code for this site</a> is <a href="https://git.sr.ht/~bt/bt.ht/tree/master/item/LICENSE">MIT</a>.
    </small>
</footer>