Last active
September 6, 2018 09:29
Pure CSS3 toggle switch.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<link rel="stylesheet" href="toggle.css"> | |
<style> | |
#status { | |
margin: 50px; | |
font-size: 300px; | |
font-family: 'Helvetica Neue'; | |
font-weight: 100; | |
color: #555; | |
} | |
.toggle { | |
margin: 20px; | |
} | |
</style> | |
<body onload="show();"> | |
<div class="toggle" onclick="show();"> | |
<input type="checkbox" class="toggle-checkbox" id="toggle" checked> | |
<label class="toggle-label" for="toggle"> | |
<div class="toggle-inner"></div> | |
<div class="toggle-switch"></div> | |
</label> | |
</div> | |
<div id="status"></div> | |
<script> | |
$ = function(id) { return document.getElementById(id); } | |
show = function() { $("status").innerText = $("toggle").checked; } | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
.toggle { | |
position: relative; | |
width: 90px; | |
-webkit-user-select: none; | |
-moz-user-select: none; | |
-ms-user-select: none; | |
} | |
.toggle-checkbox { | |
display: none; | |
} | |
.toggle-label { | |
display: block; | |
overflow: hidden; | |
cursor: pointer; | |
border: 2px solid #666666; | |
border-radius: 5px; | |
} | |
.toggle-inner { | |
width: 200%; | |
margin-left: -100%; | |
-moz-transition: margin 0.3s ease-in 0s; | |
-webkit-transition: margin 0.3s ease-in 0s; | |
-o-transition: margin 0.3s ease-in 0s; | |
transition: margin 0.3s ease-in 0s; | |
} | |
.toggle-inner:before, .toggle-inner:after { | |
float: left; | |
width: 50%; | |
height: 30px; | |
padding: 0; | |
line-height: 30px; | |
font-size: 16px; | |
color: white; | |
font-family: Trebuchet, Arial, sans-serif; | |
font-weight: bold; | |
-moz-box-sizing: border-box; | |
-webkit-box-sizing: border-box; | |
box-sizing: border-box; | |
} | |
.toggle-inner:before { | |
content: "ON"; | |
padding-left: 10px; | |
background-color: #6194FD; | |
color: #FFFFFF; | |
} | |
.toggle-inner:after { | |
content: "OFF"; | |
padding-right: 10px; | |
background-color: #F8F8F8; | |
color: #666666; | |
text-align: right; | |
} | |
.toggle-switch { | |
width: 35px; | |
margin: 0px; | |
background: #FFFFFF; | |
border: 2px solid #666666; | |
border-radius: 5px; | |
position: absolute; | |
top: 0; | |
bottom: 0; | |
right: 51px; | |
-moz-transition: all 0.3s ease-in 0s; -webkit-transition: all 0.3s ease-in 0s; | |
-o-transition: all 0.3s ease-in 0s; transition: all 0.3s ease-in 0s; | |
background-image: -moz-linear-gradient(center top, rgba(0,0,0,0.1) 0%, rgba(0,0,0,0) 100%); | |
background-image: -webkit-linear-gradient(center top, rgba(0,0,0,0.1) 0%, rgba(0,0,0,0) 100%); | |
background-image: -o-linear-gradient(center top, rgba(0,0,0,0.1) 0%, rgba(0,0,0,0) 100%); | |
background-image: linear-gradient(center top, rgba(0,0,0,0.1) 0%, rgba(0,0,0,0) 100%); | |
} | |
.toggle-checkbox:checked + .toggle-label .toggle-inner { | |
margin-left: 0; | |
} | |
.toggle-checkbox:checked + .toggle-label .toggle-switch { | |
right: 0px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment