DEV Community

Cover image for Let's create a custom toggle switch using HTML and CSS.

Let's create a custom toggle switch using HTML and CSS.

Gaurav on March 05, 2021

Hello everyone! In this quick tutorial, let's learn how to create a custom toggle switch using HTML and CSS. Step 1 - The HTML. <...
Collapse
 
mborgeaud profile image
Matias Borgeaud

Great tip!
I just did a small change to the CSS to give a transition effect to both the tick and the cross.

knot

.toggle-thumb {
 ...
 overflow: hidden;
}
svg:nth-child(1) {
  position: relative;
  left: -26px;    
}
svg {
  transition: .4s all ease;
}
.checkbox:checked + .toggle-thumb > svg {
  transform: translateX(26px);
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
devggaurav profile image
Gaurav

Hey! This looks amazing😍🤩 Thank u so much!

Collapse
 
billerr profile image
Bill Gouveros

Nice quick tut! Since it's aimed to beginners, it would also benefit from a quick explanation of why clicking the also influences the invisible checkbox.

Collapse
 
devggaurav profile image
Gaurav

Oh yea I actually forgot to add that 😅 Thanks for the suggestion

Collapse
 
francisprovost profile image
Francis Provost

Really nice! I love that you used :checked instead of using some JS.

Don't forget to make it accessible to screen readers.
For that I would replace the label by a span. The input would be labelled somewhere else in the form. I would add aria-hidden on the span. That way screen readers will ignore the svgs that are not important for to understand the input.

Collapse
 
devggaurav profile image
Gaurav

Hey, thanks! I will surely make these changes.😄

Collapse
 
csorbamatyi profile image
Matyi Csorba

Cool example. I love these pure css solutions.
Here's mine
codepen.io/Tyutyesz/pen/VwvNbbZ

Collapse
 
devggaurav profile image
Gaurav

Looks nice. clean and minimal

Collapse
 
butalin profile image
Anass Boutaline

Great article, i never tried to use css do that, alwayse use js to trigger an event, but css seems to be more accurate, thanks

Collapse
 
devggaurav profile image
Gaurav

Thanks!

Collapse
 
shtep profile image
jim shtepa

I am wondering if you could suggest basic integration to, for example, switching themes? What would be the best approach?

Collapse
 
devggaurav profile image
Gaurav • Edited

Yes, we can use this toggle to switch themes using javascript. We will have to add an event listener on the checkbox to listen for the 'change' event and create a function that toggles the theme on the body. First, you have to define your dark-mode class in your CSS and the javascript will look something like this,

const checkBox = document.getElementsByClassName("checkbox");
const body = document.body;

checkbox.addEventListener("change", darkMode);

function darkMode() {
      body.classList.toggle("dark-mode")
}

Enter fullscreen mode Exit fullscreen mode
Collapse
 
shtep profile image
jim shtepa

Is there any reason not using .checkbox {display:none} instead of .checkbox {
opacity: 0;
width: 0;
height: 0;
}?

great material and presentation. thank you!

Collapse
 
devggaurav profile image
Gaurav

Hey, thank u so much for pointing that out. It's actually my fault😅 I was trying different things to hide that checkbox to see what works best (like display: none, height:0 width:0, opacity:0, etc), and by mistake, I forgot to remove the extra code when writing this article😅display: none also works!.

Collapse
 
nikhilmwarrier profile image
nikhilmwarrier

Awesome! I made one, but without svg...
Now I will try this too!!!

Collapse
 
deepakpal profile image
Deepak Pal

Nice one like it

Collapse
 
devggaurav profile image
Gaurav

Thank u!

Collapse
 
konrud profile image
Konstantin Rouda

Good article, nicely done. I remember I've done something similar and even tried to make a web component from it.
I've called it switch-web-component

Collapse
 
devggaurav profile image
Gaurav

Nice😀

Collapse
 
b_e_sandbakk profile image
Bjørn Erik Sandbakk

Thanks for a great example. A question, can this be done with radio as well? I see a great benefit in some radio choice lists using this technic.

Greatings from Norway.

Collapse
 
devggaurav profile image
Gaurav

Hey, thanks! Yes, I think we can also create a custom radio button. I've never tried it but I will try😀