123

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <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>

            
          
!
            
              $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));
    }
  }
}

            
          
!
            
              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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console