Simple Toggle Button With Pure HTML CSS (Free Download)

Welcome to a tutorial on how to create a simple custom toggle button with pure HTML and CSS. Want to create a custom toggle button without loading an entire 3rd party framework? Yes, it is possible with some clever CSS -Read on for an example!

ⓘ I have included a zip file with all the source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

QUICK SLIDES

 

TABLE OF CONTENTS

Download & Demo Toggle Button Useful Bits
The End

 

DOWNLOAD & DEMO

Firstly, here is the download link to the example code as promised.

 

QUICK NOTES

If you spot a bug, feel free to comment below. I try to answer short questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK START & DEMO

1-toggle.html
<!-- (A) LOAD CSS -->
<link rel="stylesheet" href="toggle.css"/>
 
<!-- (B) RECTANGLE -->
<label class="tog">
  <input type="checkbox"/>
  <i></i>
</label>

<!-- (C) ROUNDED -->
<label class="tog round">
  <input type="checkbox"/>
  <i></i>
</label>
 
<!-- (D) WITH INDICATOR -->
<label class="tog round indicate">
  <input type="checkbox"/>
  <i></i>
</label>


For you guys who just want to use this as a “plugin” without all the details:

  1. Load the CSS. Captain Obvious to the rescue.
  2. Create a <label class="tog">, sandwich a checkbox and <i> inside.
  3. If you want “rounded edges”, define <label class="tog round">.
  4. If you want an “on indicator”, define <label class="tog indicate">.

That’s all. To change the colors and dimensions, change the CSS variables in toggle.css itself (section A).

 

 

CSS CUSTOM TOGGLE BUTTON

All right, let us now get into more details on how the custom toggle button actually works – This is for you guys who want to “deep customize” it.

 

PART 1) THE HTML

toggle.html
<label class="tog">
  <input type="checkbox"/>
  <i></i>
</label>

You have already seen this in the above “quick start”. Here’s a little more on how the mechanics work:

  • The basic idea here is to hide <input type="checkbox"/> and use <i> to build our custom toggle button.
  • Yes, the checkbox will still work when hidden, when we click on the <label>.

 

PART 2) CSS VARIABLES

toggle.css

For easy customizations, the first thing we define in the CSS is a whole bunch of variables.

  • The width and height of the entire toggle button.
  • How far the “inside button” should sit.
  • Automatic calculations for the dimensions of the “inside button”.
  •  Background color of the toggle button when on/off.
  • Background color of the “inside button”.
  • HTML symbol to use when the toggle switch is “on”.

 

 

PART 3) TOGGLE BUTTON CONTAINER

toggle.css

Remember the basic mechanics from part 1?

  • (B1) Set the dimensions of the custom toggle button.
  • (B2) Rebuild the “container” of the toggle button using the . Basically, make the fill up the entirely and add our own cosmetics.
  • (B3) Hide the default checkbox with .

 

PART 4) INNER TOGGLE BUTTON

toggle.css

Now that we have the “outer shell”, the next part is to build the “inner button” using … This is basically Math, calculating the size of the “inner button” and positioning it.

 

 

PART 5) TOGGLING

toggle.css

Now that we have both the “outer container” and “inner button”, all that’s left is to deal with the toggling. For the beginners:

  • In basic English, when the checkbox is checked, change the background color of the container.
  • When the checkbox is checked, move the “inner button” to the right side.

 

PART 6) ROUNDED & INDICATORS

toggle.css

Finally, these are the optional “tweaks”.

  • To “round the edges”, we simply apply .
  • To add the “on indicator”, we apply to the “inner button”.

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

HOW TO CHECK IF TOGGLED

  • Give the checkbox an id – <input type="checkbox" id="demo"/>
  • In Javascript – if (document.getElementById("demo").checked) { CHECKED }.

Yes, the checkbox is hidden, but it still works as usual.

 

COMPATIBILITY CHECKS

This custom toggle button will work on all modern browsers. If you have to support the ancient browsers, you will have to remove and “hardcode” all the CSS variables.

 

LINKS & REFERENCES

 

YOUTUBE TUTORIAL

 

THE END

Thank you for reading, and we have come to the end of this guide. I hope that it has helped you with your project, and if you have anything to share with this guide, please feel free to comment below. Good luck and happy coding!

1 thought on “Simple Toggle Button With Pure HTML CSS (Free Download)”

Leave a Comment

Your email address will not be published.