Wednesday, April 29, 2020

How to Build a Simple Toggle Switch Component With the CSS Checkbox Hack

In this short tutorial, we’ll learn how to build a CSS-only switch component by taking advantage of the “CSS checkbox hack technique”. Along the way, we’ll also look at other switch implementations on Codepen. Sound like a good challenge?

Our Switch Component

Here’s what we’ll be building during this tutorial–a simple todo checklist:

For design inspiration we’ll be using the Hngry UI Kit - Food Delivery UI Kit available on Envato Elements. It’s compatible with Sketch, Figma, and Adobe XD, comes with light and dark variants, and includes way more UI components than just these checkboxes. Take a look!

Hngry UI Kit - Food Delivery UI Kit
Hngry UI Kit - Food Delivery UI Kit: download on Envato Elements

1. Begin With the HTML Markup

To begin with, we’ll define a plain ordered list with the class of switches. Each list item will contain a checkbox and its associated label. Each label will include two spans. The first one will hold the text content, while the second empty one will be responsible for the toggle switch.

Putting it all together, here’s what the required markup looks like:


2. Define the Styles

The first thing we need to deal with in our styles is visually hiding the checkbox:

Next up, the list will have a maximum width with horizontally centered content:

To style the numbers of our list exactly as we wish, we’ll get rid of the default browser styles and take advantage of CSS Counters. This will give us a list of numbers down the side, all generated by CSS:

Styling the ordered list numbers with CSS Counters

Here are the related styles:

The labels will behave as flex containers and their direct children will be distributed in the boundaries of the main axis:

Build the Switch

To build the switch component, we’ll first specify some fixed dimensions for the corresponding span element (last one).

Start building the switch component

Next, we’ll define its ::before and ::after pseudo-elements. The ::before pseudo-element will be a circle, while the ::after pseudo-element will have a close icon as a background image.

Defining the sudo-elementss of our switch component

To achieve all this, here are the corresponding styles:


3. Checkbox Hack: Toggle the Switches

Now for the interesting part! Each time we click on a switch, its appearance will change as follows:

The checked state of our switch component

More specifically:

  • Its color will change.
  • The position of its ::before pseudo-element will change. It'll be moved with a slide transition from left to right.
  • The position and the background image of its ::after pseudo-element will change. Now it will sit on the left side of its container and will include a checkmark as a background image.

To satisfy all the requirements above, we’ll take advantage of the :checked pseudo-class and the adjacent sibling combinator (+). If you need a refresher of what these selectors do, check out the resources at the end of this tutorial.

Here are the styles that handle this functionality:


Conclusion

That’s it, folks! In this tutorial, we took advantage of the “CSS checkbox hack technique” and managed to build a useful toggle switch component. Hopefully you enjoyed this exercise and you’ll incorporate it in an upcoming project.

Here’s a reminder of what we built:

As always, thanks a lot for reading!

Checkbox Hack Inspiration on CodePen

Where better to see other toggle switch implementations than CodePen? Here are some great examples to whet your appetite:

Colorful Switch by Nikolay Talanov

CSS Heart Switch by Aaron Iker

Toggle Switch by Håvard Brynjulfsen

(S)CSS Toggle Switch by oobleck

Gender Toggle Switch by Chintu Yadav Sara

Toggle Radial Effect by Florin Pop

Further Reading

Check out these tutorials to learn more about things you can build with the CSS checkbox hack technique:

No comments:

Post a Comment