Toggle switch without JavaScript
Posted: Friday 27th January 2023 Read Time: 7 minutesRequirements
In this tutorial, I will make the following assumptions:
- You have some experience with HTML
- You are aware how form elements such as
input
andlabel
work together - You are comfortable with CSS and SASS
The result
Here is an example of the toggle switch we are going to create together. It’s okay to be excited.
1: Structure
First things first. We need some elements on the page:
|
|
Here we have a label
element that contains a checkbox input
, and we created an empty div
next to it. The label
’s for
attribute is tied
to the checkbox element, which means clicking on the label - or anything within the label - will toggle the value between checked
and unchecked.
2: Colouring in
Now the boring part is out of the way, we can start making something that looks more like a toggle switch!
We will be making use of the empty div
element we added earlier for the container of the switch. And we can utilize the
::after
selector to create a pseudo element at the end of it,
which will become the “button” that moves from left to right to signify whether or not the checkbox is active.
|
|
The positioning here is important. We need to make sure that .toggle__fill
has its position set to relative
so that all the child elements (and pseudo elements) can position themselves in relation to it.
We then need to add position: absolute;
to our pseudo element, and can precisely position it in relation to .toggle__fill
with top
and left
.
3: Hide the input
Now we can safely hide the checkbox element from the page:
|
|
4: Movement
At this point we have a plain looking toggle that does absolutely nothing when you click it. Let’s change that.
The magic here comes from the :checked
pseudo selector, which
allows us to target elements differently based on whether or not the checkbox is checked. Along with
the general sibling combinator ~
(which
helps us select the relevant sibling elements) allows us to complete the toggle effect:
|
|
5: Smooth transitions
Now our toggle snaps quickly from left to right, then back again when we click it. This is very jarring, not to mention ugly. We can do better.
Using the transition
property we can smooth out the change between the two states:
|
|
You might want to think about adjusting the transition speed for users who have prefers-reduced-motion
set:
|
|
Switch it up
So there you have it! A simple toggle switch created from three simple HTML elements and a handful of CSS sprinkles.
The biggest benefit I’ve found of building UI this way is that you add interactivity for browsers where JavaScript is disabled.
But that’s not it. You can use this method to create more than just toggle switches. I’ve used this method to create dropdown menus and modals. It’s suitable for almost anything that has a “toggle” nature to it. For a live example my website’s settings menu, mobile navigation and language switcher are all built like this.