From b3bf04932880e9f984675cff5d70da58167316cc Mon Sep 17 00:00:00 2001 From: Bradley Taunt Date: Fri, 5 Jul 2024 16:39:27 -0400 Subject: Start converting code samples into cleaner markdown chunks --- posts/animated-toggle-tabs.md | 125 +++++++++++++++++++++--------------------- 1 file changed, 63 insertions(+), 62 deletions(-) (limited to 'posts/animated-toggle-tabs.md') diff --git a/posts/animated-toggle-tabs.md b/posts/animated-toggle-tabs.md index dcbcfea..5404cfc 100644 --- a/posts/animated-toggle-tabs.md +++ b/posts/animated-toggle-tabs.md @@ -18,88 +18,89 @@ Let’s get started with the base skeleton.

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).

- -
- - - - - - -
-
+~~~html +
+ + + + + + +
+
+~~~ ## 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; - border: 1px solid lightgrey; - border-radius: 9999px; - display: flex; - justify-content: center; - margin: 20px auto; - max-width: 400px; - overflow: hidden; - padding: 4px; - position: relative; - } - +~~~css +.radio-toggles { + align-items: center; + background: #eee; + border: 1px solid lightgrey; + border-radius: 9999px; + display: flex; + justify-content: center; + margin: 20px auto; + max-width: 400px; + overflow: hidden; + padding: 4px; + position: relative; +} +~~~ Next, we “hide” (only visually) the default `radio` inputs: - - input[type="radio"] { - left: -9999px; - position: absolute; - z-index: -1; - } - +~~~css +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; - text-align: center; - width: 33.33%; - z-index: 2; - } - +~~~css +label { + cursor: pointer; + padding: 10px 20px; + text-align: center; + width: 33.33%; + 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; - box-shadow: 0 2px 4px rgba(0,0,0,0.15); - height: calc(100% - 8px); - left: calc(33.33% + 4px); - position: absolute; - width: calc(33.33% - 8px); - transition: left .4s; - z-index: 0; - } - +~~~sh +.slide-item { + background: white; + border-radius: 9999px; + box-shadow: 0 2px 4px rgba(0,0,0,0.15); + height: calc(100% - 8px); + left: calc(33.33% + 4px); + position: absolute; + width: calc(33.33% - 8px); + transition: left .4s; + 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. 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 { - left: 4px; - } - input[type="radio"]:nth-of-type(3):checked ~ .slide-item { - left: calc(66.66% + 4px); - } - +~~~css +input[type="radio"]:nth-of-type(1):checked ~ .slide-item { + left: 4px; +} +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. -- cgit v1.2.3-54-g00ecf