aboutsummaryrefslogtreecommitdiff
path: root/build/posts/tabbed-content/index.html
diff options
context:
space:
mode:
authorBradley Taunt <bt@btxx.org>2024-06-10 09:41:25 -0400
committerBradley Taunt <bt@btxx.org>2024-06-10 09:41:25 -0400
commit07e4a2dafe248280b5610f8c7d09b0f30b530f54 (patch)
tree8a145d1d4d07e1278a837ff15dadccc322d27515 /build/posts/tabbed-content/index.html
parent16d28628aca9b2d356de31c319f5e7bc0f5b2b02 (diff)
Initial modifications to rebuilt only changed files based on mod date, performance updates
Diffstat (limited to 'build/posts/tabbed-content/index.html')
-rw-r--r--build/posts/tabbed-content/index.html228
1 files changed, 228 insertions, 0 deletions
diff --git a/build/posts/tabbed-content/index.html b/build/posts/tabbed-content/index.html
new file mode 100644
index 0000000..d519fe8
--- /dev/null
+++ b/build/posts/tabbed-content/index.html
@@ -0,0 +1,228 @@
+<!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>Tabbed Content Without JavaScript</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="tabbed-content-without-javascript">Tabbed Content Without JavaScript</h1>
+<p>2019-01-28</p>
+<p>Creating tabs is a fairly trivial and common practice in web design, but many times it requires JavaScript to properly implement. Fortunately it <em>is</em> possible to create tabbed content with only using CSS.</p>
+<p><img src="/public/images/tabbed-content.png" alt="Tabbed elements with only CSS" /></p>
+<p><a href="https://codepen.io/bradleytaunt/pen/abjmayw">Live CodePen Example</a></p>
+<hr/>
+<h2 id="the-html">The HTML</h2>
+<p>The skeleton for this component is fairly basic - we just need the following structure:</p>
+<ol>
+<li><p>Parent element for each tab item</p></li>
+<li><p>Default radio input</p></li>
+<li><p>Label linked to corresponding input</p></li>
+<li><p>Inner content associated with each tab item</p></li>
+</ol>
+<p>Full HTML for reference:</p>
+<pre><code>&#60;div class="tabs"&#62;
+
+ &#60;div class="tab-item"&#62;
+ &#60;input class="tab-input" type="radio" name="tabs" id="tab-1"&#62;
+ &#60;label class="tab-label" for="tab-1"&#62;Tab 1&#60;&#47;label&#62;
+ &#60;div class="tab-content"&#62;Content goes here&#60;&#47;div&#62;
+ &#60;&#47;div&#62;
+
+ &#60;div class="tab-item"&#62;
+ &#60;input class="tab-input" type="radio" name="tabs" id="tab-2"&#62;
+ &#60;label class="tab-label" for="tab-2"&#62;Tab 2&#60;&#47;label&#62;
+ &#60;div class="tab-content"&#62;Content goes here&#60;&#47;div&#62;
+ &#60;&#47;div&#62;
+
+ &#60;div class="tab-item"&#62;
+ &#60;input class="tab-input" type="radio" name="tabs" id="tab-3"&#62;
+ &#60;label class="tab-label" for="tab-3"&#62;Tab 3&#60;&#47;label&#62;
+ &#60;div class="tab-content"&#62;Content goes here&#60;&#47;div&#62;
+ &#60;&#47;div&#62;
+
+&#60;&#47;div&#62;
+</code></pre>
+<h2 id="the-css">The CSS</h2>
+<p>First, we need to set each <code>input</code>, <code>label</code> and inner content into their own parent containers:</p>
+<pre><code>&#47;* Main parent that holds all contents *&#47;
+.tabs {
+ height: 100%;
+ min-height: 250px;
+ position: relative;
+}
+
+&#47;* Each tab items (includes heading &#38; content) *&#47;
+.tab-item {
+ display: inline;
+}
+</code></pre>
+<p>Next, we will hide the default <code>radio</code> input and design our labels to resemble a basic web tab element. The <code>z-index</code> property on the label is important for how we will be stacking our content on the z-axis (labels above inner content for example).</p>
+<pre><code>&#47;* Hide the default radio inputs *&#47;
+.tab-input {
+ position: absolute;
+ visibility: hidden;
+}
+
+&#47;* The main tab headings *&#47;
+.tab-label {
+ background: white;
+ box-shadow: inset 0 -4px 4px rgba(0,0,0,0.02);
+ color: lightgrey;
+ cursor: pointer;
+ display: inline-block;
+ font-weight: 600;
+ margin: 0 5px 0 0;
+ padding: 10px 20px;
+ position: relative;
+ text-align: center;
+ z-index: 0;
+}
+</code></pre>
+<p>The main inner content of each tab needs to have an <code>absolute</code> position set as it&#8217;s default, since the one currently selected will switch to <code>relative</code> on mobile (more on that in a moment):</p>
+<pre><code>&#47;* The inner tab content *&#47;
+.tab-content {
+ background: white;
+ bottom: 0;
+ box-shadow: 0 6px 8px rgba(0,0,0,0.02);
+ left: 0;
+ overflow: scroll;
+ padding: 20px;
+ position: absolute;
+ right: 0;
+ top: 50px;
+ z-index: 0;
+}
+</code></pre>
+<p>The final step is just telling the browser to style both the <code>label</code> and inner content of the currently selected radio <code>input</code>:</p>
+<pre><code>&#47;* Style the currently selected tab label *&#47;
+.tab-input:checked + .tab-label {
+ border: 1px solid #eee;
+ border-bottom: 0;
+ box-shadow: 0 -6px 8px rgba(0,0,0,0.02);
+ color: #268bd2;
+ z-index: 2;
+}
+
+&#47;* Show the currently selected tab content *&#47;
+.tab-input:checked ~ .tab-content {
+ border: 1px solid #eee;
+ z-index: 1;
+}
+</code></pre>
+<p>It&#8217;s as simple as that! For reference, here is the entire CSS file for easier access:</p>
+<pre><code>&#47;* Main parent that holds all contents *&#47;
+.tabs {
+ height: 100%;
+ min-height: 250px;
+ position: relative;
+}
+
+&#47;* Each tab items (includes heading &#38; content) *&#47;
+.tab-item {
+ display: inline;
+}
+
+&#47;* Hide the default radio inputs *&#47;
+.tab-input {
+ position: absolute;
+ visibility: hidden;
+}
+
+&#47;* The main tab headings *&#47;
+.tab-label {
+ background: white;
+ box-shadow: inset 0 -4px 4px rgba(0,0,0,0.02);
+ color: lightgrey;
+ cursor: pointer;
+ display: inline-block;
+ font-weight: 600;
+ margin: 0 5px 0 0;
+ padding: 10px 20px;
+ position: relative;
+ text-align: center;
+ z-index: 0;
+}
+
+&#47;* The inner tab content *&#47;
+.tab-content {
+ background: white;
+ bottom: 0;
+ box-shadow: 0 6px 8px rgba(0,0,0,0.02);
+ left: 0;
+ overflow: scroll;
+ padding: 20px;
+ position: absolute;
+ right: 0;
+ top: 50px;
+ z-index: 0;
+}
+
+&#47;* Style the currently selected tab label *&#47;
+.tab-input:checked + .tab-label {
+ border: 1px solid #eee;
+ border-bottom: 0;
+ box-shadow: 0 -6px 8px rgba(0,0,0,0.02);
+ color: #268bd2;
+ z-index: 2;
+}
+
+&#47;* Show the currently selected tab content *&#47;
+.tab-input:checked ~ .tab-content {
+ border: 1px solid #eee;
+ z-index: 1;
+}
+</code></pre>
+<h2 id="dont-forget-about-mobile">Don&#8217;t forget about mobile</h2>
+<p>With only a few extra lines of CSS we can ensure that our custom tabs will stack on top of each other and look solid on mobile devices:</p>
+<pre><code>@media(max-width:38em) {
+ .tab-label {
+ display: block;
+ width: 100%;
+ }
+ .tab-content {
+ display: none;
+ }
+ .tab-input:checked ~ .tab-content {
+ bottom: auto;
+ display: block;
+ position: relative;
+ top: auto;
+ }
+}
+</code></pre>
+<h2 id="one-minor-caveat">One minor caveat</h2>
+<p>Even though I&#8217;m a pretty big fan of implementing tabs this way, there is a small drawback:</p>
+<p>The <code>height</code> of the inner content doesn&#8217;t grow dynamically since it defaults as <code>absolute</code>, so a <code>min-height</code> or <code>height</code> value is required on the parent element. This could become a problem in certain situations where you don&#8217;t have the luxury of setting a static height.</p>
+<p>Other than that, enjoy building some JavaScript-free tabs!</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