Created
March 15, 2013 01:05
-
-
Save LeaVerou/5166717 to your computer and use it in GitHub Desktop.
iOS 6 style switch checkboxes
This file contains hidden or 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
| /** | |
| * Switch-style checkboxes. | |
| * Inspired by Espresso’s “Tools” switch | |
| */ | |
| input[type="checkbox"]:not(:checked), | |
| input[type="checkbox"]:checked { /* :checked here acting as a filter for older browsers */ | |
| position: absolute; | |
| opacity: 0; | |
| } | |
| label { | |
| position: relative; | |
| display: inline-block; | |
| margin: 0 1em 1em 0; | |
| font: 75% sans-serif; | |
| text-shadow: 0 1px 1px white; | |
| } | |
| input[type="checkbox"].ios-switch + div { | |
| width: 3em; height: 1em; | |
| border: 1px solid rgba(0,0,0,.3); | |
| border-radius: 999px; | |
| margin: 0 auto .8em; | |
| background: #eee; | |
| background-image: linear-gradient(rgba(0,0,0,.25), transparent); | |
| background-origin: border-box; | |
| background-clip: border-box; | |
| overflow: hidden; | |
| transition-duration: .4s; | |
| transition-property: padding, width; | |
| box-shadow: 0 1px 1px hsla(0,0%,100%,.8), | |
| 0 .1em .1em rgba(0,0,0,.3) inset, | |
| 0 .5em rgba(0,0,0,.05) inset; | |
| } | |
| input[type="checkbox"].ios-switch:checked + div { | |
| padding-left: 2em; width: 1em; | |
| background-color: hsl(210, 90%, 60%); | |
| } | |
| input[type="checkbox"].ios-switch + div:before { | |
| content: 'On'; | |
| float: left; | |
| width: 1.8em; height: 1.8em; | |
| margin: -1px; | |
| border: 1px solid rgba(0,0,0,.7); | |
| border-radius: inherit; | |
| background: #c4c4c4; | |
| background-image: linear-gradient(hsla(0,0%,100%,.3), hsla(0,0%,100%,0)); | |
| box-shadow: 0 0 .5em rgba(0,0,0,.5), | |
| 0 .2em hsla(0,0%,100%,.3) inset, | |
| 0 -.1em .3em hsla(0,0%,0%,.2) inset; | |
| color: white; | |
| text-shadow: none; | |
| text-indent: -2.8em; | |
| } | |
| input[type="checkbox"].ios-switch:active + div:before { | |
| background-color: #aaa; | |
| } | |
| .ios { | |
| font-size: 200%; | |
| } | |
| input[type="checkbox"].ios-switch + div:before, | |
| input[type="checkbox"].ios-switch + div:after { | |
| font: bold 55%/2 sans-serif; | |
| text-transform: uppercase; | |
| } | |
| input[type="checkbox"].ios-switch + div:after { | |
| content: 'Off'; | |
| float: left; | |
| color: #666; | |
| } | |
| body { | |
| padding: 1em; | |
| background: silver; | |
| } |
This file contains hidden or 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
| <h1>iOS style switches</h1> | |
| <label class="ios"><input type="checkbox" class="ios-switch" />Tools</label> | |
| <label class="ios"><input type="checkbox" class="ios-switch" checked />Tools</label> |
This file contains hidden or 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
| var switches = document.querySelectorAll('input[type="checkbox"].ios-switch'); | |
| for (var i=0, sw; sw = switches[i++]; ) { | |
| var div = document.createElement('div'); | |
| div.className = 'switch'; | |
| sw.parentNode.insertBefore(div, sw.nextSibling); | |
| } |
This file contains hidden or 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
| {"view":"split-vertical","fontsize":"100","seethrough":"","prefixfree":"1","page":"css"} |
TL;DR X_X
This is a grate solution, exactly what I was looking for a project of mine. Can you tell me what license apply to this code?
Suggestion, can you add:
box-sizing: content-box;
to line 12? It doesn't work correctly when box-sizing: border-box is on.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Nice one! But like in your older version, you should make a change for IE10 compatibility in line 11:
:root input[type="checkbox"].ios-switch + div { padding-left: 0; ... }IE10 can't transition from a not-given value to something.
BTW: I'm doing the same without any JS additions by transitioning a transparent border width of the container and having the switch positioned absolute:
https://github.com/levito/S2-Boilerplate-Forms/blob/master/extensions/css/form.less#L424
It works, but it's not as smooth in Firefox on my machines. Chrome and IE10 are buttery smooth.