aboutsummaryrefslogtreecommitdiff
path: root/build/easy-toggle-switches
diff options
context:
space:
mode:
authorbt <bt@btxx.org>2024-06-08 13:22:19 -0400
committerbt <bt@btxx.org>2024-06-08 13:22:19 -0400
commitdcfb172704f3afb68a30425029ec834be2883274 (patch)
tree02ac480745db802d7af03f3213a0c568322170e3 /build/easy-toggle-switches
parente146f8a64c793c337999ce316b16ebe5fe6f2dab (diff)
More content porting, on-going markdown changes for lowdown support
Diffstat (limited to 'build/easy-toggle-switches')
-rw-r--r--build/easy-toggle-switches/index.html149
1 files changed, 92 insertions, 57 deletions
diff --git a/build/easy-toggle-switches/index.html b/build/easy-toggle-switches/index.html
index 5120d90..b84f921 100644
--- a/build/easy-toggle-switches/index.html
+++ b/build/easy-toggle-switches/index.html
@@ -1,49 +1,65 @@
<!doctype html>
-<html lang="en" id="top">
+<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" href="data:,">
<title>Easy Toggle Switches</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>
+ <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>
+ <a href="#menu">Menu &darr;</a>
</nav>
<main>
-<h1>Easy Toggle Switches</h1>
+<h1 id="easy-toggle-switches">Easy Toggle Switches</h1>
+
<p>2019-02-18</p>
+
<p>Sometimes there is a need to use toggle elements in-place of the default <code>checkbox</code> inputs. The problem is, I tend to see a lot of developers reaching for plugins or JavaScript components in order to implement these toggles.</p>
+
<p><em>This is overkill</em>. You can create your own custom <code>input</code> elements to mimic toggles perfectly with just a small amount of CSS.</p>
-<h2>What we will be building</h2>
+
+<h2 id="what-we-will-be-building">What we will be building</h2>
+
<p><img src="/public/images/easy-toggle-switches.png" alt="Easy toggle switches" /></p>
+
<p><a href="https://codepen.io/bradleytaunt/pen/bGgbajY">Live CodePen Example</a></p>
-<h2>The HTML</h2>
+
+<h2 id="the-html">The HTML</h2>
+
<p>The build structure for these toggles is really simple, we only need:</p>
+
<ul>
-<li>a parent container to hold all our corresponding elements</li>
-<li>a checkbox <code>input</code> element (which we will hide)</li>
-<li>an empty label designed to represent the slider</li>
-<li>a text label that references the <code>input</code> </li>
+<li><p>a parent container to hold all our corresponding elements</p></li>
+<li><p>a checkbox <code>input</code> element (which we will hide)</p></li>
+<li><p>an empty label designed to represent the slider</p></li>
+<li><p>a text label that references the <code>input</code> </p>
+
+<p>&#47;* Main toggle parent container <em>&#47;
+
+ &#47;</em> Checkbox input, hidden with CSS <em>&#47;
+
+ &#47;</em> The toggle slider element <em>&#47;
+
+ &#47;</em> The text label to the right of the slider *&#47;
+ Toggle Switch
+</p></li>
</ul>
-<pre><code>/* Main toggle parent container */
-&lt;div class=&quot;toggle-switch&quot;&gt;
- /* Checkbox input, hidden with CSS */
- &lt;input class=&quot;toggle-input&quot; type=&quot;checkbox&quot; id=&quot;toggle-1&quot;&gt;
- /* The toggle slider element */
- &lt;label class=&quot;toggle-slider&quot; for=&quot;toggle-1&quot;&gt;&lt;/label&gt;
- /* The text label to the right of the slider */
- &lt;label class=&quot;toggle-label&quot; for=&quot;toggle-1&quot;&gt;Toggle Switch&lt;/label&gt;
-&lt;/div&gt;
-</code></pre>
-<p>And that's everything we need for the HTML.</p>
-<p><strong>ProTip:</strong> Don't forget to increment both the <code>id</code> and <code>for</code> attributes when adding additional toggles. This seems like a no-brainer but it's overlooked more than you think.</p>
-<h2>The CSS</h2>
+
+<p>And that&#8217;s everything we need for the HTML.</p>
+
+<p><strong>ProTip:</strong> Don&#8217;t forget to increment both the <code>id</code> and <code>for</code> attributes when adding additional toggles. This seems like a no-brainer but it&#8217;s overlooked more than you think.</p>
+
+<h2 id="the-css">The CSS</h2>
+
<p>To get things started we will add the styling to the <code>.toggle-switch</code> item directly (using <code>flexbox</code> in this demo for easier layout).</p>
-<p><strong>Sidenote</strong>: You will notice the inclusion of CSS variables in this demo - if you are unfamiliar with how to use root variables in CSS, take a look at one of my previous posts: <a href="https://accssible.com/2018/03/24/css-variables/">CSS variables</a>.</p>
+
+<p><strong>Sidenote</strong>: You will notice the inclusion of CSS variables in this demo - if you are unfamiliar with how to use root variables in CSS, take a look at one of my previous posts: CSS variables.</p>
+
<pre><code>:root {
--primary-color: #4A90E2;
}
@@ -56,14 +72,18 @@
margin: 20px 0;
}
</code></pre>
-<p>Next we will hide the default browser checkbox element since we won't be needing it:</p>
+
+<p>Next we will hide the default browser checkbox element since we won&#8217;t be needing it:</p>
+
<pre><code>.toggle-input {
position: absolute;
visibility: hidden;
z-index: -1;
}
</code></pre>
-<p>Let's also add some base styling for the <code>label</code> containing the text corresponding to it's <code>input</code> sibling:</p>
+
+<p>Let&#8217;s also add some base styling for the <code>label</code> containing the text corresponding to it&#8217;s <code>input</code> sibling:</p>
+
<pre><code>.toggle-label {
color: #ccc;
cursor: pointer;
@@ -73,8 +93,10 @@
transition: ease all .3s;
}
</code></pre>
+
<p>Now we target the <code>.toggle-slider</code> label and add the styling for the main slider base:</p>
-<pre><code>/* This is just the main slider base */
+
+<pre><code>&#47;* This is just the main slider base *&#47;
.toggle-slider {
background: #eee;
border-radius: 9999px;
@@ -89,13 +111,15 @@
width: 40px;
}
</code></pre>
+
<p>We <em>could</em> include a separate element for the circle toggle switcher itself, but instead we will use the <code>:before</code> pseudo element:</p>
+
<pre><code>.toggle-slider:before {
background: white;
border-radius: 9999px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1),
0 2px 4px rgba(0,0,0,0.2);
- content:'';
+ content:&#39;&#39;;
height: 16px;
left: 2px;
position: absolute;
@@ -104,13 +128,16 @@
width: 16px;
}
</code></pre>
-<h3>Interaction</h3>
-<p>Right now we just have a static toggle that does nothing when the user interacts with it. Let's change that by moving the pseudo element's position based on the checkbox <code>input</code> state and updating the label text color:</p>
+
+<h3 id="interaction">Interaction</h3>
+
+<p>Right now we just have a static toggle that does nothing when the user interacts with it. Let&#8217;s change that by moving the pseudo element&#8217;s position based on the checkbox <code>input</code> state and updating the label text color:</p>
+
<pre><code>.toggle-input:checked + .toggle-slider {
background: var(--primary-color);
}
.toggle-input:checked + .toggle-slider:before {
- /* Move 100% of the width minus it's own width plus initial 'left' */
+ &#47;* Move 100% of the width minus it&#39;s own width plus initial &#39;left&#39; *&#47;
left: calc(100% - 18px);
}
@@ -118,32 +145,39 @@
color: var(--primary-color);
}
</code></pre>
+
<p>And because we already included the <code>transition</code> property on both the base slider and label text, everything animates nicely between state changes.</p>
-<h2>Final code</h2>
-<p>To make things easier, you can find the HTML &amp; CSS is their entirety below:</p>
-<h3>HTML</h3>
-<pre><code>&lt;div class=&quot;toggle-switch&quot;&gt;
- &lt;input class=&quot;toggle-input&quot; type=&quot;checkbox&quot; id=&quot;toggle-1&quot;&gt;
- &lt;label class=&quot;toggle-slider&quot; for=&quot;toggle-1&quot;&gt;&lt;/label&gt;
- &lt;label class=&quot;toggle-label&quot; for=&quot;toggle-1&quot;&gt;Toggle Switch&lt;/label&gt;
-&lt;/div&gt;
-&lt;div class=&quot;toggle-switch&quot;&gt;
- &lt;input class=&quot;toggle-input&quot; type=&quot;checkbox&quot; id=&quot;toggle-2&quot;&gt;
- &lt;label class=&quot;toggle-slider&quot; for=&quot;toggle-2&quot;&gt;&lt;/label&gt;
- &lt;label class=&quot;toggle-label&quot; for=&quot;toggle-2&quot;&gt;Toggle Switch&lt;/label&gt;
-&lt;/div&gt;
-&lt;div class=&quot;toggle-switch&quot;&gt;
- &lt;input class=&quot;toggle-input&quot; type=&quot;checkbox&quot; id=&quot;toggle-3&quot; checked&gt;
- &lt;label class=&quot;toggle-slider&quot; for=&quot;toggle-3&quot;&gt;&lt;/label&gt;
- &lt;label class=&quot;toggle-label&quot; for=&quot;toggle-3&quot;&gt;Toggle Switch&lt;/label&gt;
-&lt;/div&gt;
-&lt;div class=&quot;toggle-switch&quot;&gt;
- &lt;input class=&quot;toggle-input&quot; type=&quot;checkbox&quot; id=&quot;toggle-4&quot;&gt;
- &lt;label class=&quot;toggle-slider&quot; for=&quot;toggle-4&quot;&gt;&lt;/label&gt;
- &lt;label class=&quot;toggle-label&quot; for=&quot;toggle-4&quot;&gt;Toggle Switch&lt;/label&gt;
-&lt;/div&gt;
+
+<h2 id="final-code">Final code</h2>
+
+<p>To make things easier, you can find the HTML &#38; CSS is their entirety below:</p>
+
+<h3 id="html">HTML</h3>
+
+<pre><code>&#60;div class="toggle-switch"&#62;
+ &#60;input class="toggle-input" type="checkbox" id="toggle-1"&#62;
+ &#60;label class="toggle-slider" for="toggle-1"&#62;&#60;&#47;label&#62;
+ &#60;label class="toggle-label" for="toggle-1"&#62;Toggle Switch&#60;&#47;label&#62;
+&#60;&#47;div&#62;
+&#60;div class="toggle-switch"&#62;
+ &#60;input class="toggle-input" type="checkbox" id="toggle-2"&#62;
+ &#60;label class="toggle-slider" for="toggle-2"&#62;&#60;&#47;label&#62;
+ &#60;label class="toggle-label" for="toggle-2"&#62;Toggle Switch&#60;&#47;label&#62;
+&#60;&#47;div&#62;
+&#60;div class="toggle-switch"&#62;
+ &#60;input class="toggle-input" type="checkbox" id="toggle-3" checked&#62;
+ &#60;label class="toggle-slider" for="toggle-3"&#62;&#60;&#47;label&#62;
+ &#60;label class="toggle-label" for="toggle-3"&#62;Toggle Switch&#60;&#47;label&#62;
+&#60;&#47;div&#62;
+&#60;div class="toggle-switch"&#62;
+ &#60;input class="toggle-input" type="checkbox" id="toggle-4"&#62;
+ &#60;label class="toggle-slider" for="toggle-4"&#62;&#60;&#47;label&#62;
+ &#60;label class="toggle-label" for="toggle-4"&#62;Toggle Switch&#60;&#47;label&#62;
+&#60;&#47;div&#62;
</code></pre>
-<h3>The CSS</h3>
+
+<h3 id="the-css-1">The CSS</h3>
+
<pre><code>:root {
--primary-color: #4A90E2;
}
@@ -177,7 +211,7 @@
border-radius: 9999px;
box-shadow: 0 4px 8px rgba(0,0,0,0.1),
0 2px 4px rgba(0,0,0,0.2);
- content:'';
+ content:&#39;&#39;;
height: 16px;
left: 2px;
position: absolute;
@@ -203,6 +237,7 @@
color: var(--primary-color);
}
</code></pre>
+
<p>Enjoy your custom toggles!</p>
<footer role="contentinfo">
<h2>Menu Navigation</h2>