Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>
    <label class="check-switch">
        <input type="checkbox" />
        <div class="track">
            <div class="knob"></div>
        </div>
    </label>
Check it
</h1>

<hr>

<p>Welcome, switchers. These are standard checkboxes styled to mimick a switch UI of our mobile forefathers. Defying reason, they are even <i>more</i> fun to toggle than a standard checkbox &mdash; even while using a mouse. Actually, mostly while using a mouse.</p>

<hr>

<div id="main">
    <!-- Just a wrapper to demo font size inheritance -->
    <div style="font-size:.65em">
        <label class="check-switch">Pico<input type="checkbox" /><div class="track"><div class="knob"></div></div></label>
    </div>

    <!-- Just a wrapper to demo font size inheritance -->
    <div style="font-size:1.25em">
      <label class="check-switch">Nano<input type="checkbox" /><div class="track"><div class="knob"></div></div></label>
    </div>
  
    <!-- Just a wrapper to demo font size inheritance -->
    <div style="font-size:2em">
      <label class="check-switch">Jumbo<input type="checkbox" /><div class="track"><div class="knob"></div></div></label>
    </div>
</div>

<hr>

<p>Built using relative units, the size of a switch will be determined by the font-size of its parent container. When I get some time I'll roll this into a Sass/Bourbon mixin and call it a day.</p>
<p>Thanks and credit go to Bandar Raffa and <a href="http://lea.verou.me/2013/03/ios-6-switch-style-checkboxes-with-pure-css/">Lea Verou</a> who did most of the hard work. And to <a href="https://twitter.com/desandro">@desandro</a> for being awesome and adding <a href="https://draggabilly.desandro.com/">Draggabilly</a>.</p>

<p>Questions/ideas/comments can go to <a href="https://twitter.com/kerns">@kerns</a>. Happy switching.</p>

              
            
!

CSS

              
                @import "bourbon";

/* Define active switch color */
$check-switch-color: hsla(270,95%,70%,1);


/* Hide our checkboxes */
.check-switch input[type="checkbox"] { 
  position: absolute;
  opacity: 0;
}

label.check-switch {
  clear: both;
  color: #999;
  display:inline-block;
 -webkit-tap-highlight-color: rgba(0,0,0,0);
}

/* Default Track */
.check-switch input[type="checkbox"] + .track {
  vertical-align: middle;
  width: 1.6588em;
  height:.95em;
  border: 1px solid rgba(0,0,0,.25);
  border-radius: 999px;
  background-color: rgba(0, 0, 0, 0.1);
  -webkit-transition-duration: .3s;
  -webkit-transition-property: background-color, box-shadow;
  box-shadow: inset 0 0 0 0px adjust-color($check-switch-color, $lightness: -30%, $alpha: -.25);
  margin: .5em 0 50px 0;
  cursor: pointer;
}

/* Default Track Checked */
.check-switch input[type="checkbox"]:checked + div {
  background-color: $check-switch-color;
  border: 1px solid adjust-color($check-switch-color, $lightness: -30%, $alpha: -.15);
  box-shadow: inset 0 0 0 .5em change-color($check-switch-color, $alpha: 0);
}

/* Default Knob */
.check-switch input[type="checkbox"] + div > div {
  left: 0;
  width: .95em;
  height: .95em;
  border-radius: inherit;
  background: #ffffff;
  -webkit-transition-timing-function: cubic-bezier(.54,1.85,.5,1);
  -webkit-transition-duration: 0.3s;
  -webkit-transition-property: transform, background-color, box-shadow;
  -moz-transition-timing-function: cubic-bezier(.54,1.85,.5,1);
  -moz-transition-duration: 0.3s;
  -moz-transition-property: transform, background-color;
  box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.3), 0px 0px 0 1px rgba(0, 0, 0, 0.2);
  margin-top: 0;
}

/* Default Knob Checked */
.check-switch input[type="checkbox"]:checked + div > div {
  left: 0.699em;
  background-color: #fff;
  box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.3), 0 0 0 1px adjust-color($check-switch-color, $lightness: -35%, $alpha: -.5);
}

/* Fixes drag performance */
.check-switch .knob.is-dragging {
  -webkit-transition: none;
}




/* Demo page layout, not important */
body {-webkit-user-select: none; cursor: default; font: 22px "Helvetica Neue";color: rgba(0, 0, 0, 0.77);font-weight: 200;padding: 0 3em 5em 3em;background: -webkit-linear-gradient(top, #f2fbff 0%, #ffffff 64%) no-repeat;background: -moz-linear-gradient(top, #f2fbff 0%, #ffffff 64%) no-repeat;background: -ms-linear-gradient(top, #f2fbff 0%, #ffffff 64%) no-repeat;background: linear-gradient(to bottom, #f2fbff 0%, #ffffff 64%) no-repeat;} h1 {font-weight: 100;font-size: 3em;color: #999;} h2 { font-weight: 200; font-size: 22px; color: adjust-color($check-switch-color, $lightness: -20%);} h3 {font-weight: 200; font-size: 18px; color: rgba(0, 0, 0, 0.77); margin-top: 50px;} a:link {text-decoration:none; color: #f06;} a:visited {text-decoration:none; color: #f06;} a:hover {text-decoration:underline;} a:active {text-decoration:underline;} hr {border:0; border-top:1px solid #efefef} p {color:#555; max-width: 35em; margin-top: 1em;} #main {clear:both; padding:2em 2em 0;} h1 label {position:relative; top:.20em; display:block !important; width:1.8em}

              
            
!

JS

              
                // external js
// https://draggabilly.desandro.com/draggabilly.pkgd.js

// use left/top for all positioning
Draggabilly.prototype.positionDrag = Draggabilly.prototype.setLeftTop;

$( function() {
  $('.knob').each( function( i, knob ) {
    var draggie = new Draggabilly( knob, {
      containment: true,
      axis: 'x'
    });
    draggie.on( 'dragMove', onDragMove );
    draggie.on( 'dragEnd', onDragEnd );
  
  });
});

function onDragMove() {
  var $knob = $( this.element );
  var $label = $knob.parents('label');
  // on initial drag move
  if ( !this.hasDragged ) {
    this.movePotential = $knob.parent().width() - $knob.width();
    $label.on( 'click', onLabelClickDragging );
    this.input = $label.find('input')[0];
    this.hasDragged = true;
  }
  
  var isChecked = this.position.x > this.movePotential / 2;
  this.input.checked = isChecked;
}

// disable label clicks when dragging
function onLabelClickDragging( event ) {
  event.preventDefault();
}

function onDragEnd() {
  // clear out Draggabilly's position setting
  this.element.style.left = '';
  delete this.hasDragged;  
  var $knob = $( this.element );
  var $label = $knob.parents('label');
  setTimeout( function() {
    $label.off( 'click', onLabelClickDragging );
  });
}


              
            
!
999px

Console