Demo CSS3: toggle switches

CSS3 toggle switches

We can make so-called toggle switches using CSS3 by means of styling the various states (selected state and the unselected state) of the select box. We start by defining a number of global CSS rules which are applicable on all 4 switches .

Global CSS rules

/* GLOBALS */
*,
  *:after,
  *:before {
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
  padding: 0;
  margin: 0;
  }
.schakelaar {
  margin: 50px auto;
  position: relative;
  }
.schakelaar label {
  width: 100%;
  height: 100%;
  position: relative;
  display: block;
  }
.schakelaar input {
  top: 0; 
  right: 0; 
  bottom: 0; 
  left: 0;
  opacity: 0;
  z-index: 100;
  position: absolute;
  width: 100%;
  height: 100%;
  cursor: pointer;
  }

Toggle switch 1

Check these examples in your browser

The HTML

<!DOCTYPE html>
<html>
<head>
        <title>Toggle switch Example 1</title>
        <link rel="stylesheet" type="text/css" href="css/style.css" />
 </head>
<body>
<div class="toggle_switch_example1">
<input type="checkbox">
<label></label>
</div>
				
<div class="toggle_switch_example1">
<input type="checkbox" checked>
<label></label>
</div>
</body>
</html>

The CSS

/* EXAMPLE 1 */

.toggle_switch_example1 {
  width: 100px;
  height: 100px;
}

.toggle_switch_example1 label {
  border-radius: 50%;
  background: #eaeaea;
  box-shadow: 
      0 3px 5px rgba(0,0,0,0.25),
      inset 0 1px 0 rgba(255,255,255,0.3),
      inset 0 -5px 5px rgba(100,100,100,0.1),
      inset 0 5px 5px rgba(255,255,255,0.3);
}

.toggle_switch_example1 label:after {
  content: "";
  position: absolute;
  top: -8%; right: -8%; bottom: -8%; left: -8%;
  z-index: -1;
  border-radius: inherit;
  background: #ddd;
  background: -moz-linear-gradient(#ccc, #fff);
  background: -ms-linear-gradient(#ccc, #fff);
  background: -o-linear-gradient(#ccc, #fff);
  background: -webkit-gradient(linear, 0 0, 0 100%, from(#ccc), to(#fff));
  background: -webkit-linear-gradient(#ccc, #fff);
  background: linear-gradient(#ccc, #fff);
  box-shadow: 
    inset 0 2px 1px rgba(0,0,0,0.15),
    0 2px 5px rgba(200,200,200,0.1);
}

.toggle_switch_example1 label:before {
  content: "";
  position: absolute;
  width: 20%;
  height: 20%;
  border-radius: inherit;
  left: 40%;
  top: 40%;
  background: #969696;
  background: radial-gradient(40% 35%, #ccc, #969696 60%);
  box-shadow:
      inset 0 2px 4px 1px rgba(0,0,0,0.3),
      0 1px 0 rgba(255,255,255,1),
      inset 0 1px 0 white;
}

.toggle_switch_example1 input:checked ~ label {
  background: #dedede;
  background: -moz-linear-gradient(#dedede, #fdfdfd);
  background: -ms-linear-gradient(#dedede, #fdfdfd);
  background: -o-linear-gradient(#dedede, #fdfdfd);
  background: -webkit-gradient(linear, 0 0, 0 100%, from(#dedede), to(#fdfdfd));
  background: -webkit-linear-gradient(#dedede, #fdfdfd);
  background: linear-gradient(#dedede, #fdfdfd);
}

.toggle_switch_example1 input:checked ~ label:before {
  background: #25d025;
  background: radial-gradient(40% 35%, #5aef5a, #25d025 60%);
  box-shadow:
      inset 0 3px 5px 1px rgba(0,0,0,0.1),
      0 1px 0 rgba(255,255,255,0.4),
      0 0 10px 2px rgba(0, 210, 0, 0.5);
}

Toggle switch1
Image 1. Toggle switch 1.

Toggle switch 2

Check these examples in your browser

The HTML

<!DOCTYPE html>
<html>
<head>
        <title>Aan- en uitschakelaar voorbeeld 1</title>
        <link rel="stylesheet" type="text/css" href="css/style.css" />
 </head>
<body>
<div class="toggle_switch_example2">
<input type="checkbox">
<label></label>
</div>
				
<div class="toggle_switch_example2">
<input type="checkbox" checked>
<label></label>
</div>
</body>
</html>

The CSS

/* EXAMPLE 2 */
.toggle_switch_example2 {
  width: 50px;
  height: 100px;
  }
  
.toggle_switch_example2 label {
  background: #cbc7bc;
  margin: 0 auto;
  border-radius: 5px;
  box-shadow:
  inset 0 1px 0 white,
  0 0 0 1px #999,
  0 0 5px 1px rgba(0,0,0,0.2),
  0 2px 0 rgba(255,255,255,0.6),
  inset 0 10px 1px #e5e5e5,
  inset 0 11px 0 rgba(255,255,255,0.5),
  inset 0 -45px 3px #ddd;
  }
  
.toggle_switch_example2 label:after {
  content: "";
  position: absolute;
  top: -20px;
  left: -25px;
  bottom: -20px;
  right: -25px;
  background: #ccc;
  background: -moz-linear-gradient(#ddd, #bbb);
  background: -ms-linear-gradient(#ddd, #bbb);
  background: -o-linear-gradient(#ddd, #bbb);
  background: -webkit-gradient(linear, 0 0, 0 100%, from(#ddd), to(#bbb));
  background: -webkit-linear-gradient(#ddd, #bbb);
  background: linear-gradient(#ddd, #bbb);
  z-index: -1;
  border-radius: 5px;
  border: 1px solid #bbb;
  box-shadow:
  0 0 5px 1px rgba(0,0,0,0.15),
  0 3px 3px rgba(0,0,0,0.3),
  inset 0 1px 0 rgba(255,255,255,0.5);
  }
  
.toggle_switch_example2 label:before {
  content: "";
  position: absolute;
  width: 8px;
  height: 8px;
  background: #666;
  top: -13px;
  left: 20px;
  border-radius: 50%;
  box-shadow:
  0 120px 0 #666,
  0 1px 0 white,
  0 121px 0 white;
  }
  
.toggle_switch_example2 input:checked ~ label {
  background: #d2cbc3;
  box-shadow:
  inset 0 1px 0 white,
  0 0 0 1px #999,
  0 0 5px 1px rgba(0,0,0,0.2),
  inset 0 -10px 0 #aaa,
  0 2px 0 rgba(255,255,255,0.1),
  inset 0 45px 3px #e0e0E0,
  0 8px 6px rgba(0,0,0,0.18);
  }
  
Toggle switch2
Image 2. Toggle switch 2.

Toggle switch 3

Check these examples in your browser

The HTML

<!DOCTYPE html>
<html>
<head>
        <title>Aan- en uitschakelaar voorbeeld 1</title>
        <link rel="stylesheet" type="text/css" href="css/style.css" />
		 <link href='http://fonts.googleapis.com/css?family=Open+Sans+Condensed:700,300' rel='stylesheet' type='text/css' />
 </head>
<body>
<div class="toggle_switch_example2">
<input type="checkbox">
<label></label>
</div>
				
<div class="toggle_switch_example2">
<input type="checkbox" checked>
<label></label>
</div>
</body>
</html>

The CSS

.toggle_switch_example3 {
  width: 180px;
  height: 50px;
}
  
.toggle_switch_example3 label {
  display: block;
  width: 100%;
  height: 100%;
  background: #dd4c4c;
  background: -moz-linear-gradient(#dd6464, #dd4c4c);
  background: -ms-linear-gradient(#dd6464, #dd4c4c);
  background: -o-linear-gradient(#dd6464, #dd4c4c);
  background: -webkit-gradient(linear, 0 0, 0 100%, from(#dd6464), to(#dd4c4c));
  background: -webkit-linear-gradient(#dd6464, #dd4c4c);
  background: linear-gradient(#dd6464, #dd4c4c);
  border-radius: 40px;
  box-shadow:
  inset 0 3px 8px 1px rgba(0,0,0,0.2),
  0 1px 0 rgba(255,255,255,0.5);
}
  
.toggle_switch_example3 label:after {
  content: "";
  position: absolute;
  z-index: -1;
  top: -8px; right: -8px; bottom: -8px; left: -8px;
  border-radius: inherit;
  background: #ababab;
  background: -moz-linear-gradient(#f2f2f2, #ababab);
  background: -ms-linear-gradient(#f2f2f2, #ababab);
  background: -o-linear-gradient(#f2f2f2, #ababab);
  background: -webkit-gradient(linear, 0 0, 0 100%, from(#f2f2f2), to(#ababab));
  background: -webkit-linear-gradient(#f2f2f2, #ababab);
  background: linear-gradient(#f2f2f2, #ababab);
  box-shadow: 0 0 10px rgba(0,0,0,0.3),
  0 1px 1px rgba(0,0,0,0.25);
}
  
.toggle_switch_example3 label:before {
  content: "";
  position: absolute;
  z-index: -1;
  top: -18px; right: -18px; bottom: -18px; left: -18px;
  border-radius: inherit;
  background: #eee;
  background: -moz-linear-gradient(#e5e7e6, #eee);
  background: -ms-linear-gradient(#e5e7e6, #eee);
  background: -o-linear-gradient(#e5e7e6, #eee);
  background: -webkit-gradient(linear, 0 0, 0 100%, from(#e5e7e6), to(#eee));
  background: -webkit-linear-gradient(#e5e7e6, #eee);
  background: linear-gradient(#e5e7e6, #eee);
  box-shadow:
  0 1px 0 rgba(255,255,255,0.5);
  -webkit-filter: blur(1px);
  -moz-filter: blur(1px);
  -ms-filter: blur(1px);
  -o-filter: blur(1px);
  filter: blur(1px);
}
  
.toggle_switch_example3 label i {
  display: block;
  height: 100%;
  width: 60%;
  border-radius: inherit;
  background: silver;
  position: absolute;
  z-index: 2;
  right: 40%;
  top: 0;
  background: #b2ac9e;
  background: -moz-linear-gradient(#f7f2f6, #b2ac9e);
  background: -ms-linear-gradient(#f7f2f6, #b2ac9e);
  background: -o-linear-gradient(#f7f2f6, #b2ac9e);
  background: -webkit-gradient(linear, 0 0, 0 100%, from(#f7f2f6), to(#b2ac9e));
  background: -webkit-linear-gradient(#f7f2f6, #b2ac9e);
  background: linear-gradient(#f7f2f6, #b2ac9e);
  box-shadow:
  inset 0 1px 0 white,
  0 0 8px rgba(0,0,0,0.3),
  0 5px 5px rgba(0,0,0,0.2);
}
  
.toggle_switch_example3 label i:after {
  content: "";
  position: absolute;
  left: 15%;
  top: 25%;
  width: 70%;
  height: 50%;
  background: #d2cbc3;
  background: -moz-linear-gradient(#cbc7bc, #d2cbc3);
  background: -ms-linear-gradient(#cbc7bc, #d2cbc3);
  background: -o-linear-gradient(#cbc7bc, #d2cbc3);
  background: -webkit-gradient(linear, 0 0, 0 100%, from(#cbc7bc), to(#d2cbc3));
  background: -webkit-linear-gradient(#cbc7bc, #d2cbc3);
  background: linear-gradient(#cbc7bc, #d2cbc3);
  border-radius: inherit;
}
  
.toggle_switch_example3 label i:before {
  content: "off";
  text-transform: uppercase;
  font-style: normal;
  font-weight: bold;
  color: rgba(0,0,0,0.4);
  text-shadow: 0 1px 0 #bcb8ae, 0 -1px 0 #97958e;
  font-family: Helvetica, Arial, sans-serif;
  font-size: 24px;
  position: absolute;
  top: 50%;
  margin-top: -12px;
  right: -50%;
  text-shadow: 0 1px 0 #e68e8e, 0 -1px 0 #e96060;
  }
  
.toggle_switch_example3 input:checked ~ label {
  background: #9abb82;
}
  
.toggle_switch_example3 input:checked ~ label i {
  right: -1%;
}
  
.toggle_switch_example3 input:checked ~ label i:before {
  content: "on";
  right: 115%;
  color: #82a06a;
  text-shadow: 
  0 1px 0 #afcb9b,
  0 -1px 0 #6b8659;
}
  
Toggle switch3
Image 3. Toggle switch 3.

Toggle switch 4

Check these examples in your browser

The HTML

<!DOCTYPE html>
<html>
<head>
        <title>Aan- en uitschakelaar voorbeeld 1</title>
        <link rel="stylesheet" type="text/css" href="css/style.css" />
 		<link rel="stylesheet" type="text/css" href="css/font-awesome.css" />
       
 </head>
<body>
<div class="toggle_switch_example2">
<input type="checkbox">
<label><i class='icon-off'></i></label>
</div>
				
<div class="toggle_switch_example2">
<input type="checkbox" checked>
<label><i class='icon-off'></i></label>
</div>
</body>
</html>

The CSS

.toggle_switch_example4 {
  width: 150px;
  height: 150px;
 }
 
.toggle_switch_example4 label {
  border-radius: 50%;
  background: #b2ac9e;
  background: -moz-linear-gradient(#f7f2f6, #b2ac9e);
  background: -ms-linear-gradient(#f7f2f6, #b2ac9e);
  background: -o-linear-gradient(#f7f2f6, #b2ac9e);
  background: -webkit-gradient(linear, 0 0, 0 100%, from(#f7f2f6), to(#b2ac9e));
  background: -webkit-linear-gradient(#f7f2f6, #b2ac9e);
  background: linear-gradient(#f7f2f6, #b2ac9e);
  position: relative;
  color: #a5a39d;
  font-size: 70px;
  text-align: center;
  line-height: 150px;
 -webkit-transition: all 0.3s ease-out;
  -moz-transition: all 0.3s ease-out;
  -ms-transition: all 0.3s ease-out;
  -o-transition: all 0.3s ease-out;
  transition: all 0.3s ease-out;
 text-shadow: 0 2px 1px rgba(0,0,0,0.25);
 box-shadow:
  inset 0 2px 3px rgba(255,255,255,0.13),
  0 5px 8px rgba(0,0,0,0.3),
  0 10px 10px 4px rgba(0,0,0,0.3);
  z-index: -1;
 }
 
.toggle_switch_example4 label:after {
  content: ""; 
  position: absolute;
  left: -20px;
  right: -20px;
  top: -20px;
  bottom: -20px;
  z-index: -2;
  border-radius: inherit;
  box-shadow:
  inset 0 1px 0 rgba(255,255,255,0.1),
  0 1px 2px rgba(0,0,0,0.3),
  0 0 10px rgba(0,0,0,0.15);
}

.toggle_switch_example4 label:before {
  content: ""; 
  position: absolute;
  left: -10px;
  right: -10px;
  top: -10px;
  bottom: -10px;
  z-index: -1;
  border-radius: inherit;
  box-shadow: inset 0 10px 10px rgba(0,0,0,0.13); 
  -webkit-filter:blur(1px);
  -moz-filter:blur(1px);
  -ms-filter:blur(1px);
  -o-filter:blur(1px);
  filter: blur(1px); 
}
  
.toggle_switch_example4 input:checked ~ label {
  box-shadow:
  inset 0 2px 3px rgba(255,255,255,0.13),
  0 5px 8px rgba(0,0,0,0.35),
  0 3px 10px 4px rgba(0,0,0,0.2);
  color: #9abb82;
} 
  
.toggle_switch_example4 .icon-off:after {
  content: "";
  display: block;
  position: absolute;
  width: 70%;
  height: 70%;
  left: 50%;
  top: 50%;
  z-index: -1;
  margin: -35% 0 0 -35%;
  border-radius: 50%;
  background: #d2cbc3;
  background: -moz-linear-gradient(#cbc7bc, #d2cbc3);
  background: -ms-linear-gradient(#cbc7bc, #d2cbc3);
  background: -o-linear-gradient(#cbc7bc, #d2cbc3);
  background: -webkit-gradient(linear, 0 0, 0 100%, from(#cbc7bc), to(#d2cbc3));
  background: -webkit-linear-gradient(#cbc7bc, #d2cbc3);
  background: linear-gradient(#cbc7bc, #d2cbc3);
  box-shadow:
  0 -2px 5px rgba(255,255,255,0.05),
  0 2px 5px rgba(255,255,255,0.1);
}
  
Toggle switch4
Image 4. Toggle switch 4.

Leave a comment