Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

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

              
                <div class="content">
  <details>
    <summary>Notes on using Switches</summary>
    <p>
      A situation one should try to avoid is having "switches" that run full width of a browser's viewport. As rarely is the label of a switch alone an effective means to determine the current state of the control.  
    </p>
    <p>
      When they label and UI of the control are positioned on opposite ends of the viewport, people who may be using software to magnify portions of their screen at a time are likely not going to see the label and the state in the same magnified window.
    </p>
    <p>
      Keeping the label and the switch UI closer together will help ensure that the state and label will be easier to visually associate with each other. It also increases the likelihood that they may both be visible in a magnified portion of hte screen.
  </p>
    <hr>
    <p>
      While these elements are visually represented as switch controls, they're semantically conveyed as checkboxes.  Use caution when representing an element as one type of control to some people, but announcing it as a different control to others.
    </p>
    <hr>
    <p>
      Note that the example 2a and b "switches" will not render exactly as intended with IE11, or in earlier versions of Firefox (pre 60).
  </p>
  </details>
</div>

<div style="max-width: 90%; margin: 0 auto 1.5em;">
  <label class="check-switch">
    Example 1a:
    <input type="checkbox">
    <span aria-hidden="true"></span>
  </label>

  <div class="switchbox">
  <label for="switchbox">
    Example 2a:
  </label>
  <input type="checkbox" id="switchbox">
</div>
</div>

<div style="width: 18em; margin: auto;">
  <label class="check-switch">
    Example 1b:
    <input type="checkbox">
    <span aria-hidden="true"></span>
  </label>

  <div class="switchbox">
    <label for="switchbox2">
      Example 2b:
    </label>
    <input type="checkbox" id="switchbox2">
  </div>
</div>


              
            
!

CSS

              
                /* containing label */
.check-switch { 
  display: block;
  overflow: hidden;
  padding: .5em;
  position: relative;
  
  &:not(:last-child) { 
    border-bottom: 1px solid #565656;
  }
}

/* provide affordance that the entire row is
   actionable */
.check-switch:hover {
  box-shadow: 0 0 2px 1px #2196f3;
}

/* using the before/after pseudo elements of the span to create the "switch" */
.check-switch span:before,
.check-switch span:after {
  border: 1px solid #565656;
  content: "";
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

/* styling specific to the knob of the switch */
.check-switch span:after {
  background: #fff;
  border-radius: 100%;
  height: 1.5em;
  right: 1.5em;
  transition: right .1825s ease-in-out;
  width: 1.5em;
}

/* styling specific to the knob "container" */
.check-switch span:before {
  background: #eee;
  border-radius: 1.75em;
  height: 1.75em;
  right: .25em;
  transition: background .2s ease-in-out;
  width: 2.75em;
}

/* hide the actual checkbox from view, but not from keyboards or ATs.
   Instead of standard visually hidden styling, instead set opacity to
   almost 0 (not zero for ChomeVox legacy bug), pointer-events none, and
   then set to full height/width of container element so that VO focus
   ring outlines the component, instead of a tiny box within the component
*/
.check-switch input {
  height: 100%;
  left: 0;
  opacity: .0001;
  position: absolute;
  top: 0;
  width: 100%;
}

.check-switch input:focus + span:before {
  outline: 2px solid;
}

/* change the position of the knob to indicate it has been checked*/
.check-switch input:checked + span:after {
  right: .25em;
}

/* update the color of the "container" to further visually indicate state */
.check-switch input:checked + span:before {
  background: #2196f3;
}

/* 'color in' the switch knob in high contrast mode by giving it
   a large border */
@media screen and (-ms-high-contrast: active) {
  .check-switch span:after {
    background-color: windowText;
  }
}




/**
 * Example 2a/b CSS
 * Revised from: https://codepen.io/scottohara/pen/KoMKXe
 *
 * Forked from Richard Keizer:
 * https://codepen.io/rakeizer/pen/QQRBoZ
 */
.switchbox {
  display: flex;
  
  &:not(:last-child) {
    border-bottom: 1px solid #565656;
  }
}

/* provide affordance that the entire row is
   actionable */
.switchbox:hover {
  box-shadow: 0 0 2px 1px #2196f3;
}

.switchbox input {
  -moz-appearance: none;
  -o-appearance: none;
  -webkit-appearance: none;
  background-color: #fafafa;
  border-radius: .825em;
  border: 1px solid #565656;
  box-shadow: inset -1.125em 0 0 1px rgba(120, 120, 120, 1);
  height: 1.5em;
  margin-right: .25em;
  outline: none;
  position: relative;
  transition: box-shadow .2s ease-in-out;
  width: 2.5em;
}

.switchbox input:focus {
  outline: 2px solid;
  outline-offset: 4px;
}

.switchbox input:checked {
  border: 1px solid rgba(0, 2, 120, 1);
  box-shadow: inset 1.125em 0 0 1px #2196f3;
}

/* remove the possibility of Microsoft's checkbox "check"
   showing up uninvited */
.switchbox input::-ms-check {
  opacity: 0;
}

.switchbox label {
  flex: 1;
  padding: .5em;
}

.switchbox > * {
  align-self: center;
}









/**
 * Demo styles
 * Styling specific to the content and wrapping
 * elements of each styled form control,
 */
html,
body {
	background: #fcfcfc;
	font-family: arial;
	font-size: 100%;
	margin: 0;
	padding: 0;
}

body {
  padding: 1.5em;
}

*,
*:before,
*:after {
	box-sizing: border-box;
}

input {
  font-size: inherit; // makes the input based switch larger
  line-height: normal;
}

.content {
  max-width: 40em;
  margin: auto;
}

              
            
!

JS

              
                /*
Checkbox inputs posing as "switches".

Ideally if needs to redesign a checkbox to look like a switch, then the control would also announce itself and act like a swtich to assistive technologies, such as screen readers.

See:
https://scottaohara.github.io/a11y_styled_form_controls/src/checkbox--switch/
*/



              
            
!
999px

Console