aboutsummaryrefslogtreecommitdiff
path: root/build/menu-toggle-css
diff options
context:
space:
mode:
authorBradley Taunt <bt@btxx.org>2024-06-06 08:05:12 -0400
committerBradley Taunt <bt@btxx.org>2024-06-06 08:05:12 -0400
commit6b742c459266b18e2b375b35205ce8a6c02f0452 (patch)
treeb16fbb9a045e33dd6c97eb5ab72e6ff4d9237ea3 /build/menu-toggle-css
Initial commit
Diffstat (limited to 'build/menu-toggle-css')
-rw-r--r--build/menu-toggle-css/index.html107
1 files changed, 107 insertions, 0 deletions
diff --git a/build/menu-toggle-css/index.html b/build/menu-toggle-css/index.html
new file mode 100644
index 0000000..fb941a8
--- /dev/null
+++ b/build/menu-toggle-css/index.html
@@ -0,0 +1,107 @@
+<!doctype html>
+<html lang="en" id="top">
+<head>
+ <meta charset="utf-8">
+ <meta name="viewport" content="width=device-width, initial-scale=1">
+ <link rel="icon" href="data:,">
+ <title>Menu Toggle with Pure CSS</title>
+ <link href="https://bt.ht/atom.xml" type="application/atom+xml" rel="alternate" title="Atom feed for blog posts" />
+ <style>*{box-sizing:border-box;}body{font-family:sans-serif;margin:0 auto;max-width:650px;padding:1rem;}img{max-width:100%;}pre{overflow:auto;}table{text-align:left;width:100%;}</style>
+</head>
+
+<nav>
+ <a href="#menu">Menu &darr;</a>
+</nav>
+
+<main>
+<h1>Menu Toggle with Pure CSS</h1>
+<p>2020-10-19</p>
+<p>When thinking through navigation designs for mobile devices sometimes the best option is to store away the content behind a toggle button. This button would then display the menu items upon interaction. Let me show you how to create such an element with only CSS - no need for JavaScript today!</p>
+<h2>Before we begin</h2>
+<p>I would like to point out that the concept of "toggling" the main menu (even for mobile) is not always the best solution. If you're interested, you can take a look at a previous article I wrote explaining why: <a href="/hamburger-menu-alternative.html">Using Hamburger Menus? Try Sausage Links</a></p>
+<p>Now that we have mentioned possible pitfalls of relying so heavily on toggle menus, let's build one!</p>
+<h2>Our Final Product</h2>
+<p><img src="/public/images/menu-toggle-css.png" alt="Menu toggle made from pure CSS" /></p>
+<p><a href="https://codepen.io/bradleytaunt/pen/mdEEvEX">Live CodePen Example</a></p>
+<h2>The HTML</h2>
+<p>To implement this design you really don't need much in terms of HTML:</p>
+<ul>
+<li>A single <code>checkbox</code> input</li>
+<li>A <code>label</code> that corresponds to the <code>checkbox</code></li>
+<li>A <code>nav</code> element to house our unordered list items</li>
+</ul>
+<pre><code>&lt;!-- The checkbox input &amp; label partner --&gt;
+&lt;input type=&quot;checkbox&quot; id=&quot;menu-toggle&quot;&gt;
+&lt;label for=&quot;menu-toggle&quot;&gt;Menu&lt;/label&gt;
+
+&lt;!-- The navigation we wish to toggle --&gt;
+&lt;nav&gt;
+ &lt;ul&gt;
+ &lt;li&gt;&lt;a href=&quot;&quot;&gt;Home&lt;/a&gt;&lt;/li&gt;
+ &lt;li&gt;&lt;a href=&quot;&quot;&gt;About&lt;/a&gt;&lt;/li&gt;
+ &lt;li&gt;&lt;a href=&quot;&quot;&gt;Articles&lt;/a&gt;&lt;/li&gt;
+ &lt;li&gt;&lt;a href=&quot;&quot;&gt;Colophon&lt;/a&gt;&lt;/li&gt;
+ &lt;li&gt;&lt;a href=&quot;&quot;&gt;Contact&lt;/a&gt;&lt;/li&gt;
+ &lt;/ul&gt;
+&lt;/nav&gt;
+</code></pre>
+<p>That's it!</p>
+<h2>The CSS</h2>
+<p>The first thing we need to do is "hide" the <code>checkbox</code> input element. It's important to avoid using <code>display: none</code> or <code>visibility: hidden</code> in order to achieve this. Those CSS properties can negatively impact accessibility (specifically screen readers). So we will be relying on the <code>position</code>, <code>z-index</code> and <code>opacity</code> properties to help us out.</p>
+<pre><code>/* Set the input position to absolute, send it off screen with zero opacity */
+input[type=&quot;checkbox&quot;] {
+ left: -9999px;
+ opacity: 0;
+ position: absolute;
+}
+</code></pre>
+<p>Then we give our corresponding <code>label</code> a minor face-lift to make it appear more button-like:</p>
+<pre><code>/* Minor visual styling to make the label more button-y */
+label {
+ border: 1px solid currentColor;
+ border-radius: 4px;
+ cursor: pointer;
+ padding: 10px;
+}
+</code></pre>
+<p>For our main <code>nav</code> element, we want to set it's position to <code>absolute</code> in order to avoid any janky page rendering issues that might occur when toggling the menu:</p>
+<p>`
+</p>
+<pre><code>/* Set nav to absolute (avoids odd page rendering space pop-in) */
+nav {
+ opacity: 0;
+ position: absolute;
+ z-index: -2;
+}
+</code></pre>
+<p>The last step is to actually <em>show</em> the menu if the user toggles the <code>checkbox</code>:</p>
+<pre><code>/* Show nav when checkbox is checked */
+input[type=&quot;checkbox&quot;]:checked ~ nav {
+ opacity: 1;
+ z-index: 1;
+}
+</code></pre>
+<p>It might not look like much, but you now have a fully functional menu toggle - <strong>made with pure CSS</strong>! </p>
+<h2>With Great Power...</h2>
+<p>Although this design is very simple to implement, please remember to use these types of menus wisely. Just because you <em>can</em> do something, doesn't always mean you <em>should</em>.</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