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

              
                <main class="max-w-6xl mx-auto my-8 pb-4 pl-4 border-l-8 border-l-teal-600">
  <h2 class="text-2xl font-medium text-amber-800">Accessibility</h2>
  <h1 class="mb-1 text-4xl font-light text-teal-800"><code>switch</code> Input Component</h1>
  <p class="font-medium">Recently, I read an <a href="https://piccalil.li/blog/a-highly-configurable-switch-component-using-modern-css/" target="_blank">article by <strong>Andy Bell</strong></a> about creating a <code>switch</code> input component that only relies on <em>html</em> and <em>css</em>.  There were several takeaways from article, which I encourage you to read. I reproduce the component here with minor changes, mainly so I don't forget about it. Here is a <a href="https://codepen.io/piccalilli/pen/MWxERjV/615c784c307feb96f5e7459c5d5aa456" target="_blank">link to the original <strong>pen</strong></a></p>
  
  <div id="the-meat-of-the-pen" class="my-8 flex justify-center">
    <label class="switch-input">
      <span id="switcher-purpose" class="visually-hidden">Toggle the power to the National Grid</span>
      <input id="switcher" type="checkbox" role="switch" class="visually-hidden" />
      <span class="switch-input__decor" data-switch-input-state="on" aria-hidden="true"
        >On</span
      >
      <span class="switch-input__decor" data-switch-input-state="off" aria-hidden="true"
        >Off</span
      >
      <span class="switch-input__thumb" aria-hidden="true"></span>
    </label>
  </div>
  
  <div id="announce" class="my-8 flex justify-center">
    <span class="font-mono text-amber-800"></span>
  </div>
  
</main>
              
            
!

CSS

              
                @import url('https://fonts.googleapis.com/css2?family=Lato:wght@100;300;400;700;900&display=swap');
html, body {
  font-family: "Lato", sans-serif;
  font-size: 20px;
}
a {
  position: relative;
  display: inline-block;
  transition: color 300ms linear, border-color 500ms linear;
  &::after {
    position: absolute; content: '';
    bottom: 0; left: 0; right: 0;
    border: solid 2px #ddd;    
  }
  &:hover {
    color: teal;
    &&::after { border-color: teal; }
  }
}

/**  The switch component **/
#the-meat-of-the-pen {
  --switch-input-thumb-size: 44px;
  --switch-input-thumb-bg: #ffffff;
  --switch-input-thumb-stroke: 1px solid grey;
  --switch-input-off-bg: #444444;
  --switch-input-off-text: #ffffff;
  --switch-input-on-bg: #00a878;
  --switch-input-on-text: #ffffff;
  --switch-input-gutter: 4px;
  --switch-input-decor-space: var(--switch-input-gutter) 1.25ch;
  --switch-input-focus-stroke: 3px solid #ff6978;
  --switch-input-focus-stroke-muted: 1px solid #ff6978;
  --switch-input-font-weight: bold;
  --switch-input-font-family: sans-serif;
  --switch-input-font-size: 18cqw;
  --switch-input-transition: inset 50ms linear;
}

/* "hide" element from visual view, but not from screen readers */
.visually-hidden {
  position: absolute;
  margin: 0;
  padding: 0;
  width: 1px;
  height: 1px;
  border: none;
  clip: rect(0 0 0 0);  /* older browsers */
  clip-path: inset(50%); /* newer browsers */
  overflow: hidden;
  white-space: nowrap; /* ensure spaces between words are preserved */
}

.switch-input {
  position: relative; z-index: 0; 
  container-type: inline-size; /* setup container: font size is based on container width */
  
  
  width: calc((var(--switch-input-thumb-size) * 2) + (var(--switch-input-gutter) * 3));
  height: calc(var(--switch-input-thumb-size) + (var(--switch-input-gutter) * 2));
  
  padding: var(--switch-input-gutter);
  
  border-radius: calc(var(--switch-input-thumb-size) + var(--switch-input-gutter));
  
  /* make it look nice */
  background: var(--switch-input-off-bg);
  color: var(--switch-input-off-text);
  text-align: left;
  text-transform: uppercase;
  font-family: var(--switch-input-font-family);
  font-weight: var(--switch-input-font-weight);
  
  cursor: pointer;
}


.switch-input__decor {
  position: absolute;
  inset-block: 0;
  inset-inline-start: 0;
  display: flex;
  align-items: center;
  width: 100%;
  padding: var(--switch-input-decor-space);
  font-size: var(--switch-input-font-size);
}
/* move the 'off' decorator to the end */
.switch-input__decor[data-switch-input-state='off'] {
  justify-content: flex-end;
}

.switch-input__thumb {
  position: absolute;
  inset-block-start: var(--switch-input-gutter);
  inset-inline-start: var(--switch-input-gutter);

  display: block;
  
  width: var(--switch-input-thumb-size);
  height: var(--switch-input-thumb-size);
  
  border-radius: var(--switch-input-thumb-size);
  border: var(--switch-input-thumb-stroke);
  
  background: var(--switch-input-thumb-bg);
  
  transition: var(--switch-input-transition);
  z-index: 1;
}

/* accessibility - don't forget focus */
.switch-input:has(:focus-visible) {
  outline: var(--switch-input-focus-stroke-muted);
  input { outline: none; }  /* hide the outline from the actual input element */
  .switch-input__thumb {
    outline: var(--switch-input-focus-stroke);    
  }
}

/* most importantly... style the on/off states */
.switch-input:has(:checked) {
  background: var(--switch-input-on-bg);
  color: var(--switch-input-on-text);
  
  /* move thumb to the right */
  .switch-input__thumb {
    inset-inline-start: calc(
      var(--switch-input-thumb-size) + (var(--switch-input-gutter) * 2)
    );
  }
}

              
            
!

JS

              
                function announceSwitchChange() {
  
  const input = document.querySelector('#switcher');
  const output = document.querySelector('#announce span');
  const purpose = document.querySelector('#switcher-purpose');
  
  if (!input || !output || !purpose) {
    console.error("Could not locate the elements", {input, output, purpose});
    return false;
  }
  
  const announceState = () => {    
    output.innerText = `${purpose.textContent}: ${input.checked ? 'ON' : 'OFF'}`;
  }
  
  input.addEventListener('change', (e) => {
    announceState();
  });
  
  //iniitalize
  announceState();  
}

announceSwitchChange();
              
            
!
999px

Console