From dcfb172704f3afb68a30425029ec834be2883274 Mon Sep 17 00:00:00 2001 From: bt Date: Sat, 8 Jun 2024 13:22:19 -0400 Subject: More content porting, on-going markdown changes for lowdown support --- build/animated-toggle-tabs/index.html | 67 +++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 23 deletions(-) (limited to 'build/animated-toggle-tabs/index.html') diff --git a/build/animated-toggle-tabs/index.html b/build/animated-toggle-tabs/index.html index a624b60..599d6c2 100644 --- a/build/animated-toggle-tabs/index.html +++ b/build/animated-toggle-tabs/index.html @@ -1,44 +1,55 @@ - + Animated Radio Tab Toggles - - + + +
-

Animated Radio Tab Toggles

+

Animated Radio Tab Toggles

+

2021-01-05

+

In this demo tutorial, we are making the assumption that we need to create a radio slide toggle for our made-up payment options. For this we want to display 3 simple payment choices to the user:

+ -

The Final Demo

+ +

The Final Demo

+

Live CodePen

+

Let’s get started with the base skeleton.

-

The HTML

-

There isn't anything special happening here. We just contain all our labels and inputs into a .radio-toggles wrapper, make sure those labels are each properly connected to their corresponding inputs, and then add an empty .slide-item element (more on that later).

-
<div class="radio-toggles">
-    <input type="radio" id="option-1" name="radio-options">
-    <label for="option-1">One-Time</label>
-    <input type="radio" id="option-2" name="radio-options" checked>
-    <label for="option-2">Recurring</label>
-    <input type="radio" id="option-3" name="radio-options">
-    <label for="option-3">Free</label>
-    <div class="slide-item"></div>
-</div>
+
+

The HTML

+ +
<div class="radio-toggles">
+    <input type="radio" id="option-1" name="radio-options">
+    <label for="option-1">One-Time</label>
+    <input type="radio" id="option-2" name="radio-options" checked>
+    <label for="option-2">Recurring</label>
+    <input type="radio" id="option-3" name="radio-options">
+    <label for="option-3">Free</label>
+    <div class="slide-item"></div>
+</div>
 
-

The CSS

+ +

The CSS

+

Now for the main event – the CSS. First we want to style the wrapper that holds all of our pieces together. You can tweak this to your liking, but I prefer a simple and clean style:

+
.radio-toggles {
     align-items: center;
     background: #eee;
@@ -53,14 +64,18 @@
     position: relative;
 }
 
+

Next, we “hide” (only visually) the default radio inputs:

-
input[type="radio"] {
+
+
input[type="radio"] {
     left: -9999px;
     position: absolute;
     z-index: -1;
 }
 
+

Then we give the corresponding label elements a little spacing and breathing room:

+
label {
     cursor: pointer;
     padding: 10px 20px;
@@ -69,7 +84,9 @@
     z-index: 2;
 }
 
+

Remember that .slide-item I referenced earlier? That element will be the visual “slider” that animates between the individual radio options. We style that like so:

+
.slide-item {
     background: white;
     border-radius: 9999px;
@@ -82,16 +99,20 @@
     z-index: 0;
 }
 
-

You'll notice the left, height, and width properties utilize the CSS calc attributes – this just gives some much needed padding and visual clean-up to the whole tabbed interface.

+ +

You’ll notice the left, height, and width properties utilize the CSS calc attributes – this just gives some much needed padding and visual clean-up to the whole tabbed interface.

+

For the finishing touches, we just need to tell the .slide-item where to position itself based on which radio input is currently selected:

-
input[type="radio"]:nth-of-type(1):checked ~ .slide-item {
+
+
input[type="radio"]:nth-of-type(1):checked ~ .slide-item {
     left: 4px;
 }
-input[type="radio"]:nth-of-type(3):checked ~ .slide-item {
+input[type="radio"]:nth-of-type(3):checked ~ .slide-item {
     left: calc(66.66% + 4px);
 }
 
-

That's pretty much it! You now have a fully functional, animated toggle slider with just a set of simple radio inputs and pure CSS.

+ +

That’s pretty much it! You now have a fully functional, animated toggle slider with just a set of simple radio inputs and pure CSS.