
how to create a Button Toggle with Icons
Before We Start
Every developers need, clean and modern elements to develop their websites, It contain pictures and a user interface element within a website. The most important one and the first section that the user sees when he/she enters a website. how to create a Button Toggle with Icons
So if you are a person who wishes to develop a website, you have to know how to create that properly.

how to create a Button Toggle with Icons
We are here to solve your problem. In this article we discuss how to create this elements. Before we start please read the below articles which easier you to understand the process, if you are new to development.
Step 1 – Add HTML
It’s too easy and simple. Just copy and paste below code in your HTML editor between <body> </body> tag.
<html>
<body>
<div class="SwitchControl off dark">
<div class="SwitchControl--slider">
<div class="SwitchControl--knob"></div>
<svg viewBox="0 0 307.2 307.2" class="SwitchControl--icon_left">
<circle cx="153.6" cy="153.6" r="68.3"></circle>
</svg>
<svg x="0px" y="0px" viewBox="0 0 307.2 307.2" class="SwitchControl--icon_right">
<circle cx="153.6" cy="153.6" r="68.267" style="fill:undefined"></circle>
<path d="M153.6,0c-9.426,0-17.067,7.641-17.067,17.067v17.067c0,9.426,7.641,17.067,17.067,17.067s17.067-7.641,17.067-17.067 V17.067C170.667,7.641,163.026,0,153.6,0z" style="fill:undefined"></path>
<path d="M153.6,256c-9.426,0-17.067,7.641-17.067,17.067v17.067c0,9.426,7.641,17.067,17.067,17.067s17.067-7.641,17.067-17.067 v-17.067C170.667,263.641,163.026,256,153.6,256z" style="fill:undefined"></path>
<path d="M290.133,136.533h-17.067c-9.426,0-17.067,7.641-17.067,17.067s7.641,17.067,17.067,17.067h17.067 c9.426,0,17.067-7.641,17.067-17.067S299.559,136.533,290.133,136.533z" style="fill:undefined"></path>
<path d="M34.133,136.533H17.067C7.641,136.533,0,144.174,0,153.6s7.641,17.067,17.067,17.067h17.067 c9.426,0,17.067-7.641,17.067-17.067S43.559,136.533,34.133,136.533z" style="fill:undefined"></path>
<path d="M261.794,44.988c-6.614-6.388-17.1-6.388-23.714,0l-12.066,12.066c-6.664,6.666-6.662,17.472,0.004,24.136 c3.2,3.199,7.538,4.996,12.063,4.997c4.526-0.001,8.866-1.8,12.066-5.001l12.066-12.066 C268.761,62.34,268.573,51.536,261.794,44.988z" style="fill:undefined"></path>
<path d="M81.606,226.014c-6.548-6.78-17.353-6.968-24.132-0.419c-0.142,0.137-0.282,0.277-0.419,0.419L44.988,238.08 c-6.78,6.548-6.968,17.352-0.419,24.132c6.548,6.78,17.353,6.968,24.132,0.419c0.142-0.137,0.282-0.277,0.419-0.419l12.066-12.066 C87.966,243.598,88.154,232.794,81.606,226.014z" style="fill:undefined"></path>
<path d="M262.212,238.08l-12.066-12.066c-6.78-6.548-17.584-6.36-24.132,0.42c-6.387,6.614-6.387,17.099,0,23.712l12.066,12.066 c6.78,6.548,17.584,6.36,24.132-0.42C268.6,255.179,268.6,244.694,262.212,238.08z" style="fill:undefined"></path>
<path d="M81.606,57.474c-0.138-0.142-0.278-0.282-0.42-0.42L69.12,44.988c-6.78-6.548-17.584-6.36-24.132,0.419 c-6.388,6.614-6.388,17.099,0,23.713l12.066,12.066c6.548,6.78,17.352,6.968,24.132,0.42 C87.966,75.058,88.154,64.254,81.606,57.474z" style="fill:undefined"></path>
</svg>
</div>
</div>
</body>
</html>
Step 2 – Add CSS
Copy and paste below code in your HTML editor between <style></style> tag.
<style>
.light {
--switch-color: rgb(216, 216, 216);
--background-color-alternate: rgb(30,30,30);
}
.dark {
--switch-color: rgb(68, 68, 68);
--background-color-alternate: rgb(245, 245, 245);
}
.SwitchControl {
border-radius: 500px;
background: var(--switch-color);
width: 250px;
height: 120px;
overflow: hidden;
cursor: pointer;
position: relative;
}
.SwitchControl .SwitchControl--slider {
width: 500px;
height: 100%;
position: absolute;
top: 0;
left: calc(50% - 50px * 5);
transition: all 0.3s cubic-bezier(0.56, 0.045, 0, 1) !important;
}
.SwitchControl .SwitchControl--slider .SwitchControl--knob {
width: 100px;
height: 100px;
background: #707070;
position: absolute;
left: calc(50% - 10px * 5);
top: 10px;
border-radius: 500px;
}
.SwitchControl .SwitchControl--slider .SwitchControl--icon_left, .SwitchControl .SwitchControl--slider .SwitchControl--icon_right {
width: 70px;
height: 70px;
position: absolute;
top: 25px;
}
.SwitchControl .SwitchControl--slider .SwitchControl--icon_left *, .SwitchControl .SwitchControl--slider .SwitchControl--icon_right * {
fill: var(--background-color-alternate) !important;
}
.SwitchControl .SwitchControl--slider .SwitchControl--icon_left {
left: 90px;
}
.SwitchControl .SwitchControl--slider .SwitchControl--icon_right {
right: 90px;
}
.SwitchControl.on .SwitchControl--slider {
transform: translateX(65px);
}
.SwitchControl.off .SwitchControl--slider {
transform: translateX(-65px);
}
</style>
Step 3 – Add JavaScript
And also copy and paste below JavaScript code between <script></script> tag.
<script>
document
.querySelectorAll(".SwitchControl")
.forEach((switchControl) =>
switchControl.addEventListener("click", () => toggleSwitch(switchControl))
);
const toggleSwitch = (control) => {
if (control.classList.contains("off")) {
enableSwitch(control);
} else if (control.classList.contains("on")) {
disableSwitch(control);
}
};
const enableSwitch = (control) => {
control.classList.remove("off");
control.classList.add("on");
};
const disableSwitch = (control) => {
control.classList.remove("on");
control.classList.add("off");
};
</script>
At the end we will have something like this. You can change fonts, colors, backgrounds and all things that you want.Enjoy it.
Before

After

Video Tutorial
Please watch this video to better understand this tutorial and don’t forget to subscribe to our channel.
Help others to find out about this article on Social Media sites. If you have any doubt or any problem, don’t hesitate to contact us. Thereafter we will be able to help you and also make sure you bookmark our site on your browser.