CSS Only Toggle Button With Smooth Transition

CSS Only Toggle Button With Smooth Transition
Code Snippet:Pure CSS/CSS3 Smooth Toggle Switch
Author: Katarzyna
Published: January 19, 2024
Last Updated: January 22, 2024
Downloads: 3,039
License: MIT
Edit Code online: View on CodePen
Read More

This CSS only code snippet helps you to create a toggle button with smooth transition. It converts the checkbox inputs to a beautiful switch toggle button. You just need to wrap your checkbox input to a div element and define its class name “switch”, then it will converted to a toggle button. Furthermore, you can also place a span element inside it that will show as an indicator.

How to Create CSS Only Toggle Button with Smooth Transition

1. First of all, create an input element with type checkbox and define its name attribute “toggle”. Define the label and span element, warp all these elements into a div element and define its class name “switch”. The complete HTML structure for the toggle switch button is as follows:

<div class="switch">
  <input type="checkbox" name="toggle">
  <label for="toggle"><i></i></label>
  <span></span> </div>

2. Style the toggle button using the following CSS.

.cd__main { background: #202838 !important; }

.switch input {
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  -ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
  filter: alpha(opacity=0);
  -moz-opacity: 0;
  opacity: 0;
  z-index: 100;
  position: absolute;
  width: 100%;
  height: 100%;
  cursor: pointer;
}

.switch {
  width: 180px;
  height: 55px;
  position: relative;
  margin: 100px auto;
}

.switch label {
  display: block;
  width: 80%;
  height: 100%;
  position: relative;
  background: #1F2736; /*#121823*/
  background: linear-gradient(#121823, #161d2b);
  border-radius: 30px 30px 30px 30px;
  box-shadow: inset 0 3px 8px 1px rgba(0,0,0,0.5),  inset 0 1px 0 rgba(0,0,0,0.5),  0 1px 0 rgba(255,255,255,0.2);
  -webkit-transition: all .5s ease;
  transition: all .5s ease;
}

.switch input ~ label i {
  display: block;
  height: 51px;
  width: 51px;
  position: absolute;
  left: 2px;
  top: 2px;
  z-index: 2;
  border-radius: inherit;
  background: #283446; /* Fallback */
  background: linear-gradient(#36455b, #283446);
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.2),  0 0 8px rgba(0,0,0,0.3),  0 12px 12px rgba(0,0,0,0.4);
  -webkit-transition: all .5s ease;
  transition: all .5s ease;
}

.switch label + span {
  content: "";
  display: inline-block;
  position: absolute;
  right: 0px;
  top: 17px;
  width: 18px;
  height: 18px;
  border-radius: 10px;
  background: #283446;
  background: gradient-gradient(#36455b, #283446);
  box-shadow: inset 0 1px 0 rgba(0,0,0,0.2),  0 1px 0 rgba(255,255,255,0.1),  0 0 10px rgba(185,231,253,0),  inset 0 0 8px rgba(0,0,0,0.9),  inset 0 -2px 5px rgba(0,0,0,0.3),  inset 0 -5px 5px rgba(0,0,0,0.5);
  -webkit-transition: all .5s ease;
  transition: all .5s ease;
  z-index: 2;
}

/* Toggle */

.switch input:checked ~ label + span {
  content: "";
  display: inline-block;
  position: absolute;
  width: 18px;
  height: 18px;
  border-radius: 10px;
  -webkit-transition: all .5s ease;
  transition: all .5s ease;
  z-index: 2;
  background: #b9f3fe;
  background: gradient-gradient(#ffffff, #77a1b9);
  box-shadow: inset 0 1px 0 rgba(0,0,0,0.1),  0 1px 0 rgba(255,255,255,0.1),  0 0 10px rgba(100,231,253,1),  inset 0 0 8px rgba( 61,157,247,0.8),  inset 0 -2px 5px rgba(185,231,253,0.3),  inset 0 -3px 8px rgba(185,231,253,0.5);
}

.switch input:checked ~ label i {
  left: auto;
  left: 63%;
  box-shadow: inset 0 1px 0 rgba(255,255,255,0.2),  0 0 8px rgba(0,0,0,0.3),  0 8px 8px rgba(0,0,0,0.3),  inset -1px 0 1px #b9f3fe;
  -webkit-transition: all .5s ease;
  transition: all .5s ease;
}

That’s all! hopefully, you have successfully created CSS only toggle button. If you have any questions or suggestions, feel free to comment below.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About CodeHim

Free Web Design Code & Scripts - CodeHim is one of the BEST developer websites that provide web designers and developers with a simple way to preview and download a variety of free code & scripts. All codes published on CodeHim are open source, distributed under OSD-compliant license which grants all the rights to use, study, change and share the software in modified and unmodified form. Before publishing, we test and review each code snippet to avoid errors, but we cannot warrant the full correctness of all content. All trademarks, trade names, logos, and icons are the property of their respective owners... find out more...

Please Rel0ad/PressF5 this page if you can't click the download/preview link

X