Convert Checkbox To Toggle Button Using CSS

In this tutorial we will Convert Checkbox To Toggle Button Using CSS. We have used before and after selectors of CSS for this purpose.


HTML Code is given below.

<!DOCTYPE html>
<html lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Convert Checkbox To Toggle Button Using CSS</title>
    <link rel="stylesheet" href="style.css">
    <input class='check' id="toggle" type="checkbox"/>
    <label for="toggle" class="button"></label>

CSS Code

CSS Code is given below. In this code we used before and after selectors to transform the label element into the toggle button, the label element is also linked with the HTML checkbox element using it's id. With this, clicking on label of checkbox will check or uncheck it.

  background-color: #22438C;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  margin: 0;
  padding: 0;
  display: none;
.button {
  position: relative;
  display: block;
  width: 200px;
  height: 50px;
.button::after {
  content: "";
  display: block;
  position: absolute;
  cursor: pointer;
.button::before {
  width: 100%;
  height: 100%;
  background-color: #bbb;
  border-radius: 25px;
  border: 1px solid #333;    
  transition: background-color 0.5s ease;
.button::after {
  top: 0;
  left: 0;
  width: 25%;
  height: 100%;
  border-radius: 50%;
  background-color: #fff;
  box-shadow: 0px 0px 5px 1px #000;    
  transition: left 0.5s ease;
.check:checked + .button::before {
  background-color: #C90011;
.check:checked + .button::after {
  left: 75%;


Video Tutorial

Watch video tutorial on how to Convert Checkbox To Toggle Button Using CSS.

Pure CSS Custom Radio Buttons Custom Emoji Radio Buttons with CSS Custom Radio Buttons with CSS Engraved HTML Buttons With CSS Neumorphic Buttons Design with CSS Cool Profile Card Design with HTML CSS Neumorphic Search Bar Design with HTML and CSS Simple Login Form with Placeholder Animation Pure CSS Button with Folded Corner Background Color Change Slider with HTML CSS and JavaScript 404 Page Not Found Template Design with HTML and CSS Movies Blog Card Design with HTML and CSS Simple CSS Border Animation with before and after Selector Custom Social Select Dropdown Menu with CSS and JavaScript Facebook Offline and Online Notification with CSS and JS New Year 2022 Text Animation with CSS and JS Change Placeholder Color using CSS Custom Select Tag DropDown with CSS and JavaScript Profile Card With Hover Effect using HTML and CSS Simple Price Range Slider With HTML CSS and JavaScript Circle With Two Colors using CSS Card With Ribbon using HTML and CSS Simple Loading Screen using HTML and CSS Disable Textarea Resizable Property using CSS Button like Links using CSS Simple Dropdown Menu Using HTML and CSS Background Color Transition using CSS Highlight Required Input Fields using CSS Disable HTML Link using CSS Difference Between Descendant Selector and Child Selector How To Change Color and Size of Hr Tag Using CSS How To Center Align HTML Table Convert Checkbox To Toggle Button Using CSS Drawing Stars using HTML and CSS Button Bounce Effect using CSS Image as Text Background using CSS background-clip Property Vertically Rotate HTML Element using CSS Vertical Align Middle using CSS Text Over Image Hover Effect - Css Hover Simple CSS Button Hover Effect using Before Selector