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

              
                <label class="switch switch--sm">
  <span class="switch__label">Enable dark mode</span>
  <span class="switch__container">
    <input type="checkbox" class="switch__input" data-toggle="theme">
    <span class="switch__slider"></span>
  </span>
</label>

<span class="icon"></span>

<article class="card">
  <header class="card__header">
    <h1 class="card__title">CSS for developers</h1>
  </header>
  <div class="card__body">
    <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Assumenda soluta ab beatae obcaecati reiciendis doloribus porro natus distinctio saepe hic laboriosam.</p>
  </div>
  <footer class="card__footer">
    <a href="#" class="btn card__btn">Action link</a>
    <a href="#" class="btn card__btn">Another action link</a>
  </footer>
</article>

              
            
!

CSS

              
                // Light theme
:root {
  --body-bg: #fff;
  --card-border-color: #ddd;
  --card-bg-color: #fff;
  --card-header-color: #e0e0e0;
  --card-title-color: #555;
  --card-text-color: #4a4a4a;
  --card-footer-color: #f8f8f8;
  --card-btn-text: dodgerblue;
  --icon: "🐵";
  
  // Switch
  --switch-size: 24px;
  --switch-radius: 30px;
  --switch-color-off: #ddd;
  --switch-color-on: #3cb371;
  --switch-label-size: 1rem;
  --slider-color: #fff;
  --slider-size: var(--switch-size);
}

// Dark Theme
:root[data-theme="dark"] {
  --body-bg: #222;
  --card-border-color: #fff;
  --card-bg-color: #333;
  --card-header-color: #666;
  --card-title-color: #e5e5e5;
  --card-text-color: #fff;
  --card-footer-color: #222;
  --card-btn-text: tomato;
  --icon: "🙈";
}


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

* {
  transition: all 0.25s;
}

body {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background-color: var(--body-bg);
  font-family: 'Open Sans', sans-serif;
  font-size: 16px;
  line-height: 1.6;
}

.btn {
  display: inline-block;
  border: 0;
  border-radius: 4px;
  margin-bottom: 25px;
  padding: 10px 15px;
  background-color: var(--button-bg);
  font-size: 1rem;
  font-weight: bold;
  cursor: pointer;
  outline: 0 none;
}


// ------------------------------------------------
// CARD
// ------------------------------------------------
.card {
  width: 500px;
  border: 1px solid var(--card-border-color);
  border-radius: 5px;
  background: var(--card-bg-color);
  box-shadow: 1px 2px 15px rgba(0, 0, 0, 0.1);
  
  &__header,
  &__body,
  &__footer {
    padding: 13px;
  }
  
  &__header {
    border-radius: 4px 4px 0 0;
    background: var(--card-header-color);
  }
  
  &__title {
    margin: 0;
    color: var(--card-title-color);
    font-family: 'Sansita', sans-serif;
    font-size: 2rem;
  }
  
  &__body {
    color: var(--card-text-color);
    font-size: 1rem;
  }
  
  &__footer {
    display: flex;
    border-radius: 0 0 4px 4px;
    background: var(--card-footer-color);
  }
  
  &__btn {
    margin-bottom: 0;
    padding: 0;
    background-color: transparent;
    color: var(--card-btn-text);
    font-size: 0.825rem;
    text-decoration: underline;
    &:not(:first-child)::before {
      content: "";
      border-left: 1px solid #ccc;
      margin-left: 0.85rem;
      padding-left: 0.85rem;
    }
  }
}

.icon {
  margin-right: 3rem;
  font-size: 5rem;
  &::before {
    content: var(--icon);    
  }
}


// ------------------------------------------------
// SWITCH
// ------------------------------------------------
.switch {
  flex-basis: 13rem;
  order: 2;
  display: flex;
  align-items: center;
  margin-left: 50px;
  cursor: pointer;
  
  // If more than one
  & + & {
    margin-left: 3rem;
  }
  
  
  // Label
  // ------------------------------------------------
  &__label {
    margin-left: 0.65rem;
    color: var(--card-text-color);
    font-family: 'Roboto', sans-serif;
    font-size: var(--switch-label-size);
    font-weight: 200;
  }

  
  
  // Container
  // ------------------------------------------------
  &__container {
    order: -1;
    position: relative;
    display: block;
    width: calc((var(--switch-size) * 2));
    height: var(--switch-size);
    //overflow: hidden;
  }
  
  
  // Checkbox
  // ------------------------------------------------
  &__input {
    position: absolute;
    left: -999rem;
  }
  
  
  // Slider
  // ------------------------------------------------
  &__slider,
  &__slider::after {
    position: absolute;
    border-radius: var(--switch-radius);
    transition: transform 400ms ease-out,
                border-color 300ms ease-out,
                background 300ms ease-out;
  }
  
  &__slider {
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background-color: var(--switch-color-off);
    
    // Slider
    &::after {
      content: "";
      width: var(--slider-size);
      height: var(--slider-size);
      background-color: var(--slider-color);
      transform: translate3d(0, 0, 0);
      border: 2px solid var(--switch-color-off);
      //box-shadow: 0 0 4px rgba(#000, .35);
    }
  }
  
  
  // Checkbox checked
  // ------------------------------------------------
  &__input:checked ~ .switch__slider {
    background-color: var(--switch-color-on);
    &::after {
      border-color: var(--switch-color-on);
      transform: translate3d(100%, 0, 0);
    } 
  }
}
              
            
!

JS

              
                const toggleBtn = document.querySelector('[data-toggle="theme"]');
const textToggleBtn = document.querySelector('.switch__label');

// Turn the theme on if the 'data-theme=dark' attribute exists in localStorage
if (localStorage.getItem('data-theme', 'dark')) {
  document.documentElement.setAttribute('data-theme', 'dark');
  document.querySelector('[data-toggle="theme"]').checked = true;
  textToggleBtn.innerText = 'Disable dark mode';
}

toggleBtn.addEventListener('click', (e) => {
  if (document.documentElement.hasAttribute('data-theme')) {
    // Turning the theme off
    document.documentElement.removeAttribute('data-theme');
    // Change text button
    textToggleBtn.innerText = 'Enable dark mode';
    // Store the preference
    localStorage.removeItem('data-theme', 'dark');
  }
  else {
    // Turning the theme on
    document.documentElement.setAttribute('data-theme', 'dark');
    textToggleBtn.innerText = 'Disable dark mode';
    // Store the preference
    localStorage.setItem('data-theme', 'dark');
  }
});
              
            
!
999px

Console