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

              
                <h2>Toggle Switch</h2>
<p id="version">Last updated in <a href="https://cdn.watermarkinsights.com/css/rc/1.1.7/wm-global.css" target="blank">rc 1.1.7</a></p>
<p>Toggle switches may be in one of two positions, usually on or off. Behind the scenes they are marked up as checkboxes.</p>
<p>The HTML structure consists of a container <code>label</code> element with an <code>input</code> followed by a corresponding element with the accessible name and an optional element with text for the state, e.g. ON/OFF. If using the state, you will need to write a script for the onchange event.</p>
<label class="switch-toggle" for="toggle">
  <input type="checkbox" id="toggle">
  <div class="togglelabel">Label for toggle</div>
  <div class="toggletext">off</div>
</label>

<h3>Previous Implementation</h3>
<p>As with the implementation above, our original implementation uses the label to create the visual switch. However, we would add a second label to associate an accessible name with the checkbox. According to web.dev this actually <em>created</em> an accessibility issue, and Lighthouse will flag it as a problem: </p>
<blockquote>If you unintentionally associate multiple labels with a single control, you'll create inconsistent behavior across browsers and assistive technologies: some will read the first label, some will read the last label, and others will read both labels.</blockquote>
<div class="toggle-switch">
  <input type="checkbox" id="oldtoggle">
  <label class="switchtoggle" for="oldtoggle" data-checked="ON"></label>
  <label for="oldtoggle">We shouldn't do this</label>
</div>
              
            
!

CSS

              
                html, body {
  height: auto;
}
body {
  padding: 0 20px 40px;
  font-size: 14px;
}
h2 {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 0;
  
  &:not(:first-of-type) {
    margin-top: 32px;
  }
  
  + p {
    margin-top: 0;
  }
}
h3 {
  font-size: 22px;
  font-weight: 400;
  margin: 32px 0 0;
  
  + p {
    margin-top: 0;
  }
}

blockquote {
  margin-left: 0;
  padding-left: 20px;
  border-left: 4px solid #e9e7ec;
}

#version {
  font-style: italic;
}
              
            
!

JS

              
                const textcontainer = document.querySelector('.toggletext')
document.getElementById('toggle').addEventListener('change', c =>  c.target.checked ? textcontainer.innerHTML = 'on'
  : textcontainer.innerHTML = 'off'
  )
              
            
!
999px

Console