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>Custom color mode toggler for Bootstrap v5.3.0+</h1>

<ul class="switches">
  <li>
    <input type="radio" id="light" name="theme-mode" checked>
    <label for="light">
      <span>Light</span>
      <span></span>
    </label>
  </li>
  <li>
    <input type="radio" id="dark" name="theme-mode">
    <label for="dark">
      <span>Dark</span>
      <span></span>
    </label>
  </li>
  <li>
    <input type="radio" id="auto" name="theme-mode">
    <label for="auto">
      <span>Auto</span>
      <span></span>
    </label>
  </li>
</ul>

<footer class="page-footer">
  <span>made by </span>
  <a href="https://georgemartsoukos.com/" target="_blank">
    <img width="24" height="24" src="https://assets.codepen.io/162656/george-martsoukos-small-logo.svg" alt="George Martsoukos logo">
  </a>
</footer>
 
              
            
!

CSS

              
                /* RESET STYLES & VARIABLES
–––––––––––––––––––––––––––––––––––––––––––––––––– */
:root {
  --white-pearl: #ede1d2;
  --gray: #e8e8ec;
  --slate-gray: #789392;
}

ul {
  list-style: none;
}

h1 {
  font-size: 26px;
}

body {
  margin: 30px auto;
  text-align: center;
}

/* MAIN STYLES
–––––––––––––––––––––––––––––––––––––––––––––––––– */
.switches {
  display: inline-block;
  padding: 0;
  border: 1px solid var(--gray);
  margin: 10px 0 0;
  border-radius: 6px;
}

.switches li {
  position: relative;
}

.switches li:not(:last-child) {
  border-bottom: 1px solid var(--gray);
}

.switches li [type="radio"] {
  position: absolute;
  left: -9999px;
}

.switches label {
  display: grid;
  grid-template-columns: 40px auto;
  align-items: center;
  gap: 10px;
  padding: 20px;
  cursor: pointer;
}

.switches span {
  flex-shrink: 0;
}

.switches span:empty {
  position: relative;
  width: 50px;
  height: 26px;
  border-radius: 15px;
  background: var(--gray);
  transition: all 0.3s;
}

.switches span:empty::before,
.switches span:empty::after {
  content: "";
  position: absolute;
}

.switches span:empty::before {
  top: 1px;
  left: 1px;
  width: 24px;
  height: 24px;
  background: var(--slate-gray);
  border-radius: 50%;
  z-index: 1;
  transition: transform 0.3s;
}

.switches span:empty::after {
  top: 50%;
  transform: translateY(-50%);
  width: 14px;
  height: 14px;
  left: 6px;
  background-size: 14px 14px;
}

.switches [type="radio"]:checked + label span:empty {
  background: var(--white-pearl);
}

.switches [type="radio"]:checked + label span:empty::before {
  transform: translateX(24px);
}

.switches li:nth-child(1) [type="radio"]:checked + label span:empty::after {
  background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA1MTIgNTEyIj48IS0tIUZvbnQgQXdlc29tZSBGcmVlIDYuNy4yIGJ5IEBmb250YXdlc29tZSAtIGh0dHBzOi8vZm9udGF3ZXNvbWUuY29tIExpY2Vuc2UgLSBodHRwczovL2ZvbnRhd2Vzb21lLmNvbS9saWNlbnNlL2ZyZWUgQ29weXJpZ2h0IDIwMjQgRm9udGljb25zLCBJbmMuLS0+PHBhdGggZD0iTTM2MS41IDEuMmM1IDIuMSA4LjYgNi42IDkuNiAxMS45TDM5MSAxMjFsMTA3LjkgMTkuOGM1LjMgMSA5LjggNC42IDExLjkgOS42czEuNSAxMC43LTEuNiAxNS4yTDQ0Ni45IDI1Nmw2Mi4zIDkwLjNjMy4xIDQuNSAzLjcgMTAuMiAxLjYgMTUuMnMtNi42IDguNi0xMS45IDkuNkwzOTEgMzkxIDM3MS4xIDQ5OC45Yy0xIDUuMy00LjYgOS44LTkuNiAxMS45cy0xMC43IDEuNS0xNS4yLTEuNkwyNTYgNDQ2LjlsLTkwLjMgNjIuM2MtNC41IDMuMS0xMC4yIDMuNy0xNS4yIDEuNnMtOC42LTYuNi05LjYtMTEuOUwxMjEgMzkxIDEzLjEgMzcxLjFjLTUuMy0xLTkuOC00LjYtMTEuOS05LjZzLTEuNS0xMC43IDEuNi0xNS4yTDY1LjEgMjU2IDIuOCAxNjUuN2MtMy4xLTQuNS0zLjctMTAuMi0xLjYtMTUuMnM2LjYtOC42IDExLjktOS42TDEyMSAxMjEgMTQwLjkgMTMuMWMxLTUuMyA0LjYtOS44IDkuNi0xMS45czEwLjctMS41IDE1LjIgMS42TDI1NiA2NS4xIDM0Ni4zIDIuOGM0LjUtMy4xIDEwLjItMy43IDE1LjItMS42ek0xNjAgMjU2YTk2IDk2IDAgMSAxIDE5MiAwIDk2IDk2IDAgMSAxIC0xOTIgMHptMjI0IDBhMTI4IDEyOCAwIDEgMCAtMjU2IDAgMTI4IDEyOCAwIDEgMCAyNTYgMHoiLz48L3N2Zz4=");
}

.switches li:nth-child(2) [type="radio"]:checked + label span:empty::after {
  background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAzODQgNTEyIj48IS0tIUZvbnQgQXdlc29tZSBGcmVlIDYuNy4yIGJ5IEBmb250YXdlc29tZSAtIGh0dHBzOi8vZm9udGF3ZXNvbWUuY29tIExpY2Vuc2UgLSBodHRwczovL2ZvbnRhd2Vzb21lLmNvbS9saWNlbnNlL2ZyZWUgQ29weXJpZ2h0IDIwMjQgRm9udGljb25zLCBJbmMuLS0+PHBhdGggZD0iTTIyMy41IDMyQzEwMCAzMiAwIDEzMi4zIDAgMjU2UzEwMCA0ODAgMjIzLjUgNDgwYzYwLjYgMCAxMTUuNS0yNC4yIDE1NS44LTYzLjRjNS00LjkgNi4zLTEyLjUgMy4xLTE4LjdzLTEwLjEtOS43LTE3LTguNWMtOS44IDEuNy0xOS44IDIuNi0zMC4xIDIuNmMtOTYuOSAwLTE3NS41LTc4LjgtMTc1LjUtMTc2YzAtNjUuOCAzNi0xMjMuMSA4OS4zLTE1My4zYzYuMS0zLjUgOS4yLTEwLjUgNy43LTE3LjNzLTcuMy0xMS45LTE0LjMtMTIuNWMtNi4zLS41LTEyLjYtLjgtMTktLjh6Ii8+PC9zdmc+");
}

.switches li:nth-child(3) [type="radio"]:checked + label span:empty::after {
  background-image: url("data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCA1MTIgNTEyIj48IS0tIUZvbnQgQXdlc29tZSBGcmVlIDYuNy4yIGJ5IEBmb250YXdlc29tZSAtIGh0dHBzOi8vZm9udGF3ZXNvbWUuY29tIExpY2Vuc2UgLSBodHRwczovL2ZvbnRhd2Vzb21lLmNvbS9saWNlbnNlL2ZyZWUgQ29weXJpZ2h0IDIwMjQgRm9udGljb25zLCBJbmMuLS0+PHBhdGggZD0iTTQ0OCAyNTZjMC0xMDYtODYtMTkyLTE5Mi0xOTJsMCAzODRjMTA2IDAgMTkyLTg2IDE5Mi0xOTJ6TTAgMjU2YTI1NiAyNTYgMCAxIDEgNTEyIDBBMjU2IDI1NiAwIDEgMSAwIDI1NnoiLz48L3N2Zz4=");
}

/* FOOTER STYLES
–––––––––––––––––––––––––––––––––––––––––––––––––– */
.page-footer {
  position: fixed;
  right: 0;
  bottom: 50px;
  display: flex;
  align-items: center;
  padding: 5px;
  font-size: 0.9em;
  color: var(--text-color);
  background: var(--bg-color);
}

.page-footer a {
  display: flex;
  margin-left: 4px;
}

              
            
!

JS

              
                const html = document.documentElement;
const switches = document.querySelector(".switches");
const inputs = switches.querySelectorAll("input");

if (localStorage.getItem("bs-dark-mode")) {
  html.setAttribute("data-bs-theme", "dark");
}

if (localStorage.getItem("bs-selected-radio")) {
  switches.querySelector(
    `#${localStorage.getItem("bs-selected-radio")}`
  ).checked = "true";
}

const setTheme = (theme) => {
  if (theme === "dark") {
    html.setAttribute("data-bs-theme", "dark");
    localStorage.setItem("bs-dark-mode", "true");
  } else {
    html.removeAttribute("data-bs-theme");
    localStorage.removeItem("bs-dark-mode");
  }
};

const handleMediaChange = (e) => {
  if (switches.querySelector('[type="radio"]:checked').id === "auto") {
    setTheme(e.matches ? "dark" : "light");
  }
};

const handleInputChange = (e) => {
  const themeMode = e.target.id;
  if (
    themeMode === "dark" ||
    (themeMode === "auto" &&
      window.matchMedia("(prefers-color-scheme: dark)").matches)
  ) {
    setTheme("dark");
  } else {
    setTheme("light");
  }
  localStorage.setItem("bs-selected-radio", themeMode);
};

window
  .matchMedia("(prefers-color-scheme: dark)")
  .addEventListener("change", handleMediaChange);

inputs.forEach((input) => input.addEventListener("input", handleInputChange));

              
            
!
999px

Console