Skip to content

Instantly share code, notes, and snippets.

@joyrexus
Last active September 6, 2018 09:29
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joyrexus/5366561 to your computer and use it in GitHub Desktop.
Save joyrexus/5366561 to your computer and use it in GitHub Desktop.
Pure CSS3 toggle switch.
<!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>
.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