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

              
                <html data-theme="light">
  <body>
<button
    type="button"
    data-theme-toggle
    aria-label="Light mode"
  >Light mode</button>
  </body>
</html>
              
            
!

CSS

              
                :root {
  --color-white: #eee;
  --color-black: #333;
}

body {
  display: grid;
  min-height: 100vh;
  place-items: center;
  
  color: var(--color-black);
  background-color: var(--color-white);
  
  [data-theme="dark"] & {
    color: var(--color-white);
    background-color: var(--color-black);
  }
}

button {
  font-size: 2rem;
  font-weight: bold;
  padding: 0.5rem 1rem;
  transition: all var(--global-transition-time) ease-in-out;
  border-radius: 2rem;
  cursor: pointer;
  border: 1px solid;
  box-shadow: 1px 1px 6px #333;
  
  color: var(--color-black);
  background-color: var(--color-white);
  
  [data-theme="dark"] & {
    color: var(--color-white);
    background-color: var(--color-black);
  }
}
              
            
!

JS

              
                /**
* Utility function to calculate the current theme setting.
* Look for a local storage value.
* Fall back to system setting.
* Fall back to light mode.
* See https://whitep4nth3r.com/blog/best-light-dark-mode-theme-toggle-javascript/
*/

const MsgWhenDark = "light 🔅";
const MsgWhenLight = "dark 🌙";
function calculateSettingAsThemeString({ localStorageTheme, systemSettingDark }) {
  if (localStorageTheme !== null) {
    return localStorageTheme;
  }

  if (systemSettingDark.matches) {
    return "dark";
  }

  return "light";
}

/**
* Utility function to update the button text and aria-label.
*/
function updateButton({ buttonEl, isDark }) {
  const newCta = isDark ? MsgWhenDark : MsgWhenLight;  
  buttonEl.innerText = newCta;
  
  buttonEl.classList = isDark
    ? "theme-button theme-button-dark"
  : "theme-button theme-button-light";

  // use an aria-label if you are omitting text on the button
  // and using a sun/moon icon, for example
  const newAriaLabel = isDark
  ? "Passer en mode clair"
  : "Passer en mode sombre";

  buttonEl.setAttribute("aria-label", newAriaLabel);
}

/**
* Utility function to update the theme setting on the html tag
*/
function updateThemeOnHtmlEl({ theme }) {
  document.querySelector("html").setAttribute("data-theme", theme);
}


/**
* On page load:
*/

/**
* 1. Grab what we need from the DOM and system settings on page load
*/
const button = document.querySelector("[data-theme-toggle]");
const localStorageTheme = localStorage.getItem("theme");
const systemSettingDark = window.matchMedia("(prefers-color-scheme: dark)");

/**
* 2. Work out the current site settings
*/
let currentThemeSetting = calculateSettingAsThemeString({ localStorageTheme, systemSettingDark });

/**
* 3. Update the theme setting and button text accoridng to current settings
*/
updateButton({ buttonEl: button, isDark: currentThemeSetting === "dark" });
updateThemeOnHtmlEl({ theme: currentThemeSetting });

/**
* 4. Add an event listener to toggle the theme
*/
button.addEventListener("click", (event) => {
  const newTheme = currentThemeSetting === "dark" ? "light" : "dark";

  localStorage.setItem("theme", newTheme);
  updateButton({ buttonEl: button, isDark: newTheme === "dark" });
  updateThemeOnHtmlEl({ theme: newTheme });

  currentThemeSetting = newTheme;
}); 
              
            
!
999px

Console