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>
  <h1>Sélecteur de thème clair/sombre</h1>

  <button class="theme-switcher js-theme-switcher" type="button" aria-pressed="false">
    <span class="visually-hidden">Changer le mode de couleurs</span>
    <span class="theme-icon">
      <svg class="theme-icon-light" aria-hidden="true" width="24" height="24" viewBox="0 0 24 24" fill="transparent" stroke="currentColor" stroke-linecap="round">
        <path fill="currentColor" d="M18 12a6 6 0 1 1-12 0 6 6 0 0 1 12 0Z"/>
        <path fill="currentColor" fill-rule="evenodd" d="M12 1.25c.41 0 .75.34.75.75v1a.75.75 0 0 1-1.5 0V2c0-.41.34-.75.75-.75ZM4.4 4.4c.3-.3.77-.3 1.06 0l.4.4a.75.75 0 0 1-1.07 1.05l-.4-.4a.75.75 0 0 1 0-1.05Zm15.2 0c.3.3.3.77 0 1.06l-.4.4a.75.75 0 0 1-1.05-1.07l.4-.4a.76.76 0 0 1 1.05 0ZM1.25 12c0-.41.34-.75.75-.75h1a.75.75 0 0 1 0 1.5H2a.75.75 0 0 1-.75-.75Zm19 0c0-.41.34-.75.75-.75h1a.75.75 0 0 1 0 1.5h-1a.75.75 0 0 1-.75-.75Zm-2.1 6.15c.3-.3.77-.3 1.06 0l.4.4a.75.75 0 1 1-1.07 1.05l-.4-.4a.75.75 0 0 1 0-1.05Zm-12.3 0c.3.3.3.77 0 1.06l-.4.4a.75.75 0 1 1-1.05-1.07l.4-.4a.74.74 0 0 1 1.05 0Zm6.15 2.1c.41 0 .75.34.75.75v1a.75.75 0 0 1-1.5 0v-1c0-.41.34-.75.75-.75Z" clip-rule="evenodd"/>
      </svg>
      <svg class="theme-icon-dark" aria-hidden="true" width="24" height="24" viewBox="0 0 24 24" fill="transparent" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round">
        <path d="M21 12.79A9 9 0 1 1 11.21 3 7 7 0 0 0 21 12.79z" />
      </svg>
    </span>
  </button>

  <section>
    <p>Le thème correspond par défaut au thème utilisateur (<code translate="no">@media prefers-color-scheme</code>) ou à la valeur stockée en <code translate="no">localStorage</code>.</p>
    <p>Le sélecteur ajoute ou modifie l'attribut <code translate="no">data-theme</code> de l'élément <code translate="no">html</code> en lui attribuant la valeur "dark" ou "light".</p>
    <p>La valeur est ensuite stockée en <code translate="no">localStorage</code>.</p>
  </section>
</main>
              
            
!

CSS

              
                .theme-switcher {
  --theme-switcher-radius: 40px;
  --theme-switcher-padding: 4px;
  --theme-switcher-outline-color: light-dark(#777, #bbb);
  --theme-switcher-outline-size: 2px;
  --theme-switcher-background: light-dark(#333, #777);
  --theme-switcher-icon-size: 24px;
  --theme-switcher-icon-color: #fff;
  --theme-switcher-icon-background: light-dark(#777, #333);
  --theme-switcher-transition: 0.25s;

  display: inline-flex;
  justify-content: start;
  width: calc((var(--theme-switcher-icon-size)) * 2.3);
  padding: var(--theme-switcher-padding);
  border: 0;
  outline: var(--theme-switcher-outline-size) solid var(--theme-switcher-outline-color);
  border-radius: var(--theme-switcher-radius);
  background-color: var(--theme-switcher-background);
  line-height: 0;
  cursor: pointer;
  
  &::before {
    content: "";
    flex: 0 0 0;
    transition: var(--theme-switcher-transition) flex-grow;
    will-change: flex-grow;
  }

  & * {
    pointer-events: none;
  }

  & > .theme-icon {
    padding: 2px;
    border-radius: 50%;
    color: var(--theme-switcher-icon-color);
    background-color: var(--theme-switcher-icon-background);
    rotate: 0deg;
    transition: var(--theme-switcher-transition) rotate;
  }
  

  & [class^="theme-icon-"] {
    width: var(--theme-switcher-icon-size);
    height: var(--theme-switcher-icon-size);
  }
  
  &:focus-visible {
    outline-color: currentcolor;
    outline-offset: 2px;
  }
  
  &[aria-pressed="false"] {

    & .theme-icon-dark {
      display: none;
    }
  }

  &[aria-pressed="true"] {
    
    & .theme-icon-light {
      display: none;
    }
    
    & > .theme-icon {
      rotate: 240deg;
    }

    &::before {
      flex: 1 0 0;
   }
  }

}

:root {
  color-scheme: light dark;
  --colors-white: #EEEEEE;
  --colors-black: #333333;
  --colors-primary: light-dark(var(--colors-black), var(--colors-white));
  --colors-secondary: light-dark(var(--colors-white), var(--colors-black));
  --font-size-base: 1.2rem;
  --font-weight-normal: 400;
  --font-weight-bold: 700;
  --border-radius-full: 9999px;
}

&[data-theme="light"] {
  color-scheme: light;
}

&[data-theme="dark"] {
  color-scheme: dark;
}

@media (prefers-reduced-motion: reduce) {
  * {
    transition: none !important;
  }
}

html {
  color: var(--colors-primary);
  background-color: var(--colors-secondary);
  font-family: system-ui, sans-serif;
}

body {
  display: grid;
  min-height: 100dvh;
  place-content: start center;
  justify-items: center;
  margin: 0;
  font-size: var(--font-size-base);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  border: 0;
}

a {
  color: inherit;
}

code {
  font-size: inherit;
}

main {
  padding: 4rem;
  margin-inline: auto;
  max-width: 800px;
}


@media (width >= 40rem) {
  main {
    max-width: 1000px;
  }
}

              
            
!

JS

              
                // Encapsulation du code dans une fonction immédiatement invoquée
(function() {
  // Déclaration des variables
  const themeSwitcher = {
    button: document.querySelector('.js-theme-switcher'),
    prefersDark: window.matchMedia('(prefers-color-scheme: dark)'),
    currentTheme: null
  };

  // Initialisation du thème
  function initTheme() {
    if (localStorage.getItem('theme')) {
      themeSwitcher.currentTheme = localStorage.getItem('theme');
    } else if (themeSwitcher.prefersDark.matches) {
      themeSwitcher.currentTheme = 'dark';
    } else {
      themeSwitcher.currentTheme = 'light';
    }
    setTheme(themeSwitcher.currentTheme);
  }

  // Gestion du clic sur le bouton
  themeSwitcher.button.addEventListener('click', function() {
    themeSwitcher.currentTheme = document.documentElement.getAttribute('data-theme') === "dark" ? "light" : "dark";
    setTheme(themeSwitcher.currentTheme);
  });

  // Gestion du changement de préférence système
  themeSwitcher.prefersDark.addEventListener('change', function(event) {
    themeSwitcher.currentTheme = event.matches ? 'dark' : 'light';
    setTheme(themeSwitcher.currentTheme);
  });

  // Fonction pour définir le thème
  function setTheme(theme) {
    const pressed = theme === 'dark' ? 'true' : 'false';
    document.documentElement.setAttribute('data-theme', theme);
    localStorage.setItem('theme', theme);
    themeSwitcher.button.setAttribute('aria-pressed', pressed);
  }

  // Initialisation
  initTheme();
})();

              
            
!
999px

Console