aboutsummaryrefslogtreecommitdiff
path: root/build/posts/tables
diff options
context:
space:
mode:
Diffstat (limited to 'build/posts/tables')
-rw-r--r--build/posts/tables/index.html133
1 files changed, 133 insertions, 0 deletions
diff --git a/build/posts/tables/index.html b/build/posts/tables/index.html
new file mode 100644
index 0000000..bc3c6d2
--- /dev/null
+++ b/build/posts/tables/index.html
@@ -0,0 +1,133 @@
+<!doctype html>
+<html lang="en">
+<head>
+ <meta charset="utf-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+ <meta name="color-scheme" content="dark light">
+ <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;}blockquote{background:rgba(0,0,0,0.1);border-left:4px solid;padding-left:5px;}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> \ No newline at end of file