Skip to content

Instantly share code, notes, and snippets.

@chrisdavies
Created December 3, 2014 02:25
Show Gist options
  • Save chrisdavies/27e04975f3eac7578991 to your computer and use it in GitHub Desktop.
Save chrisdavies/27e04975f3eac7578991 to your computer and use it in GitHub Desktop.
A CSS-only (no JS) checkbox toggle control. Might make the label text (yes/no) markkup rather than CSS injected content.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Toggle Example</title>
<style>
body {
font-family: sans-serif;
}
.toggle-check-input {
width: 1px;
height: 1px;
position: absolute;
}
.toggle-check-text {
display: inline-block;
position: relative;
text-transform: uppercase;
background: #CCC;
padding: 0.25em 0.5em 0.25em 2em;
border-radius: 1em;
min-width: 2em;
color: #FFF;
cursor: pointer;
transition: background-color 0.15s;
}
.toggle-check-text:after {
content: ' ';
display: block;
background: #FFF;
width: 1.1em;
height: 1.1em;
border-radius: 1em;
position: absolute;
left: 0.3em;
top: 0.25em;
transition: left 0.15s, margin-left 0.15s;
}
.toggle-check-text:before {
content: 'No';
}
.toggle-check-input:checked ~ .toggle-check-text {
background: #8ad869;
padding-left: 0.5em;
padding-right: 2em;
}
.toggle-check-input:checked ~ .toggle-check-text:before {
content: 'Yes';
}
.toggle-check-input:checked ~ .toggle-check-text:after {
left: 100%;
margin-left: -1.4em;
}
</style>
</head>
<body>
<label class="toggle-check">
<input type="checkbox" class="toggle-check-input" />
<span class="toggle-check-text"></span>
</label>
</body>
</html>
@chrisdavies
Copy link
Author

See it in action here:

http://codepen.io/anon/pen/GgpdwY

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment