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

              
                <h1>System-Mirroring Dark Mode with Persisted Overrides</h1>

<p>This page will be light or dark based on the user’s system preferences until overridden with site-specific preference (either <code>light</code> or <code>dark</code>). CSS-wise, the change is made by adding a <code>theme--dark</code> class to <code>&lt;html&gt;</code>.</p>

<p>Locally saved user preference: <output class="badge" id="theme_preference" style="text-transform: uppercase">…<noscript> We really need JavaScript</noscript></output></p>

<p>
  <button id="light">Light</button>
  <button id="system">System</button>
  <button id="dark">Dark</button>
</p>

<p>JavaScript is required for dark mode, even though it can be done with pure CSS. (It is currently not possible to have manual overrides <em>and</em> system-mirroring dark mode without one of: JavaScript, duplicated CSS, or multiple stylesheets. If I am wrong, <a href="https://alanhogan.com/contact?from=https://codepen.io/alanhogan/pen/PoEgwxL&reason=removing%20duplication%20without%20losing%20CSS-only%20automatic%20dark%20mode">let me know</a>.)</p>

<p><a href="https://alanhogan.com/code/modern-html-template">Based on the modern HTML template</a> by <a href="https://alanhogan.com/">Alan Hogan</a></p>

<fieldset>
  <legend>Unstyled Elements</legend>
  <input type=checkbox />
  <input value="" />
  <input type=submit value=Go />
</fieldset>

<div class=scroll>
  This box is intended to
  show how scrollbars look,
  which may be affected by the
  <code>color-scheme</code>
  CSS property.
  <br>
  <br>
  Exciting, I know.
</diV>
              
            
!

CSS

              
                button.current {
  background-color: var(--page-bg-color);
  color: var(--text-color);
  border-color: var(--page-bg-color);
  border-style: solid;
  font-weight: 600;
  filter: contrast(0.8);
}
button {
  min-width: 6.5em;
  border-radius: 0.2em;
}
button:not(.current) {
  color: var(--page-bg-color);
  background-color: var(--link-color);
  border-color: var(--link-color);
}
button:not(:disabled) {
  cursor: pointer;
}

.scroll {
  margin: 1.6em 0;
  padding: 0.6em;
  border: 1px solid currentColor;
  max-width: 12em;
  max-height: 4em;
  overflow: auto;
}

/* Automatic dark mode, system fonts, and readable max-width
 * via https://ajh.us/template */

:root {
  --page-bg-color: white;
  --text-color: #222;
  --link-color: #06d;
  --visited-link-color: #91e;
  --code-color: #456658;
  --badge-bg: #eee;
  --badge-text-color: #444;
  color-scheme: light;
}

.theme--dark:root {
  --page-bg-color: #141414;
  --text-color: #eee;
  --link-color: #2af;
  --visited-link-color: #c5f;
  --code-color: #c5eddc;
  --badge-bg: #252525;
  --badge-text-color: #ccc;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}
html {
  margin: 0;
  padding: 0;
}
html,
body,
input,
button,
textarea {
  font-family: system-ui, sans-serif;
}
body {
  padding: calc(0.5em + 0.5vmin);
  margin: 0 auto;
  max-width: 40em;
  background-color: var(--page-bg-color);
  color: var(--text-color);
}

:link {
  color: var(--link-color);
}
:visited {
  color: var(--visited-link-color);
}

code,
tt,
kbd,
pre {
  font-family: "JetBrains Mono", "IBM Plex Mono", "Source Code Pro",
    "Cascadia Code", ui-monospace, Menlo, "Ubuntu Monospace", "Inconsolata",
    "Fira Code", "Deja Vu Sans Mono", "Bitstream Vera Sans Mono", Monaco,
    "Segoe UI Mono", "Roboto Mono", "Oxygen Mono", Consolas, monospace;
  font-size: 90%;
  color: var(--code-color);
}
pre code,
pre tt,
pre kbd {
  font-size: 100%;
}
.badge {
  font-size: 80%;
  font-weight: 500;
  display: inline-block;
  padding: 0.05em 0.4em;
  background: var(--badge-bg);
  color: var(--badge-text-color);
  border-radius: 0.2em;
  text-decoration: none;
  position: relative;
  bottom: 0.1em;
}

              
            
!

JS

              
                // Constants
const themes = { system: "system", dark: "dark", light: "light" };
const themeMediaQuery = matchMedia("(prefers-color-scheme: dark)");

// Helpers
const getThemePreference = () => localStorage.getItem("themePreference");

// State
const themeSettings = {
  system: themeMediaQuery.matches ? themes.dark : themes.light,
  preference: getThemePreference() || themes.system
};

function calculateTheme(settings) {
  if (settings.preference == themes.system) {
    return settings.system;
  } else if (settings.preference == themes.dark) {
    return themes.dark;
  } else return themes.light;
}
function applyTheme(theme) {
  if (theme === themes.dark) {
    document.documentElement.classList.add("theme--dark");
  } else {
    document.documentElement.classList.remove("theme--dark");
  }
}

function recalculateAndApplyTheme() {
  applyTheme(calculateTheme(themeSettings));
}

function initializeThemes() {
  recalculateAndApplyTheme();
  themeMediaQuery.addEventListener("change", (e) => {
    themeSettings.system = e.matches ? themes.dark : themes.light;
    recalculateAndApplyTheme();
  });
}

// Should run this in document.head() to avoid flashes of incorrect theme.
initializeThemes();

///////////////////////////////////////////////
/// BELOW THIS LINE CAN BE AFTER BODY/LOAD. ///
///////////////////////////////////////////////

// DOM HELPERS ----------
const qs = (s) => document.querySelector(s);

// USER PREFERENCE UPDATING -------
const setThemePreference = (pref) => {
  if (!themes.hasOwnProperty(pref)) {
    console.error("Invalid theme", pref, "not found in", themes.keys());
  } else {
    themeSettings.preference = pref;
    localStorage.setItem("themePreference", pref);
    recalculateAndApplyTheme();
  }
};

// HOOKING UP THE BUTTONS ----------
var showCurrentPreference = () => {
  const curPref = getThemePreference();
  qs("#theme_preference").innerText = curPref || "Never Set";
  const oldBtn = qs(".current");
  if (oldBtn) {
    oldBtn.classList.remove("current");
    oldBtn.disabled = false;
  }
  if (themes[curPref]) {
    const newBtn = qs(`#${curPref}`);
    newBtn.classList.add("current");
    newBtn.disabled = true;
  }
};

qs("#light").addEventListener("click", (e) => {
  setThemePreference(themes.light);
  showCurrentPreference();
});

qs("#system").addEventListener("click", (e) => {
  setThemePreference(themes.system);
  showCurrentPreference();
});

qs("#dark").addEventListener("click", (e) => {
  setThemePreference(themes.dark);
  showCurrentPreference();
});
// End button hook-ups

// Show current:
showCurrentPreference();

// TODO: Listen to event for theme preference changes to update the buttons.

              
            
!
999px

Console