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

              
                <div class="container">
  <h1>Switch between the dark and light mode via CSS custom properties</h1>
  <p>The example also supports dark mode media query (prefers-color-scheme) and storing selected mode to localStorage.</p>
</div>

<div class="container">
  <p><strong>Toggle light / dark mode</strong></p>
  <label class="switch">
    <input type="checkbox" class="switch__state">
    <span class="switch__toggle"></span>
  </label>
</div>

<div class="container">
  <p class="ciu_embed" data-feature="css-variables" data-periods="future_1,current,past_1,past_2" data-accessible-colours="false">
    <a href="http://caniuse.com/#feat=css-variables">Can I Use css-variables?</a> Data on support for the css-variables feature across the major browsers from caniuse.com.
  </p>
  <p class="ciu_embed" data-feature="prefers-color-scheme" data-periods="future_1,current,past_1,past_2" data-accessible-colours="false">
		<a href="http://caniuse.com/#feat=prefers-color-scheme">Can I Use prefers-color-scheme?</a> Data on support for the prefers-color-scheme feature across the major browsers from caniuse.com.
	</p>
</div>

              
            
!

CSS

              
                $color-light: #fff;
$color-dark: #333;
$color-grey: #c4c4c4;
$color-green: #59ac40;

$transition-time: 0.3s;

:root {
  --theme-bg-color: var(#{$color-light});
  --theme-text-color: var(#{$color-dark});
}

html {
  font-size: 16px;
  line-height: 1;
}
body {
  background: var(--theme-bg-color);
  color: var(--theme-text-color);
  margin: 20px;
  transition: background $transition-time, color $transition-time;
  
  &[data-theme="dark"] {
    --theme-bg-color: #{$color-dark};
    --theme-text-color: #{$color-light};
  }
}

.container {
  margin: 0 auto 30px;
  max-width: 800px;
}

h1 {
  line-height: 1.2;
}

.switch {
  background: transparent;
  border-radius: 20px;
  cursor: pointer;
  display: block;
  height: 40px;
  position: relative;
  width: 80px;
}
.switch__toggle {
  background: $color-dark;
  border-radius: 20px;
  display: block;
  height: 100%;
  transition: background $transition-time;
  &::after {
    background: $color-light;
    border-radius: 100%;
    content: "";
    display: block;
    height: 34px;
    left: 0;
    margin: 3px;
    position: absolute;
    top: 0;
    transition: transform $transition-time, background $transition-time;
    width: 34px;
  }
}
.switch__state {
  opacity: 0;
  pointer-events: none;
  position: absolute;
  &:focus ~ .switch__toggle {
    box-shadow: 0 0 2px 3px rgba(0, 160, 204, 0.8);
  }
  &:checked ~ .switch__toggle {
    background: $color-light;
    &::after {
      background: $color-dark;
      transform: translate(calc(80px - 40px));
    }
  }
}

              
            
!

JS

              
                const THEME_LIGHT = 'light'
const THEME_DARK = 'dark'
const switchStateInput = document.querySelector('.switch__state')

const getThemeName = (darkEnabled) => darkEnabled ? THEME_DARK : THEME_LIGHT

const setTheme = (theme) => {
  document.body.setAttribute('data-theme', theme)
  localStorage.setItem('theme', theme)
  switchStateInput.checked = theme === THEME_DARK
}

switchStateInput.addEventListener('change', event => setTheme(getThemeName(event.target.checked)))

const mql = window.matchMedia('screen and (prefers-color-scheme: dark)')
const matchMediaListener = (event) => setTheme(getThemeName(event.matches))

mql.addListener(matchMediaListener)

const storedTheme = localStorage.getItem('theme')
const selectedTheme = storedTheme ? storedTheme : getThemeName(mql.matches)

setTheme(selectedTheme)

              
            
!
999px

Console